diff --git a/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php b/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php index 94422b17e01..4b4fdba1b4c 100644 --- a/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php +++ b/src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php @@ -32,6 +32,7 @@ use Psalm\Type; use UnexpectedValueException; +use function array_merge; use function array_pop; use function end; use function explode; @@ -187,7 +188,7 @@ public function enterNode(PhpParser\Node $node): ?int return PhpParser\NodeTraverser::DONT_TRAVERSE_CHILDREN; } - $this->type_aliases += $classlike_node_scanner->type_aliases; + $this->type_aliases = array_merge($this->type_aliases, $classlike_node_scanner->type_aliases); } elseif ($node instanceof PhpParser\Node\Stmt\TryCatch) { foreach ($node->catches as $catch) { foreach ($catch->types as $catch_type) { diff --git a/tests/TypeAnnotationTest.php b/tests/TypeAnnotationTest.php index c55a5911221..8cadb208886 100644 --- a/tests/TypeAnnotationTest.php +++ b/tests/TypeAnnotationTest.php @@ -629,6 +629,32 @@ private function assertFoo($value): void { 'assertions' => [ '$output' => 'string', ] + ], + 'importedTypeUsedInOtherType' => [ + 'code' => ' + */ + class Main { + /** @return OpeningTypeAssignment */ + public function doStuff(): array { + return []; + } + } + + $instance = new Main(); + $output = $instance->doStuff(); + ', + 'assertions' => [ + '$output===' => 'list<1|2>', + ] ] ]; }