-
Notifications
You must be signed in to change notification settings - Fork 457
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b9c29a5
commit 39f8909
Showing
7 changed files
with
132 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
if (class_exists('ReflectionUnionType', false)) { | ||
return; | ||
} | ||
|
||
class ReflectionUnionType extends ReflectionType | ||
{ | ||
|
||
/** @return ReflectionType[] */ | ||
public function getTypes() | ||
{ | ||
return []; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Reflection; | ||
|
||
use NativeUnionTypes\Foo; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Testing\TestCase; | ||
use PHPStan\Type\UnionType; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
class UnionTypesTest extends TestCase | ||
{ | ||
|
||
public function testUnionTypes(): void | ||
{ | ||
if (PHP_VERSION_ID < 80000 && !self::$useStaticReflectionProvider) { | ||
$this->markTestSkipped('Test requires PHP 8.0'); | ||
} | ||
|
||
require_once __DIR__ . '/../../../stubs/runtime/ReflectionUnionType.php'; | ||
|
||
$reflectionProvider = $this->createBroker(); | ||
$class = $reflectionProvider->getClass(Foo::class); | ||
$propertyType = $class->getNativeProperty('fooProp')->getNativeType(); | ||
$this->assertInstanceOf(UnionType::class, $propertyType); | ||
$this->assertSame('bool|int', $propertyType->describe(VerbosityLevel::precise())); | ||
|
||
$method = $class->getNativeMethod('doFoo'); | ||
$methodVariant = ParametersAcceptorSelector::selectSingle($method->getVariants()); | ||
$methodReturnType = $methodVariant->getReturnType(); | ||
$this->assertInstanceOf(UnionType::class, $methodReturnType); | ||
$this->assertSame('NativeUnionTypes\\Bar|NativeUnionTypes\\Foo', $methodReturnType->describe(VerbosityLevel::precise())); | ||
|
||
$methodParameterType = $methodVariant->getParameters()[0]->getType(); | ||
$this->assertInstanceOf(UnionType::class, $methodParameterType); | ||
$this->assertSame('bool|int', $methodParameterType->describe(VerbosityLevel::precise())); | ||
|
||
$function = $reflectionProvider->getFunction(new Name('NativeUnionTypes\doFoo'), null); | ||
$functionVariant = ParametersAcceptorSelector::selectSingle($function->getVariants()); | ||
$functionReturnType = $functionVariant->getReturnType(); | ||
$this->assertInstanceOf(UnionType::class, $functionReturnType); | ||
$this->assertSame('NativeUnionTypes\\Bar|NativeUnionTypes\\Foo', $functionReturnType->describe(VerbosityLevel::precise())); | ||
|
||
$functionParameterType = $functionVariant->getParameters()[0]->getType(); | ||
$this->assertInstanceOf(UnionType::class, $functionParameterType); | ||
$this->assertSame('bool|int', $functionParameterType->describe(VerbosityLevel::precise())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php // lint >= 8.0 | ||
|
||
namespace NativeUnionTypes; | ||
|
||
use function PHPStan\Analyser\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
public int|bool $fooProp; | ||
|
||
public function doFoo(int|bool $foo): self|Bar | ||
{ | ||
assertType('bool|int', $foo); | ||
assertType('bool|int', $this->fooProp); | ||
} | ||
|
||
} | ||
|
||
class Bar | ||
{ | ||
|
||
} | ||
|
||
function doFoo(int|bool $foo): Foo|Bar | ||
{ | ||
assertType('bool|int', $foo); | ||
} | ||
|
||
function (Foo $foo): void { | ||
assertType('bool|int', $foo->fooProp); | ||
assertType('NativeUnionTypes\\Bar|NativeUnionTypes\\Foo', $foo->doFoo(1)); | ||
assertType('NativeUnionTypes\\Bar|NativeUnionTypes\\Foo', doFoo(1)); | ||
}; | ||
|
||
function (): void { | ||
$f = function (int|bool $foo): Foo|Bar { | ||
assertType('bool|int', $foo); | ||
}; | ||
|
||
assertType('NativeUnionTypes\\Bar|NativeUnionTypes\\Foo', $f(1)); | ||
}; |