Skip to content

Commit

Permalink
ArrayFilterRule - be consistent about named arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 15, 2024
1 parent f5b198c commit cfedc7b
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/Rules/Functions/ArrayFilterRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
Expand Down Expand Up @@ -38,13 +40,28 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$functionName = $this->reflectionProvider->resolveFunctionName($node->name, $scope);
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
return [];
}

$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
if ($functionReflection->getName() !== 'array_filter') {
return [];
}

$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
$scope,
$node->getArgs(),
$functionReflection->getVariants(),
$functionReflection->getNamedArgumentsVariants(),
);

if ($functionName === null || strtolower($functionName) !== 'array_filter') {
$normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
if ($normalizedFuncCall === null) {
return [];
}

$args = $node->getArgs();
$args = $normalizedFuncCall->getArgs();
if (count($args) !== 1) {
return [];
}
Expand Down

0 comments on commit cfedc7b

Please sign in to comment.