Skip to content

Commit 527d373

Browse files
authored
Fix ImpossibleInArrayHaystackFiniteTypesRule local function confusion (#7041)
1 parent f8a065a commit 527d373

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

‎src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use PHPStan\DependencyInjection\RegisteredRule;
1111
use PHPStan\Php\PhpVersions;
1212
use PHPStan\Reflection\InitializerExprTypeResolver;
13+
use PHPStan\Reflection\ReflectionProvider;
1314
use PHPStan\Rules\IdentifierRuleError;
1415
use PHPStan\Rules\Rule;
1516
use PHPStan\Rules\RuleErrorBuilder;
@@ -52,6 +53,7 @@ public function __construct(
5253
private InitializerExprTypeResolver $initializerExprTypeResolver,
5354
#[AutowiredParameter]
5455
private bool $treatPhpDocTypesAsCertain,
56+
private ReflectionProvider $reflectionProvider,
5557
)
5658
{
5759
}
@@ -67,7 +69,7 @@ public function processNode(Node $node, Scope $scope): array
6769
return [];
6870
}
6971

70-
$functionName = strtolower((string) $node->name);
72+
$functionName = strtolower((string) $this->reflectionProvider->resolveFunctionName($node->name, $scope));
7173
if (!array_key_exists($functionName, self::FUNCTIONS)) {
7274
return [];
7375
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protected function getRule(): Rule
1818
return new ImpossibleInArrayHaystackFiniteTypesRule(
1919
self::getContainer()->getByType(InitializerExprTypeResolver::class),
2020
true,
21+
self::createReflectionProvider(),
2122
);
2223
}
2324

@@ -73,4 +74,10 @@ public function testRule(): void
7374
]);
7475
}
7576

77+
#[RequiresPhp('>= 8.1.0')]
78+
public function testNamespacedInArrayRule(): void
79+
{
80+
$this->analyse([__DIR__ . '/data/impossible-in-array-in-namespace.php'], []);
81+
}
82+
7683
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php // lint >= 8.1
2+
3+
namespace NamespacedFunction;
4+
5+
function in_array($a, $b) {
6+
echo "$a $b";
7+
}
8+
9+
enum Foo
10+
{
11+
12+
case ONE;
13+
case TWO;
14+
case THREE;
15+
16+
}
17+
18+
function reportedFiniteValueStrict(int $i): void
19+
{
20+
// should not error, as the in_array is namespaced and not the global one
21+
if (in_array($i, [Foo::ONE, 1, 2], true)) {
22+
echo 'yes';
23+
}
24+
}

0 commit comments

Comments
 (0)