Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
));
Expand Down
10 changes: 9 additions & 1 deletion src/Type/Php/ArraySearchFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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,
);
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'], []);
}

}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-14312.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace Bug14312;

function get_something(): mixed { return null; }

function test(string $a, string $b): bool {
$o = get_something();
if (array_key_exists($a, $o)) {
if (array_key_exists($b, $o)) {
return true;
}
}
return false;
}
15 changes: 15 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-14312b.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types = 1);

namespace Bug14312b;

function get_something(): mixed { return null; }

function test(string $needle): bool {
$o = get_something();
if (array_search($needle, $o) !== false) {
if (array_search($needle, $o)) {
return true;
}
}
return false;
}
Loading