|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Symfony; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\MethodCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Rules\Rule; |
| 9 | +use PHPStan\ShouldNotHappenException; |
| 10 | +use PHPStan\Type\ArrayType; |
| 11 | +use PHPStan\Type\Constant\ConstantBooleanType; |
| 12 | +use PHPStan\Type\Constant\ConstantIntegerType; |
| 13 | +use PHPStan\Type\IntegerType; |
| 14 | +use PHPStan\Type\NullType; |
| 15 | +use PHPStan\Type\ObjectType; |
| 16 | +use PHPStan\Type\StringType; |
| 17 | +use PHPStan\Type\TypeCombinator; |
| 18 | +use PHPStan\Type\TypeUtils; |
| 19 | +use PHPStan\Type\UnionType; |
| 20 | +use PHPStan\Type\VerbosityLevel; |
| 21 | +use function sprintf; |
| 22 | + |
| 23 | +final class InvalidOptionDefaultValueRule implements Rule |
| 24 | +{ |
| 25 | + |
| 26 | + public function getNodeType(): string |
| 27 | + { |
| 28 | + return MethodCall::class; |
| 29 | + } |
| 30 | + |
| 31 | + /** |
| 32 | + * @param \PhpParser\Node $node |
| 33 | + * @param \PHPStan\Analyser\Scope $scope |
| 34 | + * @return (string|\PHPStan\Rules\RuleError)[] errors |
| 35 | + */ |
| 36 | + public function processNode(Node $node, Scope $scope): array |
| 37 | + { |
| 38 | + if (!$node instanceof MethodCall) { |
| 39 | + throw new ShouldNotHappenException(); |
| 40 | + }; |
| 41 | + |
| 42 | + if (!(new ObjectType('Symfony\Component\Console\Command\Command'))->isSuperTypeOf($scope->getType($node->var))->yes()) { |
| 43 | + return []; |
| 44 | + } |
| 45 | + if (!$node->name instanceof Node\Identifier || $node->name->name !== 'addOption') { |
| 46 | + return []; |
| 47 | + } |
| 48 | + if (!isset($node->args[4])) { |
| 49 | + return []; |
| 50 | + } |
| 51 | + |
| 52 | + $modeType = isset($node->args[2]) ? $scope->getType($node->args[2]->value) : new NullType(); |
| 53 | + if ($modeType instanceof NullType) { |
| 54 | + $modeType = new ConstantIntegerType(1); // InputOption::VALUE_NONE |
| 55 | + } |
| 56 | + $modeTypes = TypeUtils::getConstantScalars($modeType); |
| 57 | + if (count($modeTypes) !== 1) { |
| 58 | + return []; |
| 59 | + } |
| 60 | + if (!$modeTypes[0] instanceof ConstantIntegerType) { |
| 61 | + return []; |
| 62 | + } |
| 63 | + $mode = $modeTypes[0]->getValue(); |
| 64 | + |
| 65 | + $defaultType = $scope->getType($node->args[4]->value); |
| 66 | + |
| 67 | + // not an array |
| 68 | + if (($mode & 8) !== 8) { |
| 69 | + $checkType = new UnionType([new StringType(), new IntegerType(), new NullType()]); |
| 70 | + if (($mode & 4) === 4) { // https://symfony.com/doc/current/console/input.html#options-with-optional-arguments |
| 71 | + $checkType = TypeCombinator::union($checkType, new ConstantBooleanType(false)); |
| 72 | + } |
| 73 | + if (!$checkType->isSuperTypeOf($defaultType)->yes()) { |
| 74 | + return [sprintf('Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects %s, %s given.', $checkType->describe(VerbosityLevel::typeOnly()), $defaultType->describe(VerbosityLevel::typeOnly()))]; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + // is array |
| 79 | + if (($mode & 8) === 8 && !(new UnionType([new ArrayType(new IntegerType(), new StringType()), new NullType()]))->isSuperTypeOf($defaultType)->yes()) { |
| 80 | + return [sprintf('Parameter #5 $default of method Symfony\Component\Console\Command\Command::addOption() expects array<int, string>|null, %s given.', $defaultType->describe(VerbosityLevel::typeOnly()))]; |
| 81 | + } |
| 82 | + |
| 83 | + return []; |
| 84 | + } |
| 85 | + |
| 86 | +} |
0 commit comments