Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure that simple union types like
A|null
yield a nullable `Reflec…
…tionNamedType` Fixes #901 ## Context Currently, when running reflection on an `A|null` type, BetterReflection produces a `Roave\BetterReflection\Reflection\ReflectionUnionType`: ```php var_dump( get_class( (new DefaultReflector(new StringSourceLocator( <<<'PHP' <?php interface A {} final class AClass { private A|null $typed; } PHP , (new BetterReflection())->astLocator() ))) ->reflectClass('AClass') ->getProperty('typed') ->getType() ) ); ``` produces ``` string(53) "Roave\BetterReflection\Reflection\ReflectionUnionType" ``` In PHP-SRC, this behavior is different: https://3v4l.org/gMA4T#v8.1rc3 ```php <?php interface A {} interface B {} class Implementation { function foo(A|null $param) { throw new Exception(); } function bar(A|B|null $param) { throw new Exception(); } } var_dump((new ReflectionParameter([Implementation::class, 'foo'], 0))->getType()); var_dump((new ReflectionParameter([Implementation::class, 'bar'], 0))->getType()); ``` produces: ``` object(ReflectionNamedType)#2 (0) { } object(ReflectionUnionType)#1 (0) { } ``` This means that a `UnionType` AST node composed of just `null` plus another type should be converted into a `ReflectionNamedType`, for the sake of compatibility with upstream (this patch does that). This is ugly, but will (for now) avoid some bad issues in downstream handling (presently blocking Roave/BackwardCompatibilityCheck#324 )
- Loading branch information