These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /* |
||
4 | * This file is part of the Sylius package. |
||
5 | * |
||
6 | * (c) Paweł Jędrzejewski |
||
7 | * |
||
8 | * For the full copyright and license information, please view the LICENSE |
||
9 | * file that was distributed with this source code. |
||
10 | */ |
||
11 | |||
12 | declare(strict_types=1); |
||
13 | |||
14 | namespace Sylius\Component\Registry; |
||
15 | |||
16 | use Webmozart\Assert\Assert; |
||
17 | use Zend\Stdlib\PriorityQueue; |
||
18 | |||
19 | final class PrioritizedServiceRegistry implements PrioritizedServiceRegistryInterface |
||
20 | { |
||
21 | /** |
||
22 | * @var PriorityQueue |
||
23 | */ |
||
24 | private $services; |
||
25 | |||
26 | /** |
||
27 | * Interface which is required by all services. |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | private $interface; |
||
32 | |||
33 | /** |
||
34 | * Human readable context for these services, e.g. "tax calculation" |
||
35 | * |
||
36 | * @var string |
||
37 | */ |
||
38 | private $context; |
||
39 | |||
40 | /** |
||
41 | * @param string $interface |
||
42 | * @param string $context |
||
43 | */ |
||
44 | public function __construct(string $interface, string $context = 'service') |
||
45 | { |
||
46 | $this->interface = $interface; |
||
47 | $this->services = new PriorityQueue(); |
||
48 | $this->context = $context; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * {@inheritdoc} |
||
53 | */ |
||
54 | public function all(): iterable |
||
55 | { |
||
56 | return $this->services; |
||
0 ignored issues
–
show
|
|||
57 | } |
||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | */ |
||
62 | public function register($service, int $priority = 0): void |
||
63 | { |
||
64 | $this->assertServiceHaveType($service); |
||
65 | $this->services->insert($service, $priority); |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * {@inheritdoc} |
||
70 | */ |
||
71 | public function unregister($service): void |
||
72 | { |
||
73 | if (!$this->has($service)) { |
||
74 | throw new NonExistingServiceException($this->context, gettype($service), array_keys($this->services->toArray())); |
||
75 | } |
||
76 | |||
77 | $this->services->remove($service); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | public function has($service): bool |
||
84 | { |
||
85 | $this->assertServiceHaveType($service); |
||
86 | |||
87 | return $this->services->contains($service); |
||
88 | } |
||
89 | |||
90 | /** |
||
91 | * @param object $service |
||
92 | */ |
||
93 | private function assertServiceHaveType($service): void |
||
94 | { |
||
95 | Assert::isInstanceOf( |
||
96 | $service, |
||
97 | $this->interface, |
||
98 | $this->context . ' needs to implement "%2$s", "%s" given.' |
||
99 | ); |
||
100 | } |
||
101 | } |
||
102 |
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:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.