Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reflection] Update return types in some reflection methods #2694

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions resources/functionMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -9993,7 +9993,7 @@
'ReflectionFunctionAbstract::getNumberOfParameters' => ['int'],
'ReflectionFunctionAbstract::getNumberOfRequiredParameters' => ['int'],
'ReflectionFunctionAbstract::getParameters' => ['list<ReflectionParameter>'],
'ReflectionFunctionAbstract::getReturnType' => ['?ReflectionType'],
'ReflectionFunctionAbstract::getReturnType' => ['?ReflectionNamedType'],
'ReflectionFunctionAbstract::getShortName' => ['string'],
'ReflectionFunctionAbstract::getStartLine' => ['int|false'],
'ReflectionFunctionAbstract::getStaticVariables' => ['array'],
Expand Down Expand Up @@ -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'],
Expand Down
2 changes: 1 addition & 1 deletion resources/functionMap_php74delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
'preg_replace_callback' => ['string|array|null', 'regex'=>'string|array', 'callback'=>'callable(array<int|string, string>):string', 'subject'=>'string|array', 'limit='=>'int', '&w_count='=>'int', 'flags='=>'int'],
'preg_replace_callback_array' => ['string|array|null', 'pattern'=>'array<string,callable>', 'subject'=>'string|array', 'limit='=>'int', '&w_count='=>'int', 'flags='=>'int'],
'sapi_windows_set_ctrl_handler' => ['bool', 'callable'=>'callable(int):void', 'add='=>'bool'],
'ReflectionProperty::getType' => ['?ReflectionType'],
'ReflectionProperty::getType' => ['?ReflectionNamedType'],
'ReflectionProperty::hasType' => ['bool'],
'ReflectionProperty::isInitialized' => ['bool', 'object='=>'?object'],
'ReflectionReference::fromArrayElement' => ['?ReflectionReference', 'array'=>'array', 'key'=>'int|string'],
Expand Down
6 changes: 6 additions & 0 deletions resources/functionMap_php80delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
'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'],
'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'],
'socket_addrinfo_lookup' => ['AddressInfo[]', 'node'=>'string', 'service='=>'mixed', 'hints='=>'array'],
'socket_select' => ['int|false', '&rw_read'=>'Socket[]|null', '&rw_write'=>'Socket[]|null', '&rw_except'=>'Socket[]|null', 'seconds'=>'int|null', 'microseconds='=>'int'],
Expand Down Expand Up @@ -230,6 +233,9 @@
'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'],
'ReflectionFunctionAbstract::getReturnType' => ['?ReflectionNamedType'],
'ReflectionParameter::getType' => ['?ReflectionNamedType'],
'ReflectionProperty::getType' => ['?ReflectionNamedType'],
'restore_include_path' => ['void'],
'socket_select' => ['int|false', '&rw_read_fds'=>'resource[]|null', '&rw_write_fds'=>'resource[]|null', '&rw_except_fds'=>'resource[]|null', 'tv_sec'=>'int|null', 'tv_usec='=>'int|null'],
'sodium_crypto_aead_chacha20poly1305_ietf_decrypt' => ['?string|?false', 'confidential_message'=>'string', 'public_message'=>'string', 'nonce'=>'string', 'key'=>'string'],
Expand Down
7 changes: 6 additions & 1 deletion resources/functionMap_php81delta.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
*/
return [
'new' => [

'ReflectionFunctionAbstract::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null'],
'ReflectionParameter::getType' => ['ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null'],
'ReflectionProperty::getType' => ['ReflectionNamedType|ReflectionUnionType|ReflectionIntersectionType|null'],
],
'old' => [
'pg_escape_bytea' => ['string', 'connection'=>'resource', 'data'=>'string'],
Expand Down Expand Up @@ -64,5 +66,8 @@
'pg_tty\'1' => ['string'],
'pg_untrace' => ['bool', 'connection='=>'resource'],
'pg_untrace\'1' => ['bool'],
'ReflectionFunctionAbstract::getReturnType' => ['ReflectionNamedType|ReflectionUnionType|null'],
'ReflectionParameter::getType' => ['ReflectionNamedType|ReflectionUnionType|null'],
'ReflectionProperty::getType' => ['ReflectionNamedType|ReflectionUnionType|null'],
]
];
7 changes: 6 additions & 1 deletion stubs/ReflectionMethod.stub
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@

class ReflectionMethod
{

/**
* @var class-string
*/
public $class;

/**
* @return ReflectionIntersectionType|ReflectionNamedType|ReflectionUnionType|null
*/
public function getReturnType()

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / Compiler Tests

Method ReflectionMethod::getReturnType() has invalid return type ReflectionIntersectionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionIntersectionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / PHPStan (7.3, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionUnionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionIntersectionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionIntersectionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionUnionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / Tests (7.4, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionIntersectionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / Tests (7.4, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionUnionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / Tests (8.0, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionIntersectionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionIntersectionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / PHPStan (7.2, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionUnionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / Tests (7.3, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionIntersectionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / Tests (7.3, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionUnionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionIntersectionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / Tests with old PHPUnit (7.2, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionIntersectionType.

Check failure on line 13 in stubs/ReflectionMethod.stub

View workflow job for this annotation

GitHub Actions / Tests with old PHPUnit (7.2, ubuntu-latest)

Method ReflectionMethod::getReturnType() has invalid return type ReflectionUnionType.
{
}
}
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
44 changes: 44 additions & 0 deletions tests/PHPStan/Reflection/data/reflectionNamedType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php declare(strict_types = 1); // lint >= 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()
);
49 changes: 49 additions & 0 deletions tests/PHPStan/Reflection/data/reflectionNamedType74.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1); // lint >= 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()
);
49 changes: 49 additions & 0 deletions tests/PHPStan/Reflection/data/reflectionNamedType80.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1); // lint >= 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()
);
49 changes: 49 additions & 0 deletions tests/PHPStan/Reflection/data/reflectionNamedType81.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php declare(strict_types = 1); // lint >= 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()
);
Loading