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

Properly clone array when changing value

This commit is contained in:
Brown 2019-08-27 17:00:00 -04:00
parent aba3659311
commit 095ea1a554
3 changed files with 34 additions and 3 deletions

View File

@ -222,12 +222,12 @@ class ArrayAssignmentAnalyzer
$child_stmt->var->inferredType = clone $context->vars_in_scope[$parent_var_id];
}
$parent_var_id = $array_var_id;
$array_type = clone $child_stmt->var->inferredType;
$child_stmt->inferredType = ArrayFetchAnalyzer::getArrayAccessTypeGivenOffset(
$statements_analyzer,
$child_stmt,
$child_stmt->var->inferredType,
$array_type,
isset($child_stmt->dim->inferredType) ? $child_stmt->dim->inferredType : Type::getInt(),
true,
$array_var_id,
@ -236,6 +236,19 @@ class ArrayAssignmentAnalyzer
$child_stmts ? null : $assignment_type
);
$child_stmt->var->inferredType = $array_type;
if ($root_var_id) {
if (!$parent_var_id) {
$rooted_parent_id = $root_var_id;
$root_type = $array_type;
} else {
$rooted_parent_id = $parent_var_id;
}
$context->vars_in_scope[$rooted_parent_id] = $array_type;
}
if (!$child_stmts) {
$child_stmt->inferredType = $assignment_type;
}
@ -243,6 +256,8 @@ class ArrayAssignmentAnalyzer
$current_type = $child_stmt->inferredType;
$current_dim = $child_stmt->dim;
$parent_var_id = $array_var_id;
if ($child_stmt->var->inferredType->hasMixed()) {
$full_var_id = false;
break;

View File

@ -765,7 +765,9 @@ class TypeCombination
);
}
unset($possibly_undefined_entries[$candidate_property_name]);
if (!$type->had_mixed_value) {
unset($possibly_undefined_entries[$candidate_property_name]);
}
}
if ($combination->array_counts !== null) {

View File

@ -1086,6 +1086,20 @@ class ArrayAssignmentTest extends TestCase
foo($arr);
}'
],
'checkEmptinessAfterConditionalArrayAdjustment' => [
'<?php
class A {
public array $arr = [];
public function foo() : void {
if (rand(0, 1)) {
$this->arr["a"] = "hello";
}
if (!$this->arr) {}
}
}'
],
];
}