1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 13:51:54 +01:00

Merge pull request #6679 from SCIF/bugfix-5694-arrayfilter

Improve handling of positive ints by array_filter()
This commit is contained in:
orklah 2021-10-17 23:20:26 +02:00 committed by GitHub
commit f7a63361ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 6 deletions

View File

@ -71,7 +71,7 @@ class ArrayFilterReturnTypeProvider implements \Psalm\Plugin\EventHandler\Functi
$new_properties = \array_filter(
array_map(
function ($keyed_type) use ($statements_source, $context) {
static function ($keyed_type) use ($statements_source, $context) {
$prev_keyed_type = $keyed_type;
$keyed_type = \Psalm\Internal\Type\AssertionReconciler::reconcile(
@ -85,16 +85,13 @@ class ArrayFilterReturnTypeProvider implements \Psalm\Plugin\EventHandler\Functi
$statements_source->getSuppressedIssues()
);
$keyed_type->possibly_undefined = ($prev_keyed_type->hasInt()
&& !$prev_keyed_type->hasLiteralInt())
|| $prev_keyed_type->hasFloat()
|| $prev_keyed_type->getId() !== $keyed_type->getId();
$keyed_type->possibly_undefined = !$prev_keyed_type->isAlwaysTruthy();
return $keyed_type;
},
$first_arg_array->properties
),
function ($keyed_type) {
static function ($keyed_type) {
return !$keyed_type->isEmpty();
}
);

View File

@ -28,6 +28,19 @@ class ArrayFunctionCallTest extends TestCase
'$e' => 'array<string, int<0, 10>|null>',
],
],
'positiveIntArrayFilter' => [
'<?php
/**
* @param numeric $a
* @param positive-int $positiveOne
* @param int<0,12> $d
* @param int<1,12> $f
* @psalm-return array{a: numeric, b?: int, c: positive-int, d?: int<0, 12>, f: int<1,12>}
*/
function makeAList($a, int $anyInt, int $positiveOne, int $d, int $f): array {
return array_filter(["a" => "1", "b" => $anyInt, "c" => $positiveOne, "d" => $d, "f" => $f]);
}'
],
'arrayFilterAdvanced' => [
'<?php
$f = array_filter(["a" => 5, "b" => 12, "c" => null], function(?int $val, string $key): bool {