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

Fix import type intersections #8994

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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
"phpunit"
],
"verify-callmap": "phpunit tests/Internal/Codebase/InternalCallMapHandlerTest.php",
"psalm": "@php ./psalm --find-dead-code",
"psalm": "@php ./psalm --find-dead-code -m",
"tests": [
"@lint",
"@cs",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,12 @@ public function start(PhpParser\Node\Stmt\ClassLike $node): ?bool

foreach ($type_aliases as $type_alias) {
// finds issues, if there are any
TypeParser::parseTokens($type_alias->replacement_tokens);
TypeParser::parseTokens(
$type_alias->replacement_tokens,
null,
[],
$this->type_aliases,
);
}

$this->type_aliases += $type_aliases;
Expand Down
1 change: 1 addition & 0 deletions src/Psalm/Internal/Type/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,7 @@ private static function getTypeFromIntersectionTree(
$intersection_types[$name] = $atomic_type;
}

// TODO: Handle type aliases better
$first_type = reset($intersection_types);
$last_type = end($intersection_types);

Expand Down
19 changes: 9 additions & 10 deletions src/Psalm/Type/Atomic.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,7 @@ private static function createInner(
string $value,
?int $analysis_php_version_id = null,
array $template_type_map = [],
array $type_aliases = [],
bool $from_docblock = false
array $type_aliases = []
): Atomic {
switch ($value) {
case 'int':
Expand Down Expand Up @@ -243,27 +242,27 @@ private static function createInner(
case 'array':
case 'associative-array':
return new TArray([
new Union([new TArrayKey($from_docblock)]),
new Union([new TMixed(false, $from_docblock)]),
new Union([new TArrayKey(false)]),
new Union([new TMixed(false, false)]),
]);

case 'non-empty-array':
return new TNonEmptyArray([
new Union([new TArrayKey($from_docblock)]),
new Union([new TMixed(false, $from_docblock)]),
new Union([new TArrayKey(false)]),
new Union([new TMixed(false, false)]),
]);

case 'callable-array':
return new TCallableArray([
new Union([new TArrayKey($from_docblock)]),
new Union([new TMixed(false, $from_docblock)]),
new Union([new TArrayKey(false)]),
new Union([new TMixed(false, false)]),
]);

case 'list':
return Type::getListAtomic(Type::getMixed(false, $from_docblock));
return Type::getListAtomic(Type::getMixed(false, false));

case 'non-empty-list':
return Type::getNonEmptyListAtomic(Type::getMixed(false, $from_docblock));
return Type::getNonEmptyListAtomic(Type::getMixed(false, false));

case 'non-empty-string':
return new TNonEmptyString();
Expand Down
26 changes: 26 additions & 0 deletions tests/TypeAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,32 @@ public function doStuff(): array {
'$output===' => 'list<1|2>',
],
],
'intersection with imported type' => [
'code' => <<<'PHP'
<?php
/**
* @psalm-type psalmA = array{a: string}
*/
class A {}
/**
* @psalm-type psalmB = array{b: string}
* @psalm-import-type psalmA from A
* @psalm-type psalmC = psalmB&psalmA
*/
class B {
/**
* @return psalmC
*/
public static function getC(): array {
return ['a' => '', 'b' => ''];
}
}
$c = B::getC();
PHP,
'assertions' => [
'$c===' => 'array{a: string, b: string}',
],
],
];
}

Expand Down