1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #4976 - improve type narrowing

This commit is contained in:
Matt Brown 2021-01-11 17:14:23 -05:00 committed by Daniil Gentili
parent 89188f378e
commit 25505b4b03
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
4 changed files with 27 additions and 2 deletions

View File

@ -6,6 +6,11 @@ use Psalm\Internal\Analyzer\Statements\Expression\ExpressionIdentifier;
/** /**
* @internal * @internal
*
* This produces a graph of probably assignments inside a loop
*
* With this map we can calculate how many times the loop analysis must
* be run before all variables have the correct types
*/ */
class AssignmentMapVisitor extends PhpParser\NodeVisitorAbstract class AssignmentMapVisitor extends PhpParser\NodeVisitorAbstract
{ {

View File

@ -83,6 +83,8 @@ class TArray extends \Psalm\Type\Atomic
public function getAssertionString(): string public function getAssertionString(): string
{ {
return $this->getKey(); return 'array<'
. $this->type_params[0]->getAssertionString() . ', ' . $this->type_params[1]->getAssertionString()
. '>' ;
} }
} }

View File

@ -192,7 +192,7 @@ class TList extends \Psalm\Type\Atomic
public function getAssertionString(): string public function getAssertionString(): string
{ {
return 'list'; return 'list<' . $this->type_param->getAssertionString() . '>';
} }
public function getChildNodes() : array public function getChildNodes() : array

View File

@ -836,6 +836,24 @@ class ValueTest extends \Psalm\Tests\TestCase
if (!in_array(A::ACTION_ONE, $case_actions, true)) {} if (!in_array(A::ACTION_ONE, $case_actions, true)) {}
}' }'
], ],
'checkIdenticalArray' => [
'<?php
/** @psalm-suppress MixedAssignment */
$array = json_decode(file_get_contents(\'php://stdin\'));
if (is_array($array)) {
$filtered = array_filter($array, fn ($value) => \is_string($value));
if ($array === $filtered) {
foreach ($array as $obj) {
echo strlen($obj);
}
}
}',
[],
[],
'7.4'
],
]; ];
} }