Skip to content

Commit

Permalink
forbidEnumInFunctionArguments: fix false negative for named args (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal authored Sep 13, 2023
1 parent 9ac0ab2 commit 741a1c0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
20 changes: 19 additions & 1 deletion src/Rule/ForbidEnumInFunctionArgumentsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;
Expand Down Expand Up @@ -44,6 +47,13 @@ class ForbidEnumInFunctionArgumentsRule implements Rule
'implode' => [1, self::REASON_IMPLICIT_TO_STRING],
];

private ReflectionProvider $reflectionProvider;

public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}

public function getNodeType(): string
{
return FuncCall::class;
Expand All @@ -69,7 +79,15 @@ public function processNode(Node $node, Scope $scope): array

$wrongArguments = [];

foreach ($node->getArgs() as $position => $argument) {
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $functionReflection->getVariants());
$funcCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); // @phpstan-ignore-line ignore bc promise

if ($funcCall === null) {
$funcCall = $node;
}

foreach ($funcCall->getArgs() as $position => $argument) {
if (!$this->matchesPosition((int) $position, $forbiddenArgumentPosition)) {
continue;
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Rule/ForbidEnumInFunctionArgumentsRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ShipMonk\PHPStan\Rule;

use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\Rule;
use ShipMonk\PHPStan\RuleTestCase;
use const PHP_VERSION_ID;
Expand All @@ -14,7 +15,9 @@ class ForbidEnumInFunctionArgumentsRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new ForbidEnumInFunctionArgumentsRule();
return new ForbidEnumInFunctionArgumentsRule(
self::getContainer()->getByType(ReflectionProvider::class),
);
}

public function test(): void
Expand Down
5 changes: 5 additions & 0 deletions tests/Rule/data/ForbidEnumInFunctionArgumentsRule/code.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,9 @@ public function testUnionAndIntersection($enumWithInterface, $enumOrNotEnum, $ar
sort([$arrayOfEnumsOrNot]); // error: Argument 1 in sort() cannot contain enum as the function causes unexpected results
}

public function testArgumentsNormalization()
{
sort(flags: 0, array: [SomeEnum::Bar]); // error: Argument 1 in sort() cannot contain enum as the function causes unexpected results
}

}

0 comments on commit 741a1c0

Please sign in to comment.