Add stub to ReflectionObject class#11774
Conversation
|
I found these snippets: https://psalm.dev/r/6f73e8f1c5<?php
declare(strict_types=1);
/** @psalm-check-type $refClass = \ReflectionClass<\DateTimeImmutable> */
$refClass = new \ReflectionClass(\DateTimeImmutable::class);
/** @psalm-check-type $obj = \DateTimeImmutable */
$obj = $refClass->newInstanceWithoutConstructor();https://psalm.dev/r/d91a57ae92<?php
declare(strict_types=1);
/** @psalm-check-type $refClass = \ReflectionClass<\DateTimeImmutable> */
$refClass = new \ReflectionClass(new \DateTimeImmutable());
/** @psalm-check-type $obj = \DateTimeImmutable */
$obj = $refClass->newInstanceWithoutConstructor();https://psalm.dev/r/700c9575b0<?php
declare(strict_types=1);
$refObject = new \ReflectionObject(new \DateTimeImmutable());
/** @psalm-check-type $obj = \DateTimeImmutable */
$obj = $refObject->newInstanceWithoutConstructor(); |
|
Nice addition! What do you think about adding some tests to
yield 'ReflectionObject infers generic type from object' => [
'code' => <<<'PHP'
<?php
$r = new ReflectionObject(new stdClass());
PHP,
'assertions' => ['$r===' => 'ReflectionObject<stdClass>'],
];
yield 'ReflectionObject::getName returns class-string' => [
'code' => <<<'PHP'
<?php
$r = new ReflectionObject(new stdClass());
$name = $r->getName();
PHP,
'assertions' => ['$name===' => "'stdClass'"],
];
yield 'ReflectionObject extends ReflectionClass' => [
'code' => <<<'PHP'
<?php
function foo(ReflectionClass $r): void {}
foo(new ReflectionObject(new stdClass()));
PHP,
];Add the invalid code provider method: /**
* @psalm-mutation-free
*/
#[Override]
public function providerInvalidCodeParse(): iterable
{
yield 'ReflectionObject rejects string argument' => [
'code' => <<<'PHP'
<?php
/** @psalm-suppress UndefinedClass */
new ReflectionObject('stdClass');
PHP,
'error_message' => 'InvalidArgument',
];
}This covers:
|
Fixed according to the official documentation: https://www.php.net/manual/en/reflectionclass.newinstanceargs.php - Set default value empty array of $args argument. - Make return type nullable.
|
I added the tests |
|
I also noticed a discrepancy in the I was guided by official php documentation: https://www.php.net/manual/en/reflectionclass.newinstanceargs.php
I was also fixed and tested it. |
|
Hello! Just writing so this PR doesn't get lost. I'd really like to get this change merged properly. Could you at least briefly let me know: Are you planning to review it anytime soon? Or is there something I should fix right now? I'm totally open to making changes. Thanks for understanding! |
|
Hey @Kenny1911 |
Class
ReflectionClasssupports generic.https://psalm.dev/r/6f73e8f1c5
https://psalm.dev/r/d91a57ae92
But
ReflectionObject, that extendsReflectionClassdoesn't it.https://psalm.dev/r/700c9575b0
I am add stub of
ReflectionObject, that supports generic.