Completed
Pull Request — master (#126)
by Phil
15:30
created

DefinitionAggregate::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 3
1
<?php declare(strict_types=1);
2
3
namespace League\Container\Definition;
4
5
use Generator;
6
use League\Container\Argument\RawArgument;
7
use League\Container\ContainerAwareTrait;
8
use League\Container\Exception\NotFoundException;
9
10
class DefinitionAggregate implements DefinitionAggregateInterface
11
{
12
    use ContainerAwareTrait;
13
14
    /**
15
     * @var \League\Container\Definition\DefinitionInterface[]
16
     */
17
    protected $definitions = [];
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function add(string $id, $definition, bool $shared = false): DefinitionInterface
23
    {
24
        if (! $definition instanceof DefinitionInterface) {
25
            $definition = $this->factory($definition);
26
        }
27
28
        $definition->setAlias($id)->setShared($shared);
0 ignored issues
show
Bug introduced by
The method setAlias() does not seem to exist on object<League\Container\...on\DefinitionInterface>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
30
        $this->definitions[] = $definition;
31
32
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this; (League\Container\Definition\DefinitionAggregate) is incompatible with the return type declared by the interface League\Container\Definit...AggregateInterface::add of type League\Container\Definition\DefinitionInterface.

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...
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function factory($concrete): DefinitionInterface
39
    {
40
        // TODO handle the never ending struggle of __invoke
41
        if (is_callable($concrete)) {
42
            return (new CallableDefinition($concrete))->setContainer($this->getContainer());
43
        }
44
45
        if (is_string($concrete) && class_exists($concrete)) {
46
            return (new ClassDefinition($concrete))->setContainer($this->getContainer());
47
        }
48
49
        $concrete = ($concrete instanceof RawArgument) ? $concrete : new RawArgument($concrete);
50
51
        return new Definition($concrete);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function resolve(string $id)
58
    {
59
        foreach ($this->getIterator() as $definition) {
60
            if ($id === $definition->getAlias()) {
61
                return $definition->resolve();
62
            }
63
        }
64
65
        throw new NotFoundException(sprintf('Alias (%s) is not being handled by the container', $id));
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getIterator(): Generator
72
    {
73
        $count = count($this->definitions);
74
75
        for ($i = 0; $i < $count; $i++) {
76
            yield $this->definitions[$i];
77
        }
78
    }
79
}
80