|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Reflection; |
| 4 | + |
| 5 | +use NativeUnionTypes\Foo; |
| 6 | +use PhpParser\Node\Name; |
| 7 | +use PHPStan\Testing\TestCase; |
| 8 | +use PHPStan\Type\UnionType; |
| 9 | +use PHPStan\Type\VerbosityLevel; |
| 10 | + |
| 11 | +class UnionTypesTest extends TestCase |
| 12 | +{ |
| 13 | + |
| 14 | + public function testUnionTypes(): void |
| 15 | + { |
| 16 | + if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) { |
| 17 | + $this->markTestSkipped('Test requires PHP 8.0'); |
| 18 | + } |
| 19 | + |
| 20 | + require_once __DIR__ . '/../../../stubs/runtime/ReflectionUnionType.php'; |
| 21 | + |
| 22 | + $reflectionProvider = $this->createBroker(); |
| 23 | + $class = $reflectionProvider->getClass(Foo::class); |
| 24 | + $propertyType = $class->getNativeProperty('fooProp')->getNativeType(); |
| 25 | + $this->assertInstanceOf(UnionType::class, $propertyType); |
| 26 | + $this->assertSame('bool|int', $propertyType->describe(VerbosityLevel::precise())); |
| 27 | + |
| 28 | + $method = $class->getNativeMethod('doFoo'); |
| 29 | + $methodVariant = ParametersAcceptorSelector::selectSingle($method->getVariants()); |
| 30 | + $methodReturnType = $methodVariant->getReturnType(); |
| 31 | + $this->assertInstanceOf(UnionType::class, $methodReturnType); |
| 32 | + $this->assertSame('NativeUnionTypes\\Bar|NativeUnionTypes\\Foo', $methodReturnType->describe(VerbosityLevel::precise())); |
| 33 | + |
| 34 | + $methodParameterType = $methodVariant->getParameters()[0]->getType(); |
| 35 | + $this->assertInstanceOf(UnionType::class, $methodParameterType); |
| 36 | + $this->assertSame('bool|int', $methodParameterType->describe(VerbosityLevel::precise())); |
| 37 | + |
| 38 | + $function = $reflectionProvider->getFunction(new Name('NativeUnionTypes\doFoo'), null); |
| 39 | + $functionVariant = ParametersAcceptorSelector::selectSingle($function->getVariants()); |
| 40 | + $functionReturnType = $functionVariant->getReturnType(); |
| 41 | + $this->assertInstanceOf(UnionType::class, $functionReturnType); |
| 42 | + $this->assertSame('NativeUnionTypes\\Bar|NativeUnionTypes\\Foo', $functionReturnType->describe(VerbosityLevel::precise())); |
| 43 | + |
| 44 | + $functionParameterType = $functionVariant->getParameters()[0]->getType(); |
| 45 | + $this->assertInstanceOf(UnionType::class, $functionParameterType); |
| 46 | + $this->assertSame('bool|int', $functionParameterType->describe(VerbosityLevel::precise())); |
| 47 | + } |
| 48 | + |
| 49 | +} |
0 commit comments