|
| 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 PHPStan\Analyser\ArgumentsNormalizer; |
| 8 | +use PHPStan\Analyser\Scope; |
| 9 | +use PHPStan\Reflection\ParametersAcceptorSelector; |
| 10 | +use PHPStan\Reflection\ReflectionProvider; |
| 11 | +use PHPStan\Rules\Rule; |
| 12 | +use PHPStan\Rules\RuleErrorBuilder; |
| 13 | +use PHPStan\Type\Accessory\AccessoryArrayListType; |
| 14 | +use PHPStan\Type\VerbosityLevel; |
| 15 | +use function count; |
| 16 | +use function sprintf; |
| 17 | +use function strtolower; |
| 18 | + |
| 19 | +/** |
| 20 | + * @implements Rule<Node\Expr\FuncCall> |
| 21 | + */ |
| 22 | +class ArrayValuesRule implements Rule |
| 23 | +{ |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private readonly ReflectionProvider $reflectionProvider, |
| 27 | + private readonly bool $treatPhpDocTypesAsCertain, |
| 28 | + ) |
| 29 | + { |
| 30 | + } |
| 31 | + |
| 32 | + public function getNodeType(): string |
| 33 | + { |
| 34 | + return FuncCall::class; |
| 35 | + } |
| 36 | + |
| 37 | + public function processNode(Node $node, Scope $scope): array |
| 38 | + { |
| 39 | + if (!($node->name instanceof Node\Name)) { |
| 40 | + return []; |
| 41 | + } |
| 42 | + |
| 43 | + if (AccessoryArrayListType::isListTypeEnabled() === false) { |
| 44 | + return []; |
| 45 | + } |
| 46 | + |
| 47 | + $functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope); |
| 48 | + |
| 49 | + if ($functionName === null || strtolower($functionName) !== 'array_values') { |
| 50 | + return []; |
| 51 | + } |
| 52 | + |
| 53 | + $functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); |
| 54 | + |
| 55 | + $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( |
| 56 | + $scope, |
| 57 | + $node->getArgs(), |
| 58 | + $functionReflection->getVariants(), |
| 59 | + null, |
| 60 | + ); |
| 61 | + |
| 62 | + $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); |
| 63 | + |
| 64 | + if ($normalizedFuncCall === null) { |
| 65 | + return []; |
| 66 | + } |
| 67 | + |
| 68 | + $args = $normalizedFuncCall->getArgs(); |
| 69 | + |
| 70 | + if (count($args) !== 1) { |
| 71 | + return []; |
| 72 | + } |
| 73 | + |
| 74 | + if ($this->treatPhpDocTypesAsCertain === true) { |
| 75 | + $arrayType = $scope->getType($args[0]->value); |
| 76 | + } else { |
| 77 | + $arrayType = $scope->getNativeType($args[0]->value); |
| 78 | + } |
| 79 | + |
| 80 | + if ($arrayType->isIterableAtLeastOnce()->no()) { |
| 81 | + $message = 'Parameter #1 $array (%s) to function array_values is empty, call has no effect.'; |
| 82 | + |
| 83 | + return [ |
| 84 | + RuleErrorBuilder::message(sprintf( |
| 85 | + $message, |
| 86 | + $arrayType->describe(VerbosityLevel::value()), |
| 87 | + ))->build(), |
| 88 | + ]; |
| 89 | + } |
| 90 | + |
| 91 | + if ($arrayType->isList()->yes()) { |
| 92 | + $message = 'Parameter #1 $array (%s) of array_values is already a list, call has no effect.'; |
| 93 | + |
| 94 | + return [ |
| 95 | + RuleErrorBuilder::message(sprintf( |
| 96 | + $message, |
| 97 | + $arrayType->describe(VerbosityLevel::value()), |
| 98 | + ))->build(), |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + return []; |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments