When using AddParamBasedOnParentClassMethodRector with a parent class having properties and a new optional method parameter, a system error occurres with the following stack trace:
use Rector\Config\RectorConfig;
use Rector\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([
DIR . '/TestClass.php',
DIR . '/ExtendingTestClass.php',
]);
// register a single rule
$rectorConfig->rule(AddParamBasedOnParentClassMethodRector::class);
};
* TestClass.php
<?php
class TestClass {
private string $string1;
private string $string2;
public function test(?string $string3 = '')
{
}
}
* ExtendingTestClass.php
<?php
class ExtendingTestClass extends TestClass {
public function test()
{
}
}
## Expected Behaviour
ExtendingTestClass.php updated content is
<?php
class ExtendingTestClass extends TestClass {
public function test(?string $string3 = '')
{
}
}
## Additional information
* Only reproducible with two different files for classes
* Removing the property `private string $string2;` from `TestClass` results in
`ExtendingTestClass.php` having invalid code:
<?php
class ExtendingTestClass extends TestClass {
public function test(?} $string3 = '')
{
}
}
* EDIT: removed 'static' keywords from code samples (reproducible with static and non-static method calls).
Bug Report
When using
AddParamBasedOnParentClassMethodRector
with a parent class having properties and a new optional method parameter, a system error occurres with the following stack trace:Minimal PHP Code Causing Issue
use Rector\Config\RectorConfig; use Rector\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector;
return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ DIR . '/TestClass.php', DIR . '/ExtendingTestClass.php', ]);
};
<?php
class TestClass { private string $string1; private string $string2;
}
<?php
class ExtendingTestClass extends TestClass { public function test() { } }
<?php
class ExtendingTestClass extends TestClass { public function test(?string $string3 = '') { } }
<?php
class ExtendingTestClass extends TestClass { public function test(?} $string3 = '') { } }