|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Comparison; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\FuncCall; |
| 7 | +use PHPStan\Analyser\Scope; |
| 8 | +use PHPStan\Reflection\InitializerExprTypeResolver; |
| 9 | +use PHPStan\Rules\IdentifierRuleError; |
| 10 | +use PHPStan\Rules\Rule; |
| 11 | +use PHPStan\Rules\RuleErrorBuilder; |
| 12 | +use PHPStan\TrinaryLogic; |
| 13 | +use PHPStan\Type\Type; |
| 14 | +use PHPStan\Type\VerbosityLevel; |
| 15 | +use function array_key_exists; |
| 16 | +use function count; |
| 17 | +use function max; |
| 18 | +use function sprintf; |
| 19 | +use function strtolower; |
| 20 | + |
| 21 | +/** |
| 22 | + * Reports finite-typed values in a constant array haystack passed to in_array(), |
| 23 | + * array_search() or array_keys() that can never be the needle, using |
| 24 | + * Type::getConstantArrays() and Type::getFiniteTypes() instead of inspecting the |
| 25 | + * AST of the array literal. |
| 26 | + * |
| 27 | + * @implements Rule<FuncCall> |
| 28 | + */ |
| 29 | +final class ImpossibleInArrayHaystackFiniteTypesRule implements Rule |
| 30 | +{ |
| 31 | + |
| 32 | + /** Argument positions of the needle and the haystack per supported function. */ |
| 33 | + private const FUNCTIONS = [ |
| 34 | + 'in_array' => ['needle' => 0, 'haystack' => 1], |
| 35 | + 'array_search' => ['needle' => 0, 'haystack' => 1], |
| 36 | + 'array_keys' => ['needle' => 1, 'haystack' => 0], |
| 37 | + ]; |
| 38 | + |
| 39 | + public function __construct( |
| 40 | + private InitializerExprTypeResolver $initializerExprTypeResolver, |
| 41 | + private bool $treatPhpDocTypesAsCertain, |
| 42 | + ) |
| 43 | + { |
| 44 | + } |
| 45 | + |
| 46 | + public function getNodeType(): string |
| 47 | + { |
| 48 | + return FuncCall::class; |
| 49 | + } |
| 50 | + |
| 51 | + public function processNode(Node $node, Scope $scope): array |
| 52 | + { |
| 53 | + if (!$node->name instanceof Node\Name) { |
| 54 | + return []; |
| 55 | + } |
| 56 | + |
| 57 | + $functionName = strtolower((string) $node->name); |
| 58 | + if (!array_key_exists($functionName, self::FUNCTIONS)) { |
| 59 | + return []; |
| 60 | + } |
| 61 | + |
| 62 | + $needleArg = self::FUNCTIONS[$functionName]['needle']; |
| 63 | + $haystackArg = self::FUNCTIONS[$functionName]['haystack']; |
| 64 | + |
| 65 | + $args = $node->getArgs(); |
| 66 | + if (count($args) <= max($needleArg, $haystackArg)) { |
| 67 | + return []; |
| 68 | + } |
| 69 | + |
| 70 | + $needleType = $this->treatPhpDocTypesAsCertain ? $scope->getType($args[$needleArg]->value) : $scope->getNativeType($args[$needleArg]->value); |
| 71 | + $haystackType = $this->treatPhpDocTypesAsCertain ? $scope->getType($args[$haystackArg]->value) : $scope->getNativeType($args[$haystackArg]->value); |
| 72 | + |
| 73 | + $constantArrays = $haystackType->getConstantArrays(); |
| 74 | + if (count($constantArrays) === 0) { |
| 75 | + return []; |
| 76 | + } |
| 77 | + |
| 78 | + $isStrict = count($args) >= 3 |
| 79 | + ? ($this->treatPhpDocTypesAsCertain ? $scope->getType($args[2]->value) : $scope->getNativeType($args[2]->value))->isTrue() |
| 80 | + : TrinaryLogic::createNo(); |
| 81 | + |
| 82 | + $deadValueTypes = []; |
| 83 | + $anyPossibleMatch = false; |
| 84 | + foreach ($constantArrays as $constantArray) { |
| 85 | + foreach ($constantArray->getValueTypes() as $valueType) { |
| 86 | + if (!$this->canNeverMatch($needleType, $valueType, $isStrict)) { |
| 87 | + $anyPossibleMatch = true; |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + if (count($valueType->getFiniteTypes()) === 0) { |
| 92 | + continue; |
| 93 | + } |
| 94 | + |
| 95 | + $deadValueTypes[$valueType->describe(VerbosityLevel::precise())] = $valueType; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // When no haystack value can ever match, the whole in_array() call is |
| 100 | + // impossible and reported by ImpossibleCheckTypeFunctionCallRule instead. |
| 101 | + // array_search() has no such companion rule, so keep reporting there. |
| 102 | + if (!$anyPossibleMatch && $functionName === 'in_array') { |
| 103 | + return []; |
| 104 | + } |
| 105 | + |
| 106 | + $verb = $isStrict->no() ? 'equal to' : 'identical to'; |
| 107 | + |
| 108 | + $errors = []; |
| 109 | + foreach ($deadValueTypes as $valueType) { |
| 110 | + $errors[] = $this->buildError($valueType, $needleType, $functionName, $verb); |
| 111 | + } |
| 112 | + |
| 113 | + return $errors; |
| 114 | + } |
| 115 | + |
| 116 | + private function canNeverMatch(Type $needleType, Type $valueType, TrinaryLogic $isStrict): bool |
| 117 | + { |
| 118 | + if ($isStrict->yes()) { |
| 119 | + return $this->initializerExprTypeResolver->resolveIdenticalType($needleType, $valueType)->type->isFalse()->yes(); |
| 120 | + } |
| 121 | + |
| 122 | + if ($isStrict->no()) { |
| 123 | + return $this->initializerExprTypeResolver->resolveEqualType($needleType, $valueType)->type->isFalse()->yes(); |
| 124 | + } |
| 125 | + |
| 126 | + return $this->initializerExprTypeResolver->resolveIdenticalType($needleType, $valueType)->type->isFalse()->yes() |
| 127 | + && $this->initializerExprTypeResolver->resolveEqualType($needleType, $valueType)->type->isFalse()->yes(); |
| 128 | + } |
| 129 | + |
| 130 | + private function buildError(Type $valueType, Type $needleType, string $functionName, string $verb): IdentifierRuleError |
| 131 | + { |
| 132 | + return RuleErrorBuilder::message(sprintf( |
| 133 | + 'Value %s in the haystack passed to %s() can never be %s the needle type %s.', |
| 134 | + $valueType->describe(VerbosityLevel::precise()), |
| 135 | + $functionName, |
| 136 | + $verb, |
| 137 | + $needleType->describe(VerbosityLevel::precise()), |
| 138 | + ))->identifier('function.impossibleHaystackValue')->build(); |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments