|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Symfony; |
| 4 | + |
| 5 | +use InvalidArgumentException; |
| 6 | +use PhpParser\Node; |
| 7 | +use PhpParser\Node\Expr\MethodCall; |
| 8 | +use PhpParser\PrettyPrinter\Standard; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\ShouldNotHappenException; |
| 12 | +use PHPStan\Symfony\ConsoleApplicationResolver; |
| 13 | +use PHPStan\Type\ObjectType; |
| 14 | +use PHPStan\Type\Symfony\Helper; |
| 15 | +use PHPStan\Type\TypeUtils; |
| 16 | +use function count; |
| 17 | +use function sprintf; |
| 18 | + |
| 19 | +final class UndefinedArgumentRule implements Rule |
| 20 | +{ |
| 21 | + |
| 22 | + /** @var \PHPStan\Symfony\ConsoleApplicationResolver */ |
| 23 | + private $consoleApplicationResolver; |
| 24 | + |
| 25 | + /** @var \PhpParser\PrettyPrinter\Standard */ |
| 26 | + private $printer; |
| 27 | + |
| 28 | + public function __construct(ConsoleApplicationResolver $consoleApplicationResolver, Standard $printer) |
| 29 | + { |
| 30 | + $this->consoleApplicationResolver = $consoleApplicationResolver; |
| 31 | + $this->printer = $printer; |
| 32 | + } |
| 33 | + |
| 34 | + public function getNodeType(): string |
| 35 | + { |
| 36 | + return MethodCall::class; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @param \PhpParser\Node $node |
| 41 | + * @param \PHPStan\Analyser\Scope $scope |
| 42 | + * @return (string|\PHPStan\Rules\RuleError)[] errors |
| 43 | + */ |
| 44 | + public function processNode(Node $node, Scope $scope): array |
| 45 | + { |
| 46 | + if (!$node instanceof MethodCall) { |
| 47 | + throw new ShouldNotHappenException(); |
| 48 | + }; |
| 49 | + |
| 50 | + $classReflection = $scope->getClassReflection(); |
| 51 | + if ($classReflection === null) { |
| 52 | + throw new ShouldNotHappenException(); |
| 53 | + } |
| 54 | + |
| 55 | + if (!(new ObjectType('Symfony\Component\Console\Command\Command'))->isSuperTypeOf(new ObjectType($classReflection->getName()))->yes()) { |
| 56 | + return []; |
| 57 | + } |
| 58 | + if (!(new ObjectType('Symfony\Component\Console\Input\InputInterface'))->isSuperTypeOf($scope->getType($node->var))->yes()) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + if (!$node->name instanceof Node\Identifier || $node->name->name !== 'getArgument') { |
| 62 | + return []; |
| 63 | + } |
| 64 | + if (!isset($node->args[0])) { |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + $argType = $scope->getType($node->args[0]->value); |
| 69 | + $argStrings = TypeUtils::getConstantStrings($argType); |
| 70 | + if (count($argStrings) !== 1) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + $argName = $argStrings[0]->getValue(); |
| 74 | + |
| 75 | + $errors = []; |
| 76 | + foreach ($this->consoleApplicationResolver->findCommands($classReflection) as $name => $command) { |
| 77 | + try { |
| 78 | + $command->getDefinition()->getArgument($argName); |
| 79 | + } catch (InvalidArgumentException $e) { |
| 80 | + if ($scope->getType(Helper::createMarkerNode($node->var, $argType, $this->printer))->equals($argType)) { |
| 81 | + continue; |
| 82 | + } |
| 83 | + $errors[] = sprintf('Command "%s" does not define argument "%s".', $name, $argName); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return $errors; |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments