Completed
Pull Request — master (#126)
by Phil
02:35
created

DefinitionAggregate   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 80%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 75
ccs 20
cts 25
cp 0.8
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 14 2
A has() 0 10 3
A getDefinition() 0 10 3
A resolve() 0 4 1
A getIterator() 0 8 2
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 15
    public function add(string $id, $definition, bool $shared = false): DefinitionInterface
23
    {
24 15
        if (! $definition instanceof DefinitionInterface) {
25 6
            $definition = (new Definition($id, $definition));
26
        }
27
28 15
        $this->definitions[] = $definition
29 15
            ->setContainer($this->getContainer())
30 15
            ->setAlias($id)
31 15
            ->setShared($shared)
32
        ;
33
34 15
        return $definition;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function has(string $id): bool
41
    {
42
        foreach ($this->getIterator() as $definition) {
43
            if ($id === $definition->getAlias()) {
44
                return true;
45
            }
46
        }
47
48
        return false;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54 6
    public function getDefinition(string $id): DefinitionInterface
55
    {
56 6
        foreach ($this->getIterator() as $definition) {
57 6
            if ($id === $definition->getAlias()) {
58 5
                return $definition;
59
            }
60
        }
61
62 3
        throw new NotFoundException(sprintf('Alias (%s) is not being handled as a definition.', $id));
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 6
    public function resolve(string $id, array $args = [], bool $new = false)
69
    {
70 6
        return $this->getDefinition($id)->resolve($args, $new);
0 ignored issues
show
Unused Code introduced by
The call to DefinitionInterface::resolve() has too many arguments starting with $new.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 9
    public function getIterator(): Generator
77
    {
78 9
        $count = count($this->definitions);
79
80 9
        for ($i = 0; $i < $count; $i++) {
81 9
            yield $this->definitions[$i];
82
        }
83 6
    }
84
}
85