From 693f39f773996e4964a2dbb6d5f86a1c5c0598bb Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 11 Nov 2026 19:54:08 +0100 Subject: [PATCH 1/8] GetTemplateTypeRule is missing named arguments --- src/Rules/Api/GetTemplateTypeRule.php | 11 +++++++++-- tests/PHPStan/Rules/Api/GetTemplateTypeRuleTest.php | 4 ++++ tests/PHPStan/Rules/Api/data/get-template-type.php | 7 ++++++- 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Rules/Api/GetTemplateTypeRule.php b/src/Rules/Api/GetTemplateTypeRule.php index ee55ed567c5..53bce2cd300 100644 --- a/src/Rules/Api/GetTemplateTypeRule.php +++ b/src/Rules/Api/GetTemplateTypeRule.php @@ -4,6 +4,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; +use PHPStan\Analyser\ArgumentsNormalizer; use PHPStan\Analyser\Scope; use PHPStan\DependencyInjection\RegisteredRule; use PHPStan\Reflection\ReflectionProvider; @@ -43,6 +44,12 @@ public function processNode(Node $node, Scope $scope): array return []; } + $classArg = ArgumentsNormalizer::getArgByPositionOrName($args, 0, 'ancestorClassName'); + $templateArg = ArgumentsNormalizer::getArgByPositionOrName($args, 1, 'templateTypeName'); + if ($classArg === null || $templateArg === null) { + return []; + } + $calledOnType = $scope->getType($node->var); $methodReflection = $scope->getMethodReflection($calledOnType, $node->name->toString()); if ($methodReflection === null) { @@ -53,8 +60,8 @@ public function processNode(Node $node, Scope $scope): array return []; } - $classType = $scope->getType($args[0]->value); - $templateType = $scope->getType($args[1]->value); + $classType = $scope->getType($classArg->value); + $templateType = $scope->getType($templateArg->value); $errors = []; foreach ($classType->getConstantStrings() as $classNameType) { if (!$this->reflectionProvider->hasClass($classNameType->getValue())) { diff --git a/tests/PHPStan/Rules/Api/GetTemplateTypeRuleTest.php b/tests/PHPStan/Rules/Api/GetTemplateTypeRuleTest.php index fab3cc8528a..6ffebb58644 100644 --- a/tests/PHPStan/Rules/Api/GetTemplateTypeRuleTest.php +++ b/tests/PHPStan/Rules/Api/GetTemplateTypeRuleTest.php @@ -27,6 +27,10 @@ public function testRule(): void 'Call to PHPStan\Type\ObjectType::getTemplateType() references unknown template type TSendd on class Generator.', 21, ], + [ + 'Call to PHPStan\Type\Type::getTemplateType() references unknown template type TSendd on class Generator.', + 26, + ], ]); } diff --git a/tests/PHPStan/Rules/Api/data/get-template-type.php b/tests/PHPStan/Rules/Api/data/get-template-type.php index 5cb59086b8c..e68ad5d8133 100644 --- a/tests/PHPStan/Rules/Api/data/get-template-type.php +++ b/tests/PHPStan/Rules/Api/data/get-template-type.php @@ -1,4 +1,4 @@ -= 8.0 namespace GetTemplateType; @@ -21,4 +21,9 @@ public function doBar(ObjectType $type): void $type->getTemplateType(Generator::class, 'TSendd'); } + public function doNamed(Type $type): void + { + $type->getTemplateType(templateTypeName: 'TSendd', ancestorClassName: Generator::class); + } + } From 0f6682d6b6363badaff3a9951527a7ac65d506f3 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 11 Nov 2026 19:59:34 +0100 Subject: [PATCH 2/8] NonexistentOffsetInArrayDimFetchRule is missing named arguments --- .../NonexistentOffsetInArrayDimFetchRule.php | 87 ++++++++++--------- ...rray-dim-fetch-on-array-key-first-last.php | 10 ++- 2 files changed, 56 insertions(+), 41 deletions(-) diff --git a/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php b/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php index 262a11554e0..8255cdd1ad5 100644 --- a/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php +++ b/src/Rules/Arrays/NonexistentOffsetInArrayDimFetchRule.php @@ -5,6 +5,7 @@ use PhpParser\Node; use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Variable; +use PHPStan\Analyser\ArgumentsNormalizer; use PHPStan\Analyser\NullsafeOperatorHelper; use PHPStan\Analyser\Scope; use PHPStan\DependencyInjection\AutowiredParameter; @@ -117,17 +118,20 @@ public function processNode(Node $node, Scope $scope): array && in_array($node->dim->name->toLowerString(), ['array_key_first', 'array_key_last'], true) && count($node->dim->getArgs()) >= 1 ) { - $arrayArg = $node->dim->getArgs()[0]->value; - $arrayType = $scope->getType($arrayArg); - if ( - $arrayArg instanceof Node\Expr\Variable - && $node->var instanceof Node\Expr\Variable - && is_string($arrayArg->name) - && $arrayArg->name === $node->var->name - && $arrayType->isArray()->yes() - && $arrayType->isIterableAtLeastOnce()->yes() - ) { - return []; + $arrayArg = ArgumentsNormalizer::getArgByPositionOrName($node->dim->getArgs(), 0, 'array'); + if ($arrayArg !== null) { + $arrayArg = $arrayArg->value; + $arrayType = $scope->getType($arrayArg); + if ( + $arrayArg instanceof Node\Expr\Variable + && $node->var instanceof Node\Expr\Variable + && is_string($arrayArg->name) + && $arrayArg->name === $node->var->name + && $arrayType->isArray()->yes() + && $arrayType->isIterableAtLeastOnce()->yes() + ) { + return []; + } } } @@ -137,24 +141,24 @@ public function processNode(Node $node, Scope $scope): array && $node->dim->name->toLowerString() === 'array_rand' && count($node->dim->getArgs()) >= 1 ) { - $numArg = null; - $arrayArg = $node->dim->getArgs()[0]->value; - if (count($node->dim->getArgs()) > 1) { - $numArg = $node->dim->getArgs()[1]->value; - } - $one = new ConstantIntegerType(1); - $arrayType = $scope->getType($arrayArg); - - if ( - $arrayArg instanceof Node\Expr\Variable - && $node->var instanceof Node\Expr\Variable - && is_string($arrayArg->name) - && $arrayArg->name === $node->var->name - && $arrayType->isArray()->yes() - && $arrayType->isIterableAtLeastOnce()->yes() - && ($numArg === null || $one->isSuperTypeOf($scope->getType($numArg))->yes()) - ) { - return []; + $arrayArg = ArgumentsNormalizer::getArgByPositionOrName($node->dim->getArgs(), 0, 'array'); + if ($arrayArg !== null) { + $arrayArg = $arrayArg->value; + $numArg = ArgumentsNormalizer::getArgByPositionOrName($node->dim->getArgs(), 1, 'num'); + $one = new ConstantIntegerType(1); + $arrayType = $scope->getType($arrayArg); + + if ( + $arrayArg instanceof Node\Expr\Variable + && $node->var instanceof Node\Expr\Variable + && is_string($arrayArg->name) + && $arrayArg->name === $node->var->name + && $arrayType->isArray()->yes() + && $arrayType->isIterableAtLeastOnce()->yes() + && ($numArg === null || $one->isSuperTypeOf($scope->getType($numArg->value))->yes()) + ) { + return []; + } } } @@ -167,17 +171,20 @@ public function processNode(Node $node, Scope $scope): array && $node->dim->right instanceof Node\Scalar\Int_ && $node->dim->right->value === 1 ) { - $arrayArg = $node->dim->left->getArgs()[0]->value; - $arrayType = $scope->getType($arrayArg); - if ( - $arrayArg instanceof Node\Expr\Variable - && $node->var instanceof Node\Expr\Variable - && is_string($arrayArg->name) - && $arrayArg->name === $node->var->name - && $arrayType->isList()->yes() - && $arrayType->isIterableAtLeastOnce()->yes() - ) { - return []; + $arrayArg = ArgumentsNormalizer::getArgByPositionOrName($node->dim->left->getArgs(), 0, 'value'); + if ($arrayArg !== null) { + $arrayArg = $arrayArg->value; + $arrayType = $scope->getType($arrayArg); + if ( + $arrayArg instanceof Node\Expr\Variable + && $node->var instanceof Node\Expr\Variable + && is_string($arrayArg->name) + && $arrayArg->name === $node->var->name + && $arrayType->isList()->yes() + && $arrayType->isIterableAtLeastOnce()->yes() + ) { + return []; + } } } diff --git a/tests/PHPStan/Rules/Arrays/data/array-dim-fetch-on-array-key-first-last.php b/tests/PHPStan/Rules/Arrays/data/array-dim-fetch-on-array-key-first-last.php index 82fac733275..355458cbd13 100644 --- a/tests/PHPStan/Rules/Arrays/data/array-dim-fetch-on-array-key-first-last.php +++ b/tests/PHPStan/Rules/Arrays/data/array-dim-fetch-on-array-key-first-last.php @@ -1,4 +1,4 @@ -= 8.0 namespace ArrayDimFetchOnArrayKeyFirstOrLast; @@ -47,4 +47,12 @@ public function countOnArray(array $hellos, array $anotherArray): string return $hellos[count($hellos) - 1]; } + + /** + * @param non-empty-array $hellos + */ + public function namedRandom(array $hellos): string + { + return $hellos[array_rand(num: 1, array: $hellos)]; + } } From bc64657c339c4fe415687814ce69825225722908 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 11 Nov 2026 20:54:59 +0100 Subject: [PATCH 3/8] ImpossibleInArrayHaystackFiniteTypesRule is missing named arguments --- ...possibleInArrayHaystackFiniteTypesRule.php | 42 ++++++++++++++----- ...ibleInArrayHaystackFiniteTypesRuleTest.php | 15 ++++++- .../data/impossible-in-array-finite-types.php | 12 ++++++ 3 files changed, 57 insertions(+), 12 deletions(-) diff --git a/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php b/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php index 6ec1b7b6756..b08ac03950a 100644 --- a/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php +++ b/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php @@ -4,12 +4,14 @@ use PhpParser\Node; use PhpParser\Node\Expr\FuncCall; +use PHPStan\Analyser\ArgumentsNormalizer; use PHPStan\Analyser\Scope; use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\Conditional; use PHPStan\DependencyInjection\RegisteredRule; use PHPStan\Php\PhpVersions; use PHPStan\Reflection\InitializerExprTypeResolver; +use PHPStan\Reflection\ParametersAcceptorSelector; use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\IdentifierRuleError; use PHPStan\Rules\Rule; @@ -23,7 +25,6 @@ use function array_unique; use function array_values; use function count; -use function max; use function sprintf; use function strtolower; @@ -51,9 +52,9 @@ final class ImpossibleInArrayHaystackFiniteTypesRule implements Rule public function __construct( private InitializerExprTypeResolver $initializerExprTypeResolver, + private ReflectionProvider $reflectionProvider, #[AutowiredParameter] private bool $treatPhpDocTypesAsCertain, - private ReflectionProvider $reflectionProvider, ) { } @@ -69,29 +70,48 @@ public function processNode(Node $node, Scope $scope): array return []; } - $functionName = strtolower((string) $this->reflectionProvider->resolveFunctionName($node->name, $scope)); - if (!array_key_exists($functionName, self::FUNCTIONS)) { + if (!$this->reflectionProvider->hasFunction($node->name, $scope)) { return []; } - $needleArg = self::FUNCTIONS[$functionName]['needle']; - $haystackArg = self::FUNCTIONS[$functionName]['haystack']; + $functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); + $functionName = strtolower($functionReflection->getName()); + if (!array_key_exists($functionName, self::FUNCTIONS)) { + return []; + } $args = $node->getArgs(); - if (count($args) <= max($needleArg, $haystackArg)) { + $parametersAcceptor = ParametersAcceptorSelector::selectFromArgs( + $scope, + $args, + $functionReflection->getVariants(), + $functionReflection->getNamedArgumentsVariants(), + ); + $normalizedFuncCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node); + if ($normalizedFuncCall === null) { + return []; + } + + $normalizedArgs = $normalizedFuncCall->getArgs(); + $needlePosition = self::FUNCTIONS[$functionName]['needle']; + $haystackPosition = self::FUNCTIONS[$functionName]['haystack']; + $needleArg = $normalizedArgs[$needlePosition] ?? null; + $haystackArg = $normalizedArgs[$haystackPosition] ?? null; + if ($needleArg === null || $haystackArg === null) { return []; } - $needleType = $this->treatPhpDocTypesAsCertain ? $scope->getType($args[$needleArg]->value) : $scope->getNativeType($args[$needleArg]->value); - $haystackType = $this->treatPhpDocTypesAsCertain ? $scope->getType($args[$haystackArg]->value) : $scope->getNativeType($args[$haystackArg]->value); + $needleType = $this->treatPhpDocTypesAsCertain ? $scope->getType($needleArg->value) : $scope->getNativeType($needleArg->value); + $haystackType = $this->treatPhpDocTypesAsCertain ? $scope->getType($haystackArg->value) : $scope->getNativeType($haystackArg->value); $constantArrays = $haystackType->getConstantArrays(); if (count($constantArrays) === 0) { return []; } - $isStrict = count($args) >= 3 - ? ($this->treatPhpDocTypesAsCertain ? $scope->getType($args[2]->value) : $scope->getNativeType($args[2]->value))->isTrue() + $strictArg = $normalizedArgs[2] ?? null; + $isStrict = $strictArg !== null + ? ($this->treatPhpDocTypesAsCertain ? $scope->getType($strictArg->value) : $scope->getNativeType($strictArg->value))->isTrue() : TrinaryLogic::createNo(); /** @var array, bool}> $deadValueTypes */ diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php index eef7003aded..e93b4fae91c 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php @@ -3,6 +3,7 @@ namespace PHPStan\Rules\Comparison; use PHPStan\Reflection\InitializerExprTypeResolver; +use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; use PHPUnit\Framework\Attributes\RequiresPhp; @@ -17,8 +18,8 @@ protected function getRule(): Rule { return new ImpossibleInArrayHaystackFiniteTypesRule( self::getContainer()->getByType(InitializerExprTypeResolver::class), + self::getContainer()->getByType(ReflectionProvider::class), true, - self::createReflectionProvider(), ); } @@ -42,6 +43,18 @@ public function testRule(): void 'Value ImpossibleInArrayFiniteTypes\Foo::ONE in the haystack passed to array_keys() can never be identical to the needle type int.', 38, ], + [ + 'Value ImpossibleInArrayFiniteTypes\Foo::ONE in the haystack passed to in_array() can never be identical to the needle type int.', + 278, + ], + [ + 'Value ImpossibleInArrayFiniteTypes\Foo::ONE in the haystack passed to array_search() can never be identical to the needle type int.', + 279, + ], + [ + 'Value ImpossibleInArrayFiniteTypes\Foo::ONE in the haystack passed to array_keys() can never be identical to the needle type int.', + 280, + ], [ 'Value \'installed\' in the haystack passed to in_array() can never be identical to the needle type mixed.', 99, diff --git a/tests/PHPStan/Rules/Comparison/data/impossible-in-array-finite-types.php b/tests/PHPStan/Rules/Comparison/data/impossible-in-array-finite-types.php index ad7ff074992..86cff80f01b 100644 --- a/tests/PHPStan/Rules/Comparison/data/impossible-in-array-finite-types.php +++ b/tests/PHPStan/Rules/Comparison/data/impossible-in-array-finite-types.php @@ -272,3 +272,15 @@ function noErrorIntegerRangeNeedle(int $i): void echo 'yes'; } } + +function reportedNamedInArray(int $i): void +{ + in_array(haystack: [Foo::ONE, 1, 2], strict: true, needle: $i); + array_search(haystack: [Foo::ONE, 1, 2], strict: true, needle: $i); + array_keys(strict: true, filter_value: $i, array: [Foo::ONE, 1, 2]); +} + +function noErrorNamedArrayKeysWithoutFilterValue(): void +{ + array_keys(array: [Foo::ONE], strict: true); +} From 2cb886ba50e6ed5749a6a1c75e35ea12e59e5af7 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 11 Nov 2026 21:07:22 +0100 Subject: [PATCH 4/8] Simplify type retrieval --- .../ImpossibleInArrayHaystackFiniteTypesRule.php | 9 +++------ .../ImpossibleInArrayHaystackFiniteTypesRuleTest.php | 1 - 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php b/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php index b08ac03950a..9ee9bd61232 100644 --- a/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php +++ b/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php @@ -6,7 +6,6 @@ use PhpParser\Node\Expr\FuncCall; use PHPStan\Analyser\ArgumentsNormalizer; use PHPStan\Analyser\Scope; -use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\Conditional; use PHPStan\DependencyInjection\RegisteredRule; use PHPStan\Php\PhpVersions; @@ -53,8 +52,6 @@ final class ImpossibleInArrayHaystackFiniteTypesRule implements Rule public function __construct( private InitializerExprTypeResolver $initializerExprTypeResolver, private ReflectionProvider $reflectionProvider, - #[AutowiredParameter] - private bool $treatPhpDocTypesAsCertain, ) { } @@ -101,8 +98,8 @@ public function processNode(Node $node, Scope $scope): array return []; } - $needleType = $this->treatPhpDocTypesAsCertain ? $scope->getType($needleArg->value) : $scope->getNativeType($needleArg->value); - $haystackType = $this->treatPhpDocTypesAsCertain ? $scope->getType($haystackArg->value) : $scope->getNativeType($haystackArg->value); + $needleType = $scope->getType($needleArg->value); + $haystackType = $scope->getType($haystackArg->value); $constantArrays = $haystackType->getConstantArrays(); if (count($constantArrays) === 0) { @@ -111,7 +108,7 @@ public function processNode(Node $node, Scope $scope): array $strictArg = $normalizedArgs[2] ?? null; $isStrict = $strictArg !== null - ? ($this->treatPhpDocTypesAsCertain ? $scope->getType($strictArg->value) : $scope->getNativeType($strictArg->value))->isTrue() + ? $scope->getNativeType($strictArg->value)->isTrue() : TrinaryLogic::createNo(); /** @var array, bool}> $deadValueTypes */ diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php index e93b4fae91c..47c3541f0a0 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php @@ -19,7 +19,6 @@ protected function getRule(): Rule return new ImpossibleInArrayHaystackFiniteTypesRule( self::getContainer()->getByType(InitializerExprTypeResolver::class), self::getContainer()->getByType(ReflectionProvider::class), - true, ); } From bcf8614f32ab652bcd95293cf436cbfdad3cbc7d Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 12 Nov 2026 07:30:55 +0100 Subject: [PATCH 5/8] add regression test --- .../ImpossibleInArrayHaystackFiniteTypesRuleTest.php | 8 ++++++++ .../data/impossible-in-array-finite-types.php | 12 ++++++++++++ 2 files changed, 20 insertions(+) diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php index 47c3541f0a0..9c6cd7d7811 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php @@ -83,6 +83,14 @@ public function testRule(): void 'Value ImpossibleInArrayFiniteTypes\HttpStatus::ServerError in the haystack passed to in_array() can never be identical to the needle type int.', 261, ], + [ + "Value 'c' in the haystack passed to array_search() can never be identical to the needle type 'a'|'b'.", + 291, + ], + [ + "Value '1' in the haystack passed to array_search() can never be identical to the needle type int.", + 297, + ], ]); } diff --git a/tests/PHPStan/Rules/Comparison/data/impossible-in-array-finite-types.php b/tests/PHPStan/Rules/Comparison/data/impossible-in-array-finite-types.php index 86cff80f01b..5b9f2b9ac77 100644 --- a/tests/PHPStan/Rules/Comparison/data/impossible-in-array-finite-types.php +++ b/tests/PHPStan/Rules/Comparison/data/impossible-in-array-finite-types.php @@ -284,3 +284,15 @@ function noErrorNamedArrayKeysWithoutFilterValue(): void { array_keys(array: [Foo::ONE], strict: true); } + +/** @param 'a'|'b' $s */ +function phpDocOnlyImpossible(string $s): void +{ + array_search($s, ['c'], true); +} + +/** @param true $strict */ +function phpDocStrict(int $i, bool $strict): void +{ + array_search($i, ['1', 1, 2], $strict); +} From edf63b7969e909e8e35323a62f1a205be830718f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 12 Nov 2026 07:30:59 +0100 Subject: [PATCH 6/8] Revert "Simplify type retrieval" This reverts commit 2cb886ba50e6ed5749a6a1c75e35ea12e59e5af7. --- .../ImpossibleInArrayHaystackFiniteTypesRule.php | 9 ++++++--- .../ImpossibleInArrayHaystackFiniteTypesRuleTest.php | 1 + 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php b/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php index 9ee9bd61232..b08ac03950a 100644 --- a/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php +++ b/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php @@ -6,6 +6,7 @@ use PhpParser\Node\Expr\FuncCall; use PHPStan\Analyser\ArgumentsNormalizer; use PHPStan\Analyser\Scope; +use PHPStan\DependencyInjection\AutowiredParameter; use PHPStan\DependencyInjection\Conditional; use PHPStan\DependencyInjection\RegisteredRule; use PHPStan\Php\PhpVersions; @@ -52,6 +53,8 @@ final class ImpossibleInArrayHaystackFiniteTypesRule implements Rule public function __construct( private InitializerExprTypeResolver $initializerExprTypeResolver, private ReflectionProvider $reflectionProvider, + #[AutowiredParameter] + private bool $treatPhpDocTypesAsCertain, ) { } @@ -98,8 +101,8 @@ public function processNode(Node $node, Scope $scope): array return []; } - $needleType = $scope->getType($needleArg->value); - $haystackType = $scope->getType($haystackArg->value); + $needleType = $this->treatPhpDocTypesAsCertain ? $scope->getType($needleArg->value) : $scope->getNativeType($needleArg->value); + $haystackType = $this->treatPhpDocTypesAsCertain ? $scope->getType($haystackArg->value) : $scope->getNativeType($haystackArg->value); $constantArrays = $haystackType->getConstantArrays(); if (count($constantArrays) === 0) { @@ -108,7 +111,7 @@ public function processNode(Node $node, Scope $scope): array $strictArg = $normalizedArgs[2] ?? null; $isStrict = $strictArg !== null - ? $scope->getNativeType($strictArg->value)->isTrue() + ? ($this->treatPhpDocTypesAsCertain ? $scope->getType($strictArg->value) : $scope->getNativeType($strictArg->value))->isTrue() : TrinaryLogic::createNo(); /** @var array, bool}> $deadValueTypes */ diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php index 9c6cd7d7811..ef197e5ba88 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php @@ -19,6 +19,7 @@ protected function getRule(): Rule return new ImpossibleInArrayHaystackFiniteTypesRule( self::getContainer()->getByType(InitializerExprTypeResolver::class), self::getContainer()->getByType(ReflectionProvider::class), + true, ); } From a0389f01b3c2d6a6c6e72cea5438d7561c8e9984 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 12 Nov 2026 07:41:40 +0100 Subject: [PATCH 7/8] revert unnecessary changes --- .../Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php | 2 +- .../ImpossibleInArrayHaystackFiniteTypesRuleTest.php | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php b/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php index b08ac03950a..94d686d2733 100644 --- a/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php +++ b/src/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRule.php @@ -52,9 +52,9 @@ final class ImpossibleInArrayHaystackFiniteTypesRule implements Rule public function __construct( private InitializerExprTypeResolver $initializerExprTypeResolver, - private ReflectionProvider $reflectionProvider, #[AutowiredParameter] private bool $treatPhpDocTypesAsCertain, + private ReflectionProvider $reflectionProvider, ) { } diff --git a/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php b/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php index ef197e5ba88..e7d61d17bd2 100644 --- a/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/ImpossibleInArrayHaystackFiniteTypesRuleTest.php @@ -3,7 +3,6 @@ namespace PHPStan\Rules\Comparison; use PHPStan\Reflection\InitializerExprTypeResolver; -use PHPStan\Reflection\ReflectionProvider; use PHPStan\Rules\Rule; use PHPStan\Testing\RuleTestCase; use PHPUnit\Framework\Attributes\RequiresPhp; @@ -18,8 +17,8 @@ protected function getRule(): Rule { return new ImpossibleInArrayHaystackFiniteTypesRule( self::getContainer()->getByType(InitializerExprTypeResolver::class), - self::getContainer()->getByType(ReflectionProvider::class), true, + self::createReflectionProvider(), ); } From fb0317ab44e1017907ae13f82c9091fb96b3893f Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Thu, 12 Nov 2026 07:55:36 +0100 Subject: [PATCH 8/8] kill mutants --- .../NonexistentOffsetInArrayDimFetchRuleTest.php | 16 ++++++++++++++++ .../array-dim-after-array-key-first-or-last.php | 16 ++++++++++++++++ .../Rules/Arrays/data/array-dim-after-count.php | 15 +++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php index 0724cb09adf..8fff49fdee8 100644 --- a/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php +++ b/tests/PHPStan/Rules/Arrays/NonexistentOffsetInArrayDimFetchRuleTest.php @@ -856,6 +856,14 @@ public function testArrayDimFetchAfterArrayKeyFirstOrLast(): void 'Offset null does not exist on array{}.', 19, ], + [ + 'Offset mixed might not exist on array|string.', + 81, + ], + [ + 'Offset int|string might not exist on array|string.', + 89, + ], ]); } @@ -876,6 +884,14 @@ public function testArrayDimFetchAfterCount(): void 'Offset int<0, max> might not exist on non-empty-array.', 42, ], + [ + 'Offset int<-1, max> might not exist on list.', + 50, + ], + [ + 'Offset int<0, max> might not exist on non-empty-array.', + 58, + ], ]); } diff --git a/tests/PHPStan/Rules/Arrays/data/array-dim-after-array-key-first-or-last.php b/tests/PHPStan/Rules/Arrays/data/array-dim-after-array-key-first-or-last.php index e27fcfa1759..3668e95dc90 100644 --- a/tests/PHPStan/Rules/Arrays/data/array-dim-after-array-key-first-or-last.php +++ b/tests/PHPStan/Rules/Arrays/data/array-dim-after-array-key-first-or-last.php @@ -72,4 +72,20 @@ public function shape(array $hellos): int|bool } return $hellos[$last]; } + + /** + * @param array|string $hellos + */ + public function maybeFirst(array|string $hellos): string + { + return $hellos[array_key_first($hellos)]; + } + + /** + * @param array|string $hellos + */ + public function maybeRandom(array|string $hellos): string + { + return $hellos[array_rand($hellos, 1)]; + } } diff --git a/tests/PHPStan/Rules/Arrays/data/array-dim-after-count.php b/tests/PHPStan/Rules/Arrays/data/array-dim-after-count.php index 4f52d30b24d..18b3d0e0bc8 100644 --- a/tests/PHPStan/Rules/Arrays/data/array-dim-after-count.php +++ b/tests/PHPStan/Rules/Arrays/data/array-dim-after-count.php @@ -42,4 +42,19 @@ public function maybeInvalid(array $hellos): string return $hellos[$count]; } + /** + * @param list $hellos + */ + public function maybeEmptyList(array $hellos): string + { + return $hellos[count($hellos) - 1]; + } + + /** + * @param non-empty-array $hellos + */ + public function maybeNonListWithIntKeys(array $hellos): string + { + return $hellos[count($hellos) - 1]; + } }