1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Allow complex types in conditional is expressions

Ref #3277
This commit is contained in:
Brown 2020-04-30 22:02:37 -04:00
parent 8ab5a0f504
commit ce8fb459e9
2 changed files with 37 additions and 1 deletions

View File

@ -391,7 +391,10 @@ class ParseTree
case '?':
if ($next_token === null || $next_token[0] !== ':') {
while (($current_leaf instanceof ParseTree\Value
|| $current_leaf instanceof ParseTree\UnionTree)
|| $current_leaf instanceof ParseTree\UnionTree
|| $current_leaf instanceof ParseTree\ObjectLikeTree
|| $current_leaf instanceof ParseTree\GenericTree
|| $current_leaf instanceof ParseTree\IntersectionTree)
&& $current_leaf->parent
) {
$current_leaf = $current_leaf->parent;

View File

@ -659,6 +659,39 @@ class TypeParseTest extends TestCase
);
}
/**
* @return void
*/
public function testConditionalTypeWithObjectLikeArray()
{
$this->assertSame(
'(T is array{a: string} ? string : int)',
(string) Type::parseString('(T is array{a: string} ? string : int)', null, ['T' => ['' => [Type::getArray()]]])
);
}
/**
* @return void
*/
public function testConditionalTypeWithGeneric()
{
$this->assertSame(
'(T is array<array-key, string> ? string : int)',
(string) Type::parseString('(T is array<string> ? string : int)', null, ['T' => ['' => [Type::getArray()]]])
);
}
/**
* @return void
*/
public function testConditionalTypeWithIntersection()
{
$this->assertSame(
'(T is A&B ? string : int)',
(string) Type::parseString('(T is A&B ? string : int)', null, ['T' => ['' => [Type::getArray()]]])
);
}
/**
* @return void
*/