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