Skip to content

Commit dd498a5

Browse files
phpstan-botclaudestaabm
authored
Fix phpstan/phpstan#14312: Parameter #2 of array_key_exists expects array, non-empty-array given (#5237)
Co-authored-by: Claude Opus 4.6 <[email protected]> Co-authored-by: Markus Staab <[email protected]>
1 parent 5e6e603 commit dd498a5

File tree

5 files changed

+55
-2
lines changed

5 files changed

+55
-2
lines changed

src/Type/Php/ArrayKeyExistsFunctionTypeSpecifyingExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,12 @@ public function specifyTypes(
7878
$specifiedTypes = new SpecifiedTypes();
7979

8080
if (count($keyType->getConstantScalarTypes()) <= 1) {
81+
$nonEmptyType = $arrayType->isArray()->yes()
82+
? new NonEmptyArrayType()
83+
: TypeCombinator::intersect(new ArrayType(new MixedType(), new MixedType()), new NonEmptyArrayType());
8184
$specifiedTypes = $specifiedTypes->unionWith($this->typeSpecifier->create(
8285
$array,
83-
new NonEmptyArrayType(),
86+
$nonEmptyType,
8487
$context,
8588
$scope,
8689
));

src/Type/Php/ArraySearchFunctionTypeSpecifyingExtension.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
use PHPStan\DependencyInjection\AutowiredService;
1212
use PHPStan\Reflection\FunctionReflection;
1313
use PHPStan\Type\Accessory\NonEmptyArrayType;
14+
use PHPStan\Type\ArrayType;
1415
use PHPStan\Type\FunctionTypeSpecifyingExtension;
16+
use PHPStan\Type\MixedType;
17+
use PHPStan\Type\TypeCombinator;
1518
use function strtolower;
1619

1720
#[AutowiredService]
@@ -42,9 +45,14 @@ public function specifyTypes(
4245
return new SpecifiedTypes();
4346
}
4447

48+
$arrayType = $scope->getType($arrayArg);
49+
$nonEmptyType = $arrayType->isArray()->yes()
50+
? new NonEmptyArrayType()
51+
: TypeCombinator::intersect(new ArrayType(new MixedType(), new MixedType()), new NonEmptyArrayType());
52+
4553
return $this->typeSpecifier->create(
4654
$arrayArg,
47-
new NonEmptyArrayType(),
55+
$nonEmptyType,
4856
$context,
4957
$scope,
5058
);

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,4 +2746,16 @@ public function testBug13247(): void
27462746
$this->analyse([__DIR__ . '/data/bug-13247.php'], []);
27472747
}
27482748

2749+
#[RequiresPhp('>= 8.0')]
2750+
public function testBug14312(): void
2751+
{
2752+
$this->analyse([__DIR__ . '/data/bug-14312.php'], []);
2753+
}
2754+
2755+
#[RequiresPhp('>= 8.0')]
2756+
public function testBug14312b(): void
2757+
{
2758+
$this->analyse([__DIR__ . '/data/bug-14312b.php'], []);
2759+
}
2760+
27492761
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14312;
4+
5+
function get_something(): mixed { return null; }
6+
7+
function test(string $a, string $b): bool {
8+
$o = get_something();
9+
if (array_key_exists($a, $o)) {
10+
if (array_key_exists($b, $o)) {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14312b;
4+
5+
function get_something(): mixed { return null; }
6+
7+
function test(string $needle): bool {
8+
$o = get_something();
9+
if (array_search($needle, $o) !== false) {
10+
if (array_search($needle, $o)) {
11+
return true;
12+
}
13+
}
14+
return false;
15+
}

0 commit comments

Comments
 (0)