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