1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Fix #1911 - allow proper namespaces in union assertions

This commit is contained in:
Matthew Brown 2019-07-07 09:14:16 -04:00
parent ea83068deb
commit bceb4efde9
2 changed files with 60 additions and 5 deletions

View File

@ -2440,11 +2440,18 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse
$assertion_type_parts = explode('|', $assertion_type);
foreach ($assertion_type_parts as $i => $assertion_type_part) {
if ($assertion_type_part !== 'falsy'
&& !isset($template_types[$assertion_type_part])
&& !isset(Type::PSALM_RESERVED_WORDS[$assertion_type_part])
) {
$assertion_type_parts[$i] = $prefix . Type::getFQCLNFromString($assertion_type_part, $this->aliases);
if ($assertion_type_part !== 'falsy') {
$namespaced_type = Type::parseTokens(
Type::fixUpLocalType(
$assertion_type_part,
$this->aliases,
$this->function_template_types + $this->class_template_types,
$this->type_aliases,
null
)
);
$assertion_type_parts[$i] = $prefix . $namespaced_type->getId();
} else {
$assertion_type_parts[$i] = $prefix . $assertion_type_part;
}

View File

@ -908,6 +908,54 @@ class AssertTest extends TestCase
if (!is_int($a)) $a->bar();',
],
'assertUnionInNamespace' => [
'<?php
namespace Foo\Bar\Baz;
/**
* @psalm-template ExpectedType of object
* @param mixed $value
* @psalm-param class-string<ExpectedType> $interface
* @psalm-assert ExpectedType|class-string<ExpectedType> $value
*/
function implementsInterface($value, $interface, string $message = ""): void {}
/**
* @psalm-template ExpectedType of object
* @param mixed $value
* @psalm-param class-string<ExpectedType> $interface
* @psalm-assert null|ExpectedType|class-string<ExpectedType> $value
*/
function nullOrImplementsInterface(?object $value, $interface, string $message = ""): void {}
interface A
{
}
/**
* @param mixed $value
*
* @psalm-return A|class-string<A>
*/
function consume($value) {
implementsInterface($value, A::class);
return $value;
}
/**
* @param mixed $value
*
* @psalm-return A|class-string<A>|null
*/
function consume2($value)
{
nullOrImplementsInterface($value, A::class);
return $value;
}'
],
];
}