Skip to content

Commit

Permalink
[Php80] Handle param with default value on AddParamBasedOnParentClass…
Browse files Browse the repository at this point in the history
…MethodRector (#1452)

* Add source with default value

* Add fixture for default value

* Rename fixture

* Fixes #1450

Co-authored-by: Ole Schäfer <ole@customgento.com>
  • Loading branch information
samsonasik and norgeindian authored Dec 10, 2021
1 parent b4fabbd commit 8d6eb87
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParamWithDefaultValue;

class IncludeDefaultParamValue extends ParentWithParamWithDefaultValue{
public function execute()
{
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Fixture;

use Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source\ParentWithParamWithDefaultValue;

class IncludeDefaultParamValue extends ParentWithParamWithDefaultValue{
public function execute($foo = true)
{
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethodRector\Source;

class ParentWithParamWithDefaultValue
{
public function execute($foo = true)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
namespace Rector\Php80\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PHPStan\Reflection\MethodReflection;
Expand Down Expand Up @@ -160,10 +163,17 @@ function (Node $subNode) use ($parentClassMethodParam): bool {
return null;
}

$paramDefault = $parentClassMethodParam->default;

if ($paramDefault instanceof Expr) {
$printParamDefault = $this->print($paramDefault);
$paramDefault = new ConstFetch(new Name($printParamDefault));
}

$paramName = $this->nodeNameResolver->getName($parentClassMethodParam);
$node->params[$key] = new Param(
new Variable($paramName),
$parentClassMethodParam->default,
$paramDefault,
$parentClassMethodParam->type,
$parentClassMethodParam->byRef,
$parentClassMethodParam->variadic,
Expand Down

0 comments on commit 8d6eb87

Please sign in to comment.