Skip to content

Commit

Permalink
Merge pull request #8708 from annervisser/allow-using-imported-type-i…
Browse files Browse the repository at this point in the history
…n-other-type

Allow using imported types in other types within the same file
  • Loading branch information
orklah committed Nov 18, 2022
2 parents ebc7599 + d10b1f9 commit 48c0df6
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/Psalm/Internal/PhpVisitor/ReflectorVisitor.php
Expand Up @@ -32,6 +32,7 @@
use Psalm\Type;
use UnexpectedValueException;

use function array_merge;
use function array_pop;
use function end;
use function explode;
Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 26 additions & 0 deletions tests/TypeAnnotationTest.php
Expand Up @@ -629,6 +629,32 @@ private function assertFoo($value): void {
'assertions' => [
'$output' => 'string',
]
],
'importedTypeUsedInOtherType' => [
'code' => '<?php
/** @psalm-type OpeningTypes=self::TYPE_A|self::TYPE_B */
class Foo {
public const TYPE_A = 1;
public const TYPE_B = 2;
}
/**
* @psalm-import-type OpeningTypes from Foo
* @psalm-type OpeningTypeAssignment=list<OpeningTypes>
*/
class Main {
/** @return OpeningTypeAssignment */
public function doStuff(): array {
return [];
}
}
$instance = new Main();
$output = $instance->doStuff();
',
'assertions' => [
'$output===' => 'list<1|2>',
]
]
];
}
Expand Down

0 comments on commit 48c0df6

Please sign in to comment.