|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Rules\Functions; |
| 4 | + |
| 5 | +use PhpParser\Node; |
| 6 | +use PhpParser\Node\Expr\FuncCall; |
| 7 | +use PhpParser\Node\Name; |
| 8 | +use PHPStan\Analyser\ArgumentsNormalizer; |
| 9 | +use PHPStan\Analyser\Scope; |
| 10 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 11 | +use PHPStan\Reflection\ReflectionProvider; |
| 12 | +use PHPStan\Rules\Rule; |
| 13 | +use PHPStan\Rules\RuleErrorBuilder; |
| 14 | +use PHPStan\Type\Type; |
| 15 | +use PHPStan\Type\VerbosityLevel; |
| 16 | +use function count; |
| 17 | +use function sprintf; |
| 18 | + |
| 19 | +/** |
| 20 | + * @implements Rule<FuncCall> |
| 21 | + */ |
| 22 | +class ArrayFilterStrictRule implements Rule |
| 23 | +{ |
| 24 | + |
| 25 | + /** @var ReflectionProvider */ |
| 26 | + private $reflectionProvider; |
| 27 | + |
| 28 | + /** @var bool */ |
| 29 | + private $treatPhpDocTypesAsCertain; |
| 30 | + |
| 31 | + /** @var bool */ |
| 32 | + private $checkNullables; |
| 33 | + |
| 34 | + public function __construct( |
| 35 | + ReflectionProvider $reflectionProvider, |
| 36 | + bool $treatPhpDocTypesAsCertain, |
| 37 | + bool $checkNullables |
| 38 | + ) |
| 39 | + { |
| 40 | + $this->reflectionProvider = $reflectionProvider; |
| 41 | + $this->treatPhpDocTypesAsCertain = $treatPhpDocTypesAsCertain; |
| 42 | + $this->checkNullables = $checkNullables; |
| 43 | + } |
| 44 | + |
| 45 | + public function getNodeType(): string |
| 46 | + { |
| 47 | + return FuncCall::class; |
| 48 | + } |
| 49 | + |
| 50 | + public function processNode(Node $node, Scope $scope): array |
| 51 | + { |
| 52 | + if (!$node->name instanceof Name) { |
| 53 | + return []; |
| 54 | + } |
| 55 | + |
| 56 | + if (!$this->reflectionProvider->hasFunction($node->name, $scope)) { |
| 57 | + return []; |
| 58 | + } |
| 59 | + |
| 60 | + $functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); |
| 61 | + |
| 62 | + if ($functionReflection->getName() !== 'array_filter') { |
| 63 | + return []; |
| 64 | + } |
| 65 | + |
| 66 | + $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
| 67 | + $scope, |
| 68 | + $node->getArgs(), |
| 69 | + $functionReflection->getVariants(), |
| 70 | + $functionReflection->getNamedArgumentsVariants() |
| 71 | + ); |
| 72 | + |
| 73 | + $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); |
| 74 | + |
| 75 | + if ($normalizedFuncCall === null) { |
| 76 | + return []; |
| 77 | + } |
| 78 | + |
| 79 | + $args = $normalizedFuncCall->getArgs(); |
| 80 | + if (count($args) === 0) { |
| 81 | + return []; |
| 82 | + } |
| 83 | + |
| 84 | + if (count($args) === 1) { |
| 85 | + return [RuleErrorBuilder::message('Call to function array_filter() requires parameter #2 to be passed to avoid loose comparison semantics.')->build()]; |
| 86 | + } |
| 87 | + |
| 88 | + $nativeCallbackType = $scope->getNativeType($args[1]->value); |
| 89 | + |
| 90 | + if ($this->treatPhpDocTypesAsCertain) { |
| 91 | + $callbackType = $scope->getType($args[1]->value); |
| 92 | + } else { |
| 93 | + $callbackType = $nativeCallbackType; |
| 94 | + } |
| 95 | + |
| 96 | + if ($this->isCallbackTypeNull($callbackType)) { |
| 97 | + $message = 'Parameter #2 of array_filter() cannot be null to avoid loose comparison semantics (%s given).'; |
| 98 | + $errorBuilder = RuleErrorBuilder::message(sprintf( |
| 99 | + $message, |
| 100 | + $callbackType->describe(VerbosityLevel::typeOnly()) |
| 101 | + )); |
| 102 | + |
| 103 | + if (!$this->isCallbackTypeNull($nativeCallbackType) && $this->treatPhpDocTypesAsCertain) { |
| 104 | + $errorBuilder->tip('Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.'); |
| 105 | + } |
| 106 | + |
| 107 | + return [$errorBuilder->build()]; |
| 108 | + } |
| 109 | + |
| 110 | + return []; |
| 111 | + } |
| 112 | + |
| 113 | + private function isCallbackTypeNull(Type $callbackType): bool |
| 114 | + { |
| 115 | + if ($callbackType->isNull()->yes()) { |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + if ($callbackType->isNull()->no()) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + return $this->checkNullables; |
| 124 | + } |
| 125 | + |
| 126 | +} |
0 commit comments