From 97f4cdb7f51f671e7a09c3a5e2a002e405b021a4 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Sun, 26 May 2019 13:11:43 -0400 Subject: [PATCH] Fix #1686 - intersect object and named class properly --- src/Psalm/Type.php | 7 ++++++- tests/InterfaceTest.php | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Type.php b/src/Psalm/Type.php index 99020d336..45a44e70c 100644 --- a/src/Psalm/Type.php +++ b/src/Psalm/Type.php @@ -1340,7 +1340,7 @@ abstract class Type } else { $combined_type = clone $type_1; - foreach ($combined_type->getTypes() as $type_1_atomic) { + foreach ($combined_type->getTypes() as $t1_key => $type_1_atomic) { foreach ($type_2->getTypes() as $type_2_atomic) { if (($type_1_atomic instanceof TIterable || $type_1_atomic instanceof TNamedObject @@ -1367,6 +1367,11 @@ abstract class Type } } } + + if ($type_1_atomic instanceof TObject && $type_2_atomic instanceof TNamedObject) { + $combined_type->removeType($t1_key); + $combined_type->addType(clone $type_2_atomic); + } } } } diff --git a/tests/InterfaceTest.php b/tests/InterfaceTest.php index af13f6e78..1b9db6fba 100644 --- a/tests/InterfaceTest.php +++ b/tests/InterfaceTest.php @@ -581,6 +581,29 @@ class InterfaceTest extends TestCase return $i->foo(); }', ], + 'intersectionObjectTypes' => [ + 'foo(); + } + + /** @param IBar&IFoo $i */ + function iBarFirst($i) : C { + return $i->foo(); + }', + ], ]; }