44
55use PhpParser \Node ;
66use PhpParser \Node \Expr \FuncCall ;
7+ use PHPStan \Analyser \ArgumentsNormalizer ;
78use PHPStan \Analyser \Scope ;
89use PHPStan \DependencyInjection \AutowiredParameter ;
910use PHPStan \DependencyInjection \Conditional ;
1011use PHPStan \DependencyInjection \RegisteredRule ;
1112use PHPStan \Php \PhpVersions ;
1213use PHPStan \Reflection \InitializerExprTypeResolver ;
14+ use PHPStan \Reflection \ParametersAcceptorSelector ;
1315use PHPStan \Reflection \ReflectionProvider ;
1416use PHPStan \Rules \IdentifierRuleError ;
1517use PHPStan \Rules \Rule ;
2325use function array_unique ;
2426use function array_values ;
2527use function count ;
26- use function max ;
2728use function sprintf ;
2829use 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 */
0 commit comments