diff --git a/src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php b/src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php index ae9afb30055..ffbdc4069fb 100644 --- a/src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php +++ b/src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php @@ -78,9 +78,12 @@ public function specifyTypes( $specifiedTypes = new SpecifiedTypes(); if (count($keyType->getConstantScalarTypes()) <= 1) { + $nonEmptyType = $arrayType->isArray()->yes() + ? new NonEmptyArrayType() + : TypeCombinator::intersect(new ArrayType(new MixedType(), new MixedType()), new NonEmptyArrayType()); $specifiedTypes = $specifiedTypes->unionWith($this->typeSpecifier->create( $array, - new NonEmptyArrayType(), + $nonEmptyType, $context, $scope, )); diff --git a/src/Type/Php/ArraySearchFunctionTypeSpecifyingExtension.php b/src/Type/Php/ArraySearchFunctionTypeSpecifyingExtension.php index 38d8909e9b1..2d8e2ceaf69 100644 --- a/src/Type/Php/ArraySearchFunctionTypeSpecifyingExtension.php +++ b/src/Type/Php/ArraySearchFunctionTypeSpecifyingExtension.php @@ -11,7 +11,10 @@ use PHPStan\DependencyInjection\AutowiredService; use PHPStan\Reflection\FunctionReflection; use PHPStan\Type\Accessory\NonEmptyArrayType; +use PHPStan\Type\ArrayType; use PHPStan\Type\FunctionTypeSpecifyingExtension; +use PHPStan\Type\MixedType; +use PHPStan\Type\TypeCombinator; use function strtolower; #[AutowiredService] @@ -42,9 +45,14 @@ public function specifyTypes( return new SpecifiedTypes(); } + $arrayType = $scope->getType($arrayArg); + $nonEmptyType = $arrayType->isArray()->yes() + ? new NonEmptyArrayType() + : TypeCombinator::intersect(new ArrayType(new MixedType(), new MixedType()), new NonEmptyArrayType()); + return $this->typeSpecifier->create( $arrayArg, - new NonEmptyArrayType(), + $nonEmptyType, $context, $scope, ); diff --git a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php index 6bb50169fe6..f78f5918f77 100644 --- a/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php +++ b/tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php @@ -2746,4 +2746,16 @@ public function testBug13247(): void $this->analyse([__DIR__ . '/data/bug-13247.php'], []); } + #[RequiresPhp('>= 8.0')] + public function testBug14312(): void + { + $this->analyse([__DIR__ . '/data/bug-14312.php'], []); + } + + #[RequiresPhp('>= 8.0')] + public function testBug14312b(): void + { + $this->analyse([__DIR__ . '/data/bug-14312b.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Functions/data/bug-14312.php b/tests/PHPStan/Rules/Functions/data/bug-14312.php new file mode 100644 index 00000000000..c07ceac0bbf --- /dev/null +++ b/tests/PHPStan/Rules/Functions/data/bug-14312.php @@ -0,0 +1,15 @@ +