Skip to content

Commit 0044649

Browse files
mglamanondrejmirtes
authored andcommitted
ArrayFilterFunctionReturnTypeReturnTypeExtension handle error types
1 parent 758e5f1 commit 0044649

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/Type/Php/ArrayFilterFunctionReturnTypeReturnTypeExtension.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
2424
use PHPStan\Type\Constant\ConstantBooleanType;
2525
use PHPStan\Type\DynamicFunctionReturnTypeExtension;
26+
use PHPStan\Type\ErrorType;
2627
use PHPStan\Type\MixedType;
2728
use PHPStan\Type\NeverType;
2829
use PHPStan\Type\NullType;
@@ -54,9 +55,13 @@ public function getTypeFromFunctionCall(FunctionReflection $functionReflection,
5455
}
5556

5657
$arrayArgType = $scope->getType($arrayArg);
58+
$arrayArgType->isArray();
5759
$keyType = $arrayArgType->getIterableKeyType();
5860
$itemType = $arrayArgType->getIterableValueType();
5961

62+
if ($itemType instanceof ErrorType || $keyType instanceof ErrorType) {
63+
return new ConstantArrayType([], []);
64+
}
6065
if ($itemType instanceof NeverType || $keyType instanceof NeverType) {
6166
return new ConstantArrayType([], []);
6267
}

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,7 @@ public function dataFileAsserts(): iterable
13861386
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8366.php');
13871387
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-7291.php');
13881388
yield from $this->gatherAssertTypes(__DIR__ . '/data/conditional-vars.php');
1389+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-10189.php');
13891390
}
13901391

13911392
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Bug10189;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
interface SomeInterface {
8+
}
9+
10+
/**
11+
* @return array|SomeInterface|null|false
12+
*/
13+
function _file_save_upload_from_form() {
14+
return [];
15+
}
16+
17+
function file_managed_file_save_upload(): array {
18+
if (!$files = _file_save_upload_from_form()) {
19+
return [];
20+
}
21+
assertType('non-empty-array|Bug10189\SomeInterface', $files);
22+
$files = array_filter($files);
23+
assertType('array{}', $files);
24+
25+
return empty($files) ? [] : [1,2];
26+
}

0 commit comments

Comments
 (0)