diff --git a/stubs/ReflectionFunctionAbstract.stub b/stubs/ReflectionFunctionAbstract.stub index 0154996741..da1a6edc18 100644 --- a/stubs/ReflectionFunctionAbstract.stub +++ b/stubs/ReflectionFunctionAbstract.stub @@ -14,4 +14,19 @@ abstract class ReflectionFunctionAbstract public function getAttributes(?string $name = null, int $flags = 0) { } + + /** + * @phpstan-assert-if-true !null $this->getReturnType() + */ + public function hasReturnType(): bool + { + } + + /** + * @phpstan-assert-if-true !null $this->getTentativeReturnType() + */ + public function hasTentativeReturnType(): bool + { + } + } diff --git a/stubs/ReflectionParameter.stub b/stubs/ReflectionParameter.stub index e92f0b51f3..12e7c1467e 100644 --- a/stubs/ReflectionParameter.stub +++ b/stubs/ReflectionParameter.stub @@ -8,4 +8,12 @@ class ReflectionParameter public function getAttributes(?string $name = null, int $flags = 0) { } + + /** + * @phpstan-assert-if-true !null $this->getType() + */ + public function hasType(): bool + { + } + } diff --git a/stubs/ReflectionProperty.stub b/stubs/ReflectionProperty.stub index 002daeeeee..4cfb92d0db 100644 --- a/stubs/ReflectionProperty.stub +++ b/stubs/ReflectionProperty.stub @@ -2,10 +2,19 @@ class ReflectionProperty { + /** * @return list> */ public function getAttributes(?string $name = null, int $flags = 0) { } + + /** + * @phpstan-assert-if-true !null $this->getType() + */ + public function hasType(): bool + { + } + } diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 6fa3e65e19..559468dfba 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -77,6 +77,12 @@ private static function findTestFiles(): iterable yield __DIR__ . '/data/explode-php80.php'; } + if (PHP_VERSION_ID >= 80000) { + yield __DIR__ . '/data/reflection-type-php8.php'; + } else { + yield __DIR__ . '/data/reflection-type-php7.php'; + } + if (PHP_VERSION_ID >= 80000) { yield __DIR__ . '/../Reflection/data/unionTypes.php'; yield __DIR__ . '/../Reflection/data/mixedType.php'; diff --git a/tests/PHPStan/Analyser/data/reflection-type-php7.php b/tests/PHPStan/Analyser/data/reflection-type-php7.php new file mode 100644 index 0000000000..98030a142a --- /dev/null +++ b/tests/PHPStan/Analyser/data/reflection-type-php7.php @@ -0,0 +1,33 @@ +getType()); + assertType('ReflectionType|null', $reflectionFunctionAbstract->getReturnType()); + assertType('ReflectionType|null', $reflectionParameter->getType()); + + if ($reflectionProperty->hasType()) { + assertType('ReflectionType', $reflectionProperty->getType()); + } else { + assertType('null', $reflectionProperty->getType()); + } + + if ($reflectionFunctionAbstract->hasReturnType()) { + assertType('ReflectionType', $reflectionFunctionAbstract->getReturnType()); + } else { + assertType('null', $reflectionFunctionAbstract->getReturnType()); + } + + if ($reflectionParameter->hasType()) { + assertType('ReflectionType', $reflectionParameter->getType()); + } else { + assertType('null', $reflectionParameter->getType()); + } +} diff --git a/tests/PHPStan/Analyser/data/reflection-type-php8.php b/tests/PHPStan/Analyser/data/reflection-type-php8.php new file mode 100644 index 0000000000..b809e5bdc4 --- /dev/null +++ b/tests/PHPStan/Analyser/data/reflection-type-php8.php @@ -0,0 +1,40 @@ +getType()); + assertType('ReflectionType|null', $reflectionFunctionAbstract->getReturnType()); + assertType('ReflectionType|null', $reflectionFunctionAbstract->getTentativeReturnType()); + assertType('ReflectionType|null', $reflectionParameter->getType()); + + if ($reflectionProperty->hasType()) { + assertType('ReflectionType', $reflectionProperty->getType()); + } else { + assertType('null', $reflectionProperty->getType()); + } + + if ($reflectionFunctionAbstract->hasReturnType()) { + assertType('ReflectionType', $reflectionFunctionAbstract->getReturnType()); + } else { + assertType('null', $reflectionFunctionAbstract->getReturnType()); + } + + if ($reflectionFunctionAbstract->hasTentativeReturnType()) { + assertType('ReflectionType', $reflectionFunctionAbstract->getTentativeReturnType()); + } else { + assertType('null', $reflectionFunctionAbstract->getTentativeReturnType()); + } + + if ($reflectionParameter->hasType()) { + assertType('ReflectionType', $reflectionParameter->getType()); + } else { + assertType('null', $reflectionParameter->getType()); + } +} diff --git a/tests/PHPStan/Analyser/nsrt/reflection-type.php b/tests/PHPStan/Analyser/nsrt/reflection-type.php deleted file mode 100644 index d390747fe6..0000000000 --- a/tests/PHPStan/Analyser/nsrt/reflection-type.php +++ /dev/null @@ -1,15 +0,0 @@ -getType()); - assertType('ReflectionType|null', $reflectionFunctionAbstract->getReturnType()); - assertType('ReflectionType|null', $reflectionParameter->getType()); -}