-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
CheckTypeDeclarationsPass.php
201 lines (160 loc) · 7.15 KB
/
CheckTypeDeclarationsPass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Argument\IteratorArgument;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
use Symfony\Component\DependencyInjection\Exception\InvalidParameterTypeException;
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\Parameter;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
/**
* Checks whether injected parameters are compatible with type declarations.
*
* This pass should be run after all optimization passes.
*
* It can be added either:
* * before removing passes to check all services even if they are not currently used,
* * after removing passes to check only services are used in the app.
*
* @author Nicolas Grekas <p@tchwork.com>
* @author Julien Maulny <jmaulny@darkmira.fr>
*/
final class CheckTypeDeclarationsPass extends AbstractRecursivePass
{
private const SCALAR_TYPES = ['int', 'float', 'bool', 'string'];
private $autoload;
/**
* @param bool $autoload Whether services who's class in not loaded should be checked or not.
* Defaults to false to save loading code during compilation.
*/
public function __construct(bool $autoload = false)
{
$this->autoload = $autoload;
}
/**
* {@inheritdoc}
*/
protected function processValue($value, $isRoot = false)
{
if (!$value instanceof Definition || $value->hasErrors()) {
return parent::processValue($value, $isRoot);
}
if (!$this->autoload && !class_exists($class = $value->getClass(), false) && !interface_exists($class, false)) {
return parent::processValue($value, $isRoot);
}
if (ServiceLocator::class === $value->getClass()) {
return parent::processValue($value, $isRoot);
}
if ($constructor = $this->getConstructor($value, false)) {
$this->checkTypeDeclarations($value, $constructor, $value->getArguments());
}
foreach ($value->getMethodCalls() as $methodCall) {
try {
$reflectionMethod = $this->getReflectionMethod($value, $methodCall[0]);
} catch (RuntimeException $e) {
if ($value->getFactory()) {
continue;
}
throw $e;
}
$this->checkTypeDeclarations($value, $reflectionMethod, $methodCall[1]);
}
return parent::processValue($value, $isRoot);
}
/**
* @throws InvalidArgumentException When not enough parameters are defined for the method
*/
private function checkTypeDeclarations(Definition $checkedDefinition, \ReflectionFunctionAbstract $reflectionFunction, array $values): void
{
$numberOfRequiredParameters = $reflectionFunction->getNumberOfRequiredParameters();
if (\count($values) < $numberOfRequiredParameters) {
throw new InvalidArgumentException(sprintf('Invalid definition for service "%s": "%s::%s()" requires %d arguments, %d passed.', $this->currentId, $reflectionFunction->class, $reflectionFunction->name, $numberOfRequiredParameters, \count($values)));
}
$reflectionParameters = $reflectionFunction->getParameters();
$checksCount = min($reflectionFunction->getNumberOfParameters(), \count($values));
for ($i = 0; $i < $checksCount; ++$i) {
if (!$reflectionParameters[$i]->hasType() || $reflectionParameters[$i]->isVariadic()) {
continue;
}
$this->checkType($checkedDefinition, $values[$i], $reflectionParameters[$i]);
}
if ($reflectionFunction->isVariadic() && ($lastParameter = end($reflectionParameters))->hasType()) {
$variadicParameters = \array_slice($values, $lastParameter->getPosition());
foreach ($variadicParameters as $variadicParameter) {
$this->checkType($checkedDefinition, $variadicParameter, $lastParameter);
}
}
}
/**
* @throws InvalidParameterTypeException When a parameter is not compatible with the declared type
*/
private function checkType(Definition $checkedDefinition, $value, \ReflectionParameter $parameter): void
{
$type = $parameter->getType()->getName();
if ($value instanceof Reference) {
if (!$this->container->has($value = (string) $value)) {
return;
}
if ('service_container' === $value && is_a($type, Container::class, true)) {
return;
}
$value = $this->container->findDefinition($value);
}
if ('self' === $type) {
$type = $parameter->getDeclaringClass()->getName();
}
if ('static' === $type) {
$type = $checkedDefinition->getClass();
}
if ($value instanceof Definition) {
$class = $value->getClass();
if (!$class || (!$this->autoload && !class_exists($class, false) && !interface_exists($class, false))) {
return;
}
if ('callable' === $type && method_exists($class, '__invoke')) {
return;
}
if ('iterable' === $type && is_subclass_of($class, 'Traversable')) {
return;
}
if (is_a($class, $type, true)) {
return;
}
throw new InvalidParameterTypeException($this->currentId, $class, $parameter);
}
if ($value instanceof Parameter) {
$value = $this->container->getParameter($value);
} elseif (\is_string($value) && '%' === ($value[0] ?? '') && preg_match('/^%([^%]+)%$/', $value, $match)) {
$value = $this->container->getParameter($match[1]);
}
if (null === $value && $parameter->allowsNull()) {
return;
}
if (\in_array($type, self::SCALAR_TYPES, true) && is_scalar($value)) {
return;
}
if ('callable' === $type && \is_array($value) && isset($value[0]) && ($value[0] instanceof Reference || $value[0] instanceof Definition)) {
return;
}
if ('iterable' === $type && (\is_array($value) || $value instanceof \Traversable || $value instanceof IteratorArgument)) {
return;
}
if ('Traversable' === $type && ($value instanceof \Traversable || $value instanceof IteratorArgument)) {
return;
}
$checkFunction = sprintf('is_%s', $parameter->getType()->getName());
if (!$parameter->getType()->isBuiltin() || !$checkFunction($value)) {
throw new InvalidParameterTypeException($this->currentId, \is_object($value) ? \get_class($value) : \gettype($value), $parameter);
}
}
}