Bug #81611 [Com]: ArgumentCountError when getting default value from ReflectionParameter with new

From: Date: Fri, 12 Nov 2021 17:37:28 +0000
Subject: Bug #81611 [Com]: ArgumentCountError when getting default value from ReflectionParameter with new
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-237736@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=81611&edit=1

 ID:                 81611
 Comment by:         camporter1 at gmail dot com
 Reported by:        ondrej at mirtes dot cz
 Summary:            ArgumentCountError when getting default value from
                     ReflectionParameter with new
 Status:             Open
 Type:               Bug
 Package:            Reflection related
 Operating System:   N/A
 PHP Version:        8.1.0RC5
 Block user comment: N
 Private report:     N

 New Comment:

@nikic Sure, I'll have one ready in a few hours.


Previous Comments:
------------------------------------------------------------------------
[2021-11-12 09:33:14] nikic@php.net

@camporter1: Your patch looks reasonable to me, would you like to submit a PR?

------------------------------------------------------------------------
[2021-11-12 03:27:48] camporter1 at gmail dot com

The issue seems to be that the scope is not correct when the class is determined when evaluating new
self().

A hacky patch would be https://gist.github.com/camporter/0fe6df7909c2473323f6892ced78fb62
which allows zend_ast_fetch_class to provide the scope it was given rather than relying on the
current executed scope.

------------------------------------------------------------------------
[2021-11-11 08:44:09] ondrej at mirtes dot cz

Description:
------------
Trying to get a default value from ReflectionParameter when the default value contains
"new" leads to "Uncaught ArgumentCountError: ReflectionParameter::__construct()
expects exactly 2 arguments, 0 given" from an internal function.

Test script:
---------------
<?php // lint >= 8.1

namespace NewInInitializers;

class Foo
{

	public function doFoo(
		object $test = new self()
	): object
	{
		return $test;
	}

}

class Bar extends Foo
{

}

$ref = new \ReflectionClass(Foo::class);
$params = $ref->getMethod('doFoo')->getParameters();
foreach ($params as $param) {
    var_dump($param->getDefaultValue());
}

Expected result:
----------------
object(NewInInitializers\Foo)#1 (0) {
}

Actual result:
--------------
Fatal error: Uncaught ArgumentCountError: ReflectionParameter::__construct() expects exactly 2
arguments, 0 given in /in/aJ2qI:25
Stack trace:
#0 [internal function]: ReflectionParameter->__construct()
#1 /in/aJ2qI(25): ReflectionParameter->getDefaultValue()
#2 {main}
  thrown in /in/aJ2qI on line 25

Process exited with code 255.


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=81611&edit=1


Thread (6 messages)

« previous php.bugs (#237736) next »