Skip to content

Commit 7b1065c

Browse files
committed
Fix array_filter with callback optional persistance
1 parent 4f7beff commit 7b1065c

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/Type/Php/ArrayFilterFunctionReturnTypeReturnTypeExtension.php

+2
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ private function filterByTruthyValue(Scope $scope, Error|Variable|null $itemVar,
192192
$results = [];
193193
foreach ($constantArrays as $constantArray) {
194194
$builder = ConstantArrayTypeBuilder::createEmpty();
195+
$optionalKeys = $constantArray->getOptionalKeys();
195196
foreach ($constantArray->getKeyTypes() as $i => $keyType) {
196197
$itemType = $constantArray->getValueTypes()[$i];
197198
[$newKeyType, $newItemType, $optional] = $this->processKeyAndItemType($scope, $keyType, $itemType, $itemVar, $keyVar, $expr);
199+
$optional = $optional || in_array($i, $optionalKeys, true);
198200
if ($newKeyType instanceof NeverType || $newItemType instanceof NeverType) {
199201
continue;
200202
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Bug11570;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @param array{one?: string, two?: string|null, three: string|null} $data
9+
*/
10+
function test(array $data): void
11+
{
12+
$data = array_filter($data, fn($var) => $var !== null);
13+
assertType("array{one?: string, two?: string, three?: string}", $data);
14+
}

0 commit comments

Comments
 (0)