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

Improve handling of unset array, ensuring that keys don’t persevere

This commit is contained in:
Matthew Brown 2019-12-22 01:42:39 +00:00
parent a2a2d8cd97
commit 4c8730c5a3
5 changed files with 30 additions and 3 deletions

View File

@ -301,7 +301,10 @@ class IfAnalyzer
// we calculate the vars redefined in a hypothetical else statement to determine
// which vars of the if we can safely change
$pre_assignment_else_redefined_vars = $temp_else_context->getRedefinedVars($context->vars_in_scope, true);
$pre_assignment_else_redefined_vars = array_intersect_key(
$temp_else_context->getRedefinedVars($context->vars_in_scope, true),
$changed_var_ids
);
// this captures statements in the if conditional
if ($context->collect_references) {

View File

@ -1676,7 +1676,9 @@ class MethodCallAnalyzer extends \Psalm\Internal\Analyzer\Statements\Expression\
}
if (!isset($class_template_params[$type_name])) {
$class_template_params[$type_name][$class_storage->name] = [Type::getMixed()];
$class_template_params[$type_name] = [
$class_storage->name => [Type::getMixed()]
];
}
$i++;

View File

@ -88,6 +88,10 @@ class NegatedAssertionReconciler extends Reconciler
if (!$is_equality) {
if ($assertion === 'isset') {
if ($existing_var_type->possibly_undefined) {
return Type::getEmpty();
}
return Type::getNull();
} elseif ($assertion === 'array-key-exists') {
return Type::getEmpty();

View File

@ -221,6 +221,8 @@ class Reconciler
}
}
$did_type_exist = isset($existing_types[$key]);
$result_type = isset($existing_types[$key])
? clone $existing_types[$key]
: self::getValueForKey(
@ -228,7 +230,7 @@ class Reconciler
$key,
$existing_types,
$code_location,
$has_isset,
$has_isset || $has_inverted_isset,
$has_empty
);
@ -280,6 +282,10 @@ class Reconciler
throw new \UnexpectedValueException('$result_type should not be null');
}
if (!$did_type_exist && $result_type->isEmpty()) {
continue;
}
$type_changed = !$before_adjustment || !$result_type->equals($before_adjustment);
if ($type_changed || $failed_reconciliation) {

View File

@ -694,6 +694,18 @@ class IssetTest extends \Psalm\Tests\TestCase
class B extends P {}
class C extends P {}'
],
'issetCreateObjectLikeWithType' => [
'<?php
function foo(array $options): void {
if (isset($options["a"])) {
$options["b"] = "hello";
}
if (\is_array($options["b"])) {}
}
'
],
];
}