Skip to content
Permalink
Browse files

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

  • Loading branch information
muglug committed Dec 22, 2019
1 parent a2a2d8c commit 4c8730c5a3f35eb8de32136b1fbabfb2a2862449
@@ -301,7 +301,10 @@ function ($carry, Clause $clause) {

// 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) {
@@ -1676,7 +1676,9 @@ public static function getClassTemplateParams(
}

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++;
@@ -88,6 +88,10 @@ public static function reconcile(

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();
@@ -221,14 +221,16 @@ public static function reconcileKeyedTypes(
}
}

$did_type_exist = isset($existing_types[$key]);

$result_type = isset($existing_types[$key])
? clone $existing_types[$key]
: self::getValueForKey(
$codebase,
$key,
$existing_types,
$code_location,
$has_isset,
$has_isset || $has_inverted_isset,
$has_empty
);

@@ -280,6 +282,10 @@ public static function reconcileKeyedTypes(
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) {
@@ -694,6 +694,18 @@ class A extends P {}
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"])) {}
}
'
],
];
}

0 comments on commit 4c8730c

Please sign in to comment.
You can’t perform that action at this time.