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