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

Don‘t turn a string into an array|string by accident

This commit is contained in:
Matt Brown 2017-11-20 17:10:05 -05:00
parent 0c2ea418e3
commit a069cf4ea9
2 changed files with 20 additions and 2 deletions

View File

@ -1139,6 +1139,8 @@ class AssignmentChecker
$var_id_additions = [];
$real_var_id = true;
$child_stmt = null;
// First go from the root element up, and go as far as we can to figure out what
@ -1167,9 +1169,11 @@ class AssignmentChecker
$var_id_additions[] = '[\'' . $child_stmt->dim->value . '\']';
} else {
$var_id_additions[] = '[' . $child_stmt->dim->inferredType . ']';
$real_var_id = false;
}
} else {
$var_id_additions[] = '';
$real_var_id = false;
}
if (!isset($child_stmt->var->inferredType)) {
@ -1204,7 +1208,11 @@ class AssignmentChecker
}
}
if ($root_var_id && isset($child_stmt->var->inferredType) && !$child_stmt->var->inferredType->hasObjectType()) {
if ($root_var_id
&& $real_var_id
&& isset($child_stmt->var->inferredType)
&& !$child_stmt->var->inferredType->hasObjectType()
) {
$array_var_id = $root_var_id . implode('', $var_id_additions);
$context->vars_in_scope[$array_var_id] = clone $assignment_type;
}
@ -1300,7 +1308,7 @@ class AssignmentChecker
} else {
$new_child_type = $root_type; // noop
}
} else {
} elseif (array_keys($root_type->types) !== ['string']) {
$array_assignment_type = new Type\Union([
new TArray([
isset($current_dim->inferredType) ? $current_dim->inferredType : Type::getInt(),
@ -1312,6 +1320,8 @@ class AssignmentChecker
$root_type,
$array_assignment_type
);
} else {
$new_child_type = $root_type;
}
unset($new_child_type->types['null']);

View File

@ -692,6 +692,14 @@ class ArrayAssignmentTest extends TestCase
],
'error_levels' => ['PossiblyNullArrayAssignment'],
],
'stringAssignment' => [
'<?php
$str = "hello";
$str[0] = "i";',
'assertions' => [
'$str' => 'string',
],
],
];
}