1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix bug asserting array emptiness

This commit is contained in:
Brown 2019-10-03 15:27:50 -04:00
parent d8855282c6
commit 549aee47f1
2 changed files with 17 additions and 15 deletions

View File

@ -199,6 +199,18 @@ class ArrayAssignmentAnalyzer
&& is_string($child_stmt->dim->name)
) {
$var_id_additions[] = '[$' . $child_stmt->dim->name . ']';
} elseif ($child_stmt->dim instanceof PhpParser\Node\Expr\PropertyFetch
&& $child_stmt->dim->name instanceof PhpParser\Node\Identifier
) {
$object_id = ExpressionAnalyzer::getArrayVarId(
$child_stmt->dim->var,
$statements_analyzer->getFQCLN(),
$statements_analyzer
);
if ($object_id) {
$var_id_additions[] = '[' . $object_id . '->' . $child_stmt->dim->name->name . ']';
}
} else {
$var_id_additions[] = '[' . $child_stmt->dim->inferredType . ']';
$full_var_id = false;

View File

@ -271,7 +271,7 @@ class AssertTest extends TestCase
}
}',
],
'SKIPPED-bug' => [
'assertArrayWithPropertyOffset' => [
'<?php
class A {
public int $id = 0;
@ -280,20 +280,10 @@ class AssertTest extends TestCase
public function foo() : void {}
}
/** @param A[] $a_of_a */
function foo(array $a_of_a): void {
$arr = [];
$a = array_pop($a_of_a);
if (!isset($arr[$a->id])) {
$arr[$a->id] = new B();
}
$arr[$a->id]->foo();
/** @var A */
$a = array_pop($a_of_a);
/**
* @param array<int, B> $arr
*/
function foo(A $a, array $arr): void {
if (!isset($arr[$a->id])) {
$arr[$a->id] = new B();
}