From 3263b64301155e3ecf963b333f94f722915abbab Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 16 May 2018 11:47:39 -0400 Subject: [PATCH] Filter conditions better when encountering parent instanceof --- src/Psalm/Type/Reconciler.php | 9 +++++++++ tests/TypeAlgebraTest.php | 10 +++++----- tests/TypeReconciliationTest.php | 16 ++++++++++++++++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Psalm/Type/Reconciler.php b/src/Psalm/Type/Reconciler.php index 068d8629e..e44ff5834 100644 --- a/src/Psalm/Type/Reconciler.php +++ b/src/Psalm/Type/Reconciler.php @@ -665,6 +665,8 @@ class Reconciler $any_scalar_type_match_found = false; + $matching_atomic_types = []; + foreach ($new_type->getTypes() as $new_type_part) { $has_local_match = false; @@ -689,6 +691,9 @@ class Reconciler ) || $type_coerced ) { $has_local_match = true; + if ($type_coerced) { + $matching_atomic_types[] = $existing_var_type_part; + } break; } @@ -719,6 +724,10 @@ class Reconciler } } + if ($matching_atomic_types) { + $new_type = new Type\Union($matching_atomic_types); + } + if (!$has_match && (!$is_loose_equality || !$any_scalar_type_match_found)) { if ($new_var_type === 'null') { if ($existing_var_type->from_docblock) { diff --git a/tests/TypeAlgebraTest.php b/tests/TypeAlgebraTest.php index e5ea4f80d..dd40a08a0 100644 --- a/tests/TypeAlgebraTest.php +++ b/tests/TypeAlgebraTest.php @@ -795,15 +795,15 @@ class TypeAlgebraTest extends TestCase ], 'instanceofNoRedundant' => [ ' [ + 'foo(); + }', + ], ]; }