From 2b2af4e9d5d9faa0b75ea82142bccadb59713f7d Mon Sep 17 00:00:00 2001 From: Javier Spagnoletti Date: Tue, 24 Oct 2023 12:31:36 -0300 Subject: [PATCH] Update stubs and tests --- resources/functionMap.php | 4 +- resources/functionMap_php80delta.php | 6 +-- resources/functionMap_php81delta.php | 6 +-- stubs/ReflectionMethod.stub | 7 ++- .../Analyser/NodeScopeResolverTest.php | 10 ++++ .../Reflection/data/reflectionNamedType.php | 44 +++++++++++++++++ .../Reflection/data/reflectionNamedType74.php | 49 +++++++++++++++++++ .../Reflection/data/reflectionNamedType80.php | 49 +++++++++++++++++++ .../Reflection/data/reflectionNamedType81.php | 49 +++++++++++++++++++ 9 files changed, 213 insertions(+), 11 deletions(-) create mode 100644 tests/PHPStan/Reflection/data/reflectionNamedType.php create mode 100644 tests/PHPStan/Reflection/data/reflectionNamedType74.php create mode 100644 tests/PHPStan/Reflection/data/reflectionNamedType80.php create mode 100644 tests/PHPStan/Reflection/data/reflectionNamedType81.php diff --git a/resources/functionMap.php b/resources/functionMap.php index d5b4e6a7ea..bf44a73a3a 100644 --- a/resources/functionMap.php +++ b/resources/functionMap.php @@ -9993,7 +9993,7 @@ 'ReflectionFunctionAbstract::getNumberOfParameters' => ['int'], 'ReflectionFunctionAbstract::getNumberOfRequiredParameters' => ['int'], 'ReflectionFunctionAbstract::getParameters' => ['list'], -'ReflectionFunctionAbstract::getReturnType' => ['?ReflectionType'], +'ReflectionFunctionAbstract::getReturnType' => ['?ReflectionNamedType'], 'ReflectionFunctionAbstract::getShortName' => ['string'], 'ReflectionFunctionAbstract::getStartLine' => ['int|false'], 'ReflectionFunctionAbstract::getStaticVariables' => ['array'], @@ -10051,7 +10051,7 @@ 'ReflectionParameter::getDefaultValueConstantName' => ['?string'], 'ReflectionParameter::getName' => ['non-empty-string'], 'ReflectionParameter::getPosition' => ['int'], -'ReflectionParameter::getType' => ['ReflectionType|null'], +'ReflectionParameter::getType' => ['ReflectionNamedType|null'], 'ReflectionParameter::hasType' => ['bool'], 'ReflectionParameter::isArray' => ['bool'], 'ReflectionParameter::isCallable' => ['bool'], diff --git a/resources/functionMap_php80delta.php b/resources/functionMap_php80delta.php index 0e52d8d537..095a0dec0c 100644 --- a/resources/functionMap_php80delta.php +++ b/resources/functionMap_php80delta.php @@ -91,8 +91,7 @@ 'PhpToken::getTokenName' => ['string'], 'preg_match_all' => ['0|positive-int|false', 'pattern'=>'string', 'subject'=>'string', '&w_subpatterns='=>'array', 'flags='=>'int', 'offset='=>'int'], 'proc_get_status' => ['array{command: string, pid: int, running: bool, signaled: bool, stopped: bool, exitcode: int, termsig: int, stopsig: int}', 'process'=>'resource'], - 'ReflectionFunction::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|null'], - 'ReflectionMethod::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|null'], + 'ReflectionFunctionAbstract::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|null'], 'ReflectionParameter::getType' => ['ReflectionNamedType|ReflectionUnionType|null'], 'ReflectionProperty::getType' => ['ReflectionNamedType|ReflectionUnionType|null'], 'set_error_handler' => ['?callable', 'callback'=>'null|callable(int,string,string,int):bool', 'error_types='=>'int'], @@ -234,8 +233,7 @@ 'png2wbmp' => ['bool', 'pngname'=>'string', 'wbmpname'=>'string', 'dest_height'=>'int', 'dest_width'=>'int', 'threshold'=>'int'], 'proc_get_status' => ['array{command: string, pid: int, running: bool, signaled: bool, stopped: bool, exitcode: int, termsig: int, stopsig: int}|false', 'process'=>'resource'], 'read_exif_data' => ['array', 'filename'=>'string', 'sections_needed='=>'string', 'sub_arrays='=>'bool', 'read_thumbnail='=>'bool'], - 'ReflectionFunction::getReturnType' => ['?ReflectionNamedType'], - 'ReflectionMethod::getReturnType' => ['?ReflectionNamedType'], + 'ReflectionFunctionAbstract::getReturnType' => ['?ReflectionNamedType'], 'ReflectionParameter::getType' => ['?ReflectionNamedType'], 'ReflectionProperty::getType' => ['?ReflectionNamedType'], 'restore_include_path' => ['void'], diff --git a/resources/functionMap_php81delta.php b/resources/functionMap_php81delta.php index 7fff3018aa..cfd83f8cc3 100644 --- a/resources/functionMap_php81delta.php +++ b/resources/functionMap_php81delta.php @@ -21,8 +21,7 @@ */ return [ 'new' => [ - 'ReflectionFunction::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null'], - 'ReflectionMethod::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null'], + 'ReflectionFunctionAbstract::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null'], 'ReflectionParameter::getType' => ['ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null'], 'ReflectionProperty::getType' => ['ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null'], ], @@ -67,8 +66,7 @@ 'pg_tty\'1' => ['string'], 'pg_untrace' => ['bool', 'connection='=>'resource'], 'pg_untrace\'1' => ['bool'], - 'ReflectionFunction::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|null'], - 'ReflectionMethod::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|null'], + 'ReflectionFunctionAbstract::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|null'], 'ReflectionParameter::getType' => ['ReflectionNamedType|ReflectionUnionType|null'], 'ReflectionProperty::getType' => ['ReflectionNamedType|ReflectionUnionType|null'], ] diff --git a/stubs/ReflectionMethod.stub b/stubs/ReflectionMethod.stub index 8107e50667..66b7000938 100644 --- a/stubs/ReflectionMethod.stub +++ b/stubs/ReflectionMethod.stub @@ -2,10 +2,15 @@ class ReflectionMethod { - /** * @var class-string */ public $class; + /** + * @return ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null + */ + public function getReturnType() + { + } } diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 1a38b8bc73..6e22cfa5d9 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -149,6 +149,16 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/../Reflection/data/staticReturnType.php'); + if (PHP_VERSION_ID < 70400) { + yield from $this->gatherAssertTypes(__DIR__ . '/../Reflection/data/reflectionNamedType.php'); + } elseif (PHP_VERSION_ID < 80000) { + yield from $this->gatherAssertTypes(__DIR__ . '/../Reflection/data/reflectionNamedType74.php'); + } elseif (PHP_VERSION_ID < 80100) { + yield from $this->gatherAssertTypes(__DIR__ . '/../Reflection/data/reflectionNamedType80.php'); + } else { + yield from $this->gatherAssertTypes(__DIR__ . '/../Reflection/data/reflectionNamedType81.php'); + } + yield from $this->gatherAssertTypes(__DIR__ . '/data/minmax.php'); if (PHP_VERSION_ID < 80000) { yield from $this->gatherAssertTypes(__DIR__ . '/data/minmax-arrays.php'); diff --git a/tests/PHPStan/Reflection/data/reflectionNamedType.php b/tests/PHPStan/Reflection/data/reflectionNamedType.php new file mode 100644 index 0000000000..b290f384cf --- /dev/null +++ b/tests/PHPStan/Reflection/data/reflectionNamedType.php @@ -0,0 +1,44 @@ += 8.0 + +namespace Reflection\NamedType; + +use function PHPStan\Testing\assertType; + +final class Foo +{ + private $bar = 'hello'; + + public function baz(bool $tiny): float + { + return 4.2; + } +} + +function qux(bool $switch): array +{ + return []; +} + +$reflectionMethod = new \ReflectionMethod(Foo::class, 'baz'); + +assertType( + 'ReflectionNamedType|null', + $reflectionMethod->getParameters()[0]->getType() +); + +assertType( + 'ReflectionNamedType|null', + $reflectionMethod->getReturnType() +); + +$reflectionFunction = new \ReflectionFunction('qux'); + +assertType( + 'ReflectionNamedType|null', + $reflectionFunction->getParameters()[0]->getType() +); + +assertType( + 'ReflectionNamedType|null', + $reflectionFunction->getReturnType() +); diff --git a/tests/PHPStan/Reflection/data/reflectionNamedType74.php b/tests/PHPStan/Reflection/data/reflectionNamedType74.php new file mode 100644 index 0000000000..8843265c26 --- /dev/null +++ b/tests/PHPStan/Reflection/data/reflectionNamedType74.php @@ -0,0 +1,49 @@ += 8.0 + +namespace Reflection\NamedType; + +use function PHPStan\Testing\assertType; + +final class Foo +{ + private string $bar = 'hello'; + + public function baz(bool $tiny): float + { + return 4.2; + } +} + +function qux(bool $switch): array +{ + return []; +} + +assertType( + 'ReflectionNamedType|null', + (new \ReflectionProperty(Foo::class, 'bar'))->getType() +); + +$reflectionMethod = new \ReflectionMethod(Foo::class, 'baz'); + +assertType( + 'ReflectionNamedType|null', + $reflectionMethod->getParameters()[0]->getType() +); + +assertType( + 'ReflectionNamedType|null', + $reflectionMethod->getReturnType() +); + +$reflectionFunction = new \ReflectionFunction('qux'); + +assertType( + 'ReflectionNamedType|null', + $reflectionFunction->getParameters()[0]->getType() +); + +assertType( + 'ReflectionNamedType|null', + $reflectionFunction->getReturnType() +); diff --git a/tests/PHPStan/Reflection/data/reflectionNamedType80.php b/tests/PHPStan/Reflection/data/reflectionNamedType80.php new file mode 100644 index 0000000000..98844b74ad --- /dev/null +++ b/tests/PHPStan/Reflection/data/reflectionNamedType80.php @@ -0,0 +1,49 @@ += 8.0 + +namespace Reflection\NamedType; + +use function PHPStan\Testing\assertType; + +final class Foo +{ + private string $bar = 'hello'; + + public function baz(int|bool $tiny): float + { + return 4.2; + } +} + +function qux(bool $switch): array +{ + return []; +} + +assertType( + 'ReflectionNamedType|ReflectionUnionType|null', + (new \ReflectionProperty(Foo::class, 'bar'))->getType() +); + +$reflectionMethod = new \ReflectionMethod(Foo::class, 'baz'); + +assertType( + 'ReflectionNamedType|ReflectionUnionType|null', + $reflectionMethod->getParameters()[0]->getType() +); + +assertType( + 'ReflectionNamedType|ReflectionUnionType|null', + $reflectionMethod->getReturnType() +); + +$reflectionFunction = new \ReflectionFunction('qux'); + +assertType( + 'ReflectionNamedType|ReflectionUnionType|null', + $reflectionFunction->getParameters()[0]->getType() +); + +assertType( + 'ReflectionNamedType|ReflectionUnionType|null', + $reflectionFunction->getReturnType() +); diff --git a/tests/PHPStan/Reflection/data/reflectionNamedType81.php b/tests/PHPStan/Reflection/data/reflectionNamedType81.php new file mode 100644 index 0000000000..50cb06eb7c --- /dev/null +++ b/tests/PHPStan/Reflection/data/reflectionNamedType81.php @@ -0,0 +1,49 @@ += 8.0 + +namespace Reflection\NamedType; + +use function PHPStan\Testing\assertType; + +final class Foo +{ + private string $bar = 'hello'; + + public function baz(int|bool $tiny): float + { + return 4.2; + } +} + +function qux(bool $switch): array +{ + return []; +} + +assertType( + 'ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null', + (new \ReflectionProperty(Foo::class, 'bar'))->getType() +); + +$reflectionMethod = new \ReflectionMethod(Foo::class, 'baz'); + +assertType( + 'ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null', + $reflectionMethod->getParameters()[0]->getType() +); + +assertType( + 'ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null', + $reflectionMethod->getReturnType() +); + +$reflectionFunction = new \ReflectionFunction('qux'); + +assertType( + 'ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null', + $reflectionFunction->getParameters()[0]->getType() +); + +assertType( + 'ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null', + $reflectionFunction->getReturnType() +);