GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

CachedManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Workflow library.
5
 *
6
 * @package    workflow
7
 * @author     David Molineus <[email protected]>
8
 * @copyright  2014-2017 netzmacht David Molineus
9
 * @license    LGPL 3.0 https://github.com/netzmacht/workflow
10
 * @filesource
11
 */
12
13
declare(strict_types=1);
14
15
namespace Netzmacht\Workflow\Manager;
16
17
use Netzmacht\Workflow\Data\EntityId;
18
use Netzmacht\Workflow\Flow\Item;
19
use Netzmacht\Workflow\Flow\Workflow;
20
use Netzmacht\Workflow\Handler\TransitionHandler;
21
22
/**
23
 * Workflow manager decorator caching the items and the relation between workflows and entities.
24
 *
25
 * @package Netzmacht\Workflow\Manager
26
 */
27
class CachedManager implements Manager
28
{
29
    /**
30
     * Workflow manager.
31
     *
32
     * @var Manager
33
     */
34
    private $manager;
35
36
    /**
37
     * Workflow entity mapping.
38
     *
39
     * @var array
40
     */
41
    private $workflows = array();
42
43
    /**
44
     * Cached workflow items.
45
     *
46
     * @var array
47
     */
48
    private $items = array();
49
50
    /**
51
     * Construct.
52
     *
53
     * @param Manager $manager The inside workflow manager.
54
     */
55
    public function __construct(Manager $manager)
56
    {
57
        $this->manager = $manager;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function handle(Item $item, ?string $transitionName = null, bool $changeWorkflow = false): ?TransitionHandler
64
    {
65
        return $this->manager->handle($item, $transitionName);
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function addWorkflow(Workflow $workflow): Manager
72
    {
73
        $this->manager->addWorkflow($workflow);
74
75
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (Netzmacht\Workflow\Manager\CachedManager) is incompatible with the return type declared by the interface Netzmacht\Workflow\Manager\Manager::addWorkflow of type self.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81
    public function getWorkflow(EntityId $entityId, $entity): Workflow
82
    {
83
        $key = (string) $entityId;
84
85
        if (!isset($this->workflows[$key])) {
86
            $this->workflows[$key] = $this->manager->getWorkflow($entityId, $entity);
87
        }
88
89
        return $this->workflows[$key];
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function getWorkflowByName(string $name): Workflow
96
    {
97
        return $this->manager->getWorkflowByName($name);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function getWorkflowByItem(Item $item): Workflow
104
    {
105
        return $this->getWorkflow($item->getEntityId(), $item->getEntity());
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function hasWorkflow(EntityId $entityId, $entity): bool
112
    {
113
        $key = (string) $entityId;
114
115
        if (isset($this->workflows[$key])) {
116
            return true;
117
        }
118
119
        return $this->manager->hasWorkflow($entityId, $entity);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getWorkflows(): iterable
126
    {
127
        return $this->manager->getWorkflows();
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function createItem(EntityId $entityId, $entity): Item
134
    {
135
        $key = (string) $entityId;
136
137
        if (!isset($this->items[$key])) {
138
            $this->items[$key] = $this->manager->createItem($entityId, $entity);
139
        }
140
141
        return $this->items[$key];
142
    }
143
}
144