Completed
Push — master ( d81c19...f57266 )
by Kamil
20s
created

Component/Registry/PrioritizedServiceRegistry.php (1 issue)

Upgrade to new PHP Analysis Engine

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
Bug Best Practice introduced by
The return type of return $this->services; (Zend\Stdlib\PriorityQueue) is incompatible with the return type declared by the interface Sylius\Component\Registr...eRegistryInterface::all of type Sylius\Component\Registry\iterable.

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...
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