From 24977fb0211246e8063500f0d7bc725baa299a21 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Wed, 2 Oct 2019 01:17:45 -0400 Subject: [PATCH] Fix small bug --- src/Psalm/Type/Reconciler.php | 48 ++++++++++++++++++++++------------- tests/AssertTest.php | 16 ++++++++++++ 2 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/Psalm/Type/Reconciler.php b/src/Psalm/Type/Reconciler.php index dd0b4a32b..77b099f28 100644 --- a/src/Psalm/Type/Reconciler.php +++ b/src/Psalm/Type/Reconciler.php @@ -163,22 +163,6 @@ class Reconciler $codebase = $statements_analyzer->getCodebase(); foreach ($new_types as $key => $new_type_parts) { - $result_type = isset($existing_types[$key]) - ? clone $existing_types[$key] - : self::getValueForKey( - $codebase, - $key, - $existing_types, - $code_location - ); - - if ($result_type && empty($result_type->getTypes())) { - throw new \InvalidArgumentException('Union::$types cannot be empty after get value for ' . $key); - } - - $before_adjustment = $result_type ? clone $result_type : null; - - $failed_reconciliation = 0; $has_negation = false; $has_equality = false; $has_isset = false; @@ -212,7 +196,32 @@ class Reconciler $has_count_check = $has_count_check || $new_type_part_part === 'non-empty-countable'; + } + } + + $result_type = isset($existing_types[$key]) + ? clone $existing_types[$key] + : self::getValueForKey( + $codebase, + $key, + $existing_types, + $code_location, + $has_isset + ); + + if ($result_type && empty($result_type->getTypes())) { + throw new \InvalidArgumentException('Union::$types cannot be empty after get value for ' . $key); + } + + $before_adjustment = $result_type ? clone $result_type : null; + + $failed_reconciliation = 0; + + foreach ($new_type_parts as $new_type_part_parts) { + $orred_type = null; + + foreach ($new_type_part_parts as $new_type_part_part) { $result_type_candidate = AssertionReconciler::reconcile( $new_type_part_part, $result_type ? clone $result_type : null, @@ -411,7 +420,8 @@ class Reconciler Codebase $codebase, string $key, array &$existing_keys, - CodeLocation $code_location = null + ?CodeLocation $code_location, + bool $has_isset ) { $key_parts = self::breakUpPathIntoParts($key); @@ -458,7 +468,9 @@ class Reconciler if ($existing_key_type_part instanceof Type\Atomic\TArray) { $new_base_type_candidate = clone $existing_key_type_part->type_params[1]; - $new_base_type_candidate->possibly_undefined = true; + if ($has_isset) { + $new_base_type_candidate->possibly_undefined = true; + } } elseif (!$existing_key_type_part instanceof Type\Atomic\ObjectLike) { return Type::getMixed(); } elseif ($array_key[0] === '$') { diff --git a/tests/AssertTest.php b/tests/AssertTest.php index 26a7a92d5..22e3b007a 100644 --- a/tests/AssertTest.php +++ b/tests/AssertTest.php @@ -255,6 +255,22 @@ class AssertTest extends TestCase return ["b" => 1]; }' ], + 'setArrayConstantOffset' => [ + '