Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using imported types in other types within the same file #8708

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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