Skip to content
Permalink
Browse files

Allow union of class-strings in signature

Ref #1851
  • Loading branch information...
muglug committed Jun 27, 2019
1 parent 3088432 commit ba121a94e7110ba8e25560bc414efa8321108c6a
@@ -877,7 +877,11 @@ private static function scrapeTypeProperties(
return null;
}
if ($type instanceof TLiteralString) {
if ($type instanceof Type\Atomic\TTemplateParamClass) {
$combination->value_types[$type_key] = $type;
} elseif ($type instanceof Type\Atomic\TClassString && $type->as !== 'object') {
$combination->value_types[$type_key] = $type;
} elseif ($type instanceof TLiteralString) {
if ($combination->strings !== null && count($combination->strings) < $literal_limit) {
$combination->strings[$type_key] = $type;
} else {
@@ -59,7 +59,7 @@ public function __toString()
*/
public function getId()
{
return $this->getKey();
return 'class-string<' . $this->param_name . ' as ' . $this->as . '>';
}
/**
@@ -487,6 +487,24 @@ class A {}
bar(foo(A::class));',
],
'templateAsUnionClassStringPassingValidClass' => [
'<?php
class A {}
class B {}
/**
* @template T1 as A
* @template T2 as B
* @param class-string<T1>|class-string<T2> $type
* @return T1|T2
*/
function f(string $type) {
return new $type();
}
f(A::class);
f(B::class);',
],
];
}
@@ -552,6 +570,25 @@ function bar(Traversable $t) : void {
}',
'error_message' => 'MixedArgumentTypeCoercion',
],
'templateAsUnionClassStringPassingInvalidClass' => [
'<?php
class A {}
class B {}
class C {}
/**
* @template T1 as A
* @template T2 as B
* @param class-string<T1>|class-string<T2> $type
* @return T1|T2
*/
function f(string $type) {
return new $type();
}
f(C::class);',
'error_message' => 'InvalidArgument',
],
];
}
}
@@ -291,6 +291,14 @@ public function testParamaterizedClassString()
$this->assertSame('class-string<A>', (string) Type::parseString('class-string<A>'));
}
/**
* @return void
*/
public function testParamaterizedClassStringUnion()
{
$this->assertSame('class-string<A>|class-string<B>', (string) Type::parseString('class-string<A>|class-string<B>'));
}
/**
*
* @return void

0 comments on commit ba121a9

Please sign in to comment.
You can’t perform that action at this time.