Skip to content
Open
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
18 changes: 14 additions & 4 deletions src/Type/Php/ArrayFilterFunctionReturnTypeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,22 @@ private function processKeyAndItemType(MutatingScope $scope, Type $keyType, Type
return [new NeverType(), new NeverType(), false];
}

$scope = $scope->filterByTruthyValue($expr);
$truthyScope = $scope->filterByTruthyValue($expr);

$optional = !$booleanResult->isTrue()->yes();
if ($optional) {
$falseyScope = $scope->filterByFalseyValue($expr);
$falseyItemType = $itemVarName !== null ? $falseyScope->getVariableType($itemVarName) : $itemType;
$falseyKeyType = $keyVarName !== null ? $falseyScope->getVariableType($keyVarName) : $keyType;
if ($falseyItemType instanceof NeverType || $falseyKeyType instanceof NeverType) {
$optional = false;
}
}

return [
$keyVarName !== null ? $scope->getVariableType($keyVarName) : $keyType,
$itemVarName !== null ? $scope->getVariableType($itemVarName) : $itemType,
!$booleanResult->isTrue()->yes(),
$keyVarName !== null ? $truthyScope->getVariableType($keyVarName) : $keyType,
$itemVarName !== null ? $truthyScope->getVariableType($itemVarName) : $itemType,
$optional,
];
}

Expand Down
24 changes: 24 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-11730.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php // lint >= 8.1

declare(strict_types = 1);

namespace Bug11730;

use function PHPStan\Testing\assertType;

class Foo {}

/** @return ($value is Foo ? true : false) */
function isFoo(mixed $value): bool {
return $value instanceof Foo;
}

/** @phpstan-assert-if-true Foo $value */
function checkFoo(mixed $value): bool {
return $value instanceof Foo;
}

$data = [new Foo, new Foo];

assertType('array{Bug11730\Foo, Bug11730\Foo}', array_filter($data, isFoo(...)));
assertType('array{Bug11730\Foo, Bug11730\Foo}', array_filter($data, checkFoo(...)));
Loading