Skip to content

Commit

Permalink
Merge pull request #8834 from danog/fix_class_string_unions
Browse files Browse the repository at this point in the history
Fix parsing of class string of unions
  • Loading branch information
orklah committed Dec 4, 2022
2 parents f3e1a2c + 4c27705 commit cc9c67d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/Psalm/Internal/Type/TypeParser.php
Expand Up @@ -711,17 +711,16 @@ private static function getTypeFromGenericTree(
);
}

$param_union_types = array_values($generic_params[0]->getAtomicTypes());

if (count($param_union_types) > 1) {
throw new TypeParseTreeException('Union types are not allowed in class string param');
}
$types = [];
foreach ($generic_params[0]->getAtomicTypes() as $type) {
if (!$type instanceof TNamedObject) {
throw new TypeParseTreeException('Class string param should be a named object');
}

if (!$param_union_types[0] instanceof TNamedObject) {
throw new TypeParseTreeException('Class string param should be a named object');
$types []= new TClassString($type->value, $type, false, false, false, $from_docblock);
}

return new TClassString($class_name, $param_union_types[0], false, false, false, $from_docblock);
return new Union($types);
}

if ($generic_type_value === 'class-string-map') {
Expand Down
20 changes: 20 additions & 0 deletions tests/ClassLikeStringTest.php
Expand Up @@ -859,6 +859,26 @@ function foo(A $a): void {
if (get_class($a) === A::class) {}
}',
],
'classStringUnion' => [
'code' => '<?php
class Foo
{
/** @var class-string<TypeOne>|class-string<TypeTwo> */
public ?string $bar = null;
/** @var class-string<TypeOne|TypeTwo> */
public ?string $baz = null;
}
class TypeOne {}
class TypeTwo {}
$foo = new Foo;
$foo->bar = TypeOne::class;
$foo->bar = TypeOne::class;
$foo->baz = TypeTwo::class;
$foo->baz = TypeTwo::class;'
]
];
}

Expand Down

0 comments on commit cc9c67d

Please sign in to comment.