Skip to content

Commit bc64657

Browse files
committed
ImpossibleInArrayHaystackFiniteTypesRule is missing named arguments
1 parent 0f6682d commit bc64657

3 files changed

Lines changed: 57 additions & 12 deletions

File tree

‎src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php‎

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44

55
use PhpParser\Node;
66
use PhpParser\Node\Expr\FuncCall;
7+
use PHPStan\Analyser\ArgumentsNormalizer;
78
use PHPStan\Analyser\Scope;
89
use PHPStan\DependencyInjection\AutowiredParameter;
910
use PHPStan\DependencyInjection\Conditional;
1011
use PHPStan\DependencyInjection\RegisteredRule;
1112
use PHPStan\Php\PhpVersions;
1213
use PHPStan\Reflection\InitializerExprTypeResolver;
14+
use PHPStan\Reflection\ParametersAcceptorSelector;
1315
use PHPStan\Reflection\ReflectionProvider;
1416
use PHPStan\Rules\IdentifierRuleError;
1517
use PHPStan\Rules\Rule;
@@ -23,7 +25,6 @@
2325
use function array_unique;
2426
use function array_values;
2527
use function count;
26-
use function max;
2728
use function sprintf;
2829
use function strtolower;
2930

@@ -51,9 +52,9 @@ final class ImpossibleInArrayHaystackFiniteTypesRule implements Rule
5152

5253
public function __construct(
5354
private InitializerExprTypeResolver $initializerExprTypeResolver,
55+
private ReflectionProvider $reflectionProvider,
5456
#[AutowiredParameter]
5557
private bool $treatPhpDocTypesAsCertain,
56-
private ReflectionProvider $reflectionProvider,
5758
)
5859
{
5960
}
@@ -69,29 +70,48 @@ public function processNode(Node $node, Scope $scope): array
6970
return [];
7071
}
7172

72-
$functionName = strtolower((string) $this->reflectionProvider->resolveFunctionName($node->name, $scope));
73-
if (!array_key_exists($functionName, self::FUNCTIONS)) {
73+
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) {
7474
return [];
7575
}
7676

77-
$needleArg = self::FUNCTIONS[$functionName]['needle'];
78-
$haystackArg = self::FUNCTIONS[$functionName]['haystack'];
77+
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
78+
$functionName = strtolower($functionReflection->getName());
79+
if (!array_key_exists($functionName, self::FUNCTIONS)) {
80+
return [];
81+
}
7982

8083
$args = $node->getArgs();
81-
if (count($args) <= max($needleArg, $haystackArg)) {
84+
$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs(
85+
$scope,
86+
$args,
87+
$functionReflection->getVariants(),
88+
$functionReflection->getNamedArgumentsVariants(),
89+
);
90+
$normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);
91+
if ($normalizedFuncCall === null) {
92+
return [];
93+
}
94+
95+
$normalizedArgs = $normalizedFuncCall->getArgs();
96+
$needlePosition = self::FUNCTIONS[$functionName]['needle'];
97+
$haystackPosition = self::FUNCTIONS[$functionName]['haystack'];
98+
$needleArg = $normalizedArgs[$needlePosition] ?? null;
99+
$haystackArg = $normalizedArgs[$haystackPosition] ?? null;
100+
if ($needleArg === null || $haystackArg === null) {
82101
return [];
83102
}
84103

85-
$needleType = $this->treatPhpDocTypesAsCertain ? $scope->getType($args[$needleArg]->value) : $scope->getNativeType($args[$needleArg]->value);
86-
$haystackType = $this->treatPhpDocTypesAsCertain ? $scope->getType($args[$haystackArg]->value) : $scope->getNativeType($args[$haystackArg]->value);
104+
$needleType = $this->treatPhpDocTypesAsCertain ? $scope->getType($needleArg->value) : $scope->getNativeType($needleArg->value);
105+
$haystackType = $this->treatPhpDocTypesAsCertain ? $scope->getType($haystackArg->value) : $scope->getNativeType($haystackArg->value);
87106

88107
$constantArrays = $haystackType->getConstantArrays();
89108
if (count($constantArrays) === 0) {
90109
return [];
91110
}
92111

93-
$isStrict = count($args) >= 3
94-
? ($this->treatPhpDocTypesAsCertain ? $scope->getType($args[2]->value) : $scope->getNativeType($args[2]->value))->isTrue()
112+
$strictArg = $normalizedArgs[2] ?? null;
113+
$isStrict = $strictArg !== null
114+
? ($this->treatPhpDocTypesAsCertain ? $scope->getType($strictArg->value) : $scope->getNativeType($strictArg->value))->isTrue()
95115
: TrinaryLogic::createNo();
96116

97117
/** @var array<string, array{Type, list<string>, bool}> $deadValueTypes */

‎tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace PHPStan\Rules\Comparison;
44

55
use PHPStan\Reflection\InitializerExprTypeResolver;
6+
use PHPStan\Reflection\ReflectionProvider;
67
use PHPStan\Rules\Rule;
78
use PHPStan\Testing\RuleTestCase;
89
use PHPUnit\Framework\Attributes\RequiresPhp;
@@ -17,8 +18,8 @@ protected function getRule(): Rule
1718
{
1819
return new ImpossibleInArrayHaystackFiniteTypesRule(
1920
self::getContainer()->getByType(InitializerExprTypeResolver::class),
21+
self::getContainer()->getByType(ReflectionProvider::class),
2022
true,
21-
self::createReflectionProvider(),
2223
);
2324
}
2425

@@ -42,6 +43,18 @@ public function testRule(): void
4243
'Value ImpossibleInArrayFiniteTypes\Foo::ONE in the haystack passed to array_keys() can never be identical to the needle type int.',
4344
38,
4445
],
46+
[
47+
'Value ImpossibleInArrayFiniteTypes\Foo::ONE in the haystack passed to in_array() can never be identical to the needle type int.',
48+
278,
49+
],
50+
[
51+
'Value ImpossibleInArrayFiniteTypes\Foo::ONE in the haystack passed to array_search() can never be identical to the needle type int.',
52+
279,
53+
],
54+
[
55+
'Value ImpossibleInArrayFiniteTypes\Foo::ONE in the haystack passed to array_keys() can never be identical to the needle type int.',
56+
280,
57+
],
4558
[
4659
'Value \'installed\' in the haystack passed to in_array() can never be identical to the needle type mixed.',
4760
99,

‎tests/PHPStan/Rules/Comparison/data/impossible-in-array-finite-types.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,15 @@ function noErrorIntegerRangeNeedle(int $i): void
272272
echo 'yes';
273273
}
274274
}
275+
276+
function reportedNamedInArray(int $i): void
277+
{
278+
in_array(haystack: [Foo::ONE, 1, 2], strict: true, needle: $i);
279+
array_search(haystack: [Foo::ONE, 1, 2], strict: true, needle: $i);
280+
array_keys(strict: true, filter_value: $i, array: [Foo::ONE, 1, 2]);
281+
}
282+
283+
function noErrorNamedArrayKeysWithoutFilterValue(): void
284+
{
285+
array_keys(array: [Foo::ONE], strict: true);
286+
}

0 commit comments

Comments
 (0)