Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions stubs/ReflectionFunctionAbstract.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}

}
8 changes: 8 additions & 0 deletions stubs/ReflectionParameter.stub
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
}

}
9 changes: 9 additions & 0 deletions stubs/ReflectionProperty.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,19 @@

class ReflectionProperty
{

/**
* @return list<ReflectionAttribute<object>>
*/
public function getAttributes(?string $name = null, int $flags = 0)
{
}

/**
* @phpstan-assert-if-true !null $this->getType()
*/
public function hasType(): bool
{
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
33 changes: 33 additions & 0 deletions tests/PHPStan/Analyser/data/reflection-type-php7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace ReflectionTypeTestPhp7;

use function PHPStan\Testing\assertType;

function test(
\ReflectionProperty $reflectionProperty,
\ReflectionFunctionAbstract $reflectionFunctionAbstract,
\ReflectionParameter $reflectionParameter
){
assertType('ReflectionType|null', $reflectionProperty->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());
}
}
40 changes: 40 additions & 0 deletions tests/PHPStan/Analyser/data/reflection-type-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace ReflectionTypeTestPhp8;

use function PHPStan\Testing\assertType;

function test(
\ReflectionProperty $reflectionProperty,
\ReflectionFunctionAbstract $reflectionFunctionAbstract,
\ReflectionParameter $reflectionParameter
){
assertType('ReflectionType|null', $reflectionProperty->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());
}
}
15 changes: 0 additions & 15 deletions tests/PHPStan/Analyser/nsrt/reflection-type.php

This file was deleted.

Loading