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:
parent
89188f378e
commit
25505b4b03
@ -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
|
||||||
{
|
{
|
||||||
|
@ -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()
|
||||||
|
. '>' ;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
@ -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'
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user