Skip to content

Commit

Permalink
[Php80] Handle parent with typed param on AddParamBasedOnParentClassM…
Browse files Browse the repository at this point in the history
…ethodRector (#1455)

Co-authored-by: Tomas Votruba <tomas.vot@gmail.com>
Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
3 people committed Dec 11, 2021
1 parent 4bb7ad6 commit 7fbf604
Show file tree
Hide file tree
Showing 3 changed files with 55 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\ParentWithTypedParam;

class B extends ParentWithTypedParam{
public function execute()
{
}
}

?>
-----
<?php

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

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

class B extends ParentWithTypedParam{
public function execute(int $foo)
{
}
}

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

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

class ParentWithTypedParam
{
public function execute(int $foo)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Rector\Php80\Rector\ClassMethod;

use PhpParser\Node;
use PhpParser\Node\ComplexType;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
Expand All @@ -16,6 +18,7 @@
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\MethodName;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\VendorLocker\ParentClassMethodTypeOverrideGuard;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand Down Expand Up @@ -208,10 +211,12 @@ function (Node $subNode) use ($parentClassMethodParam): bool {
}

$paramName = $this->nodeNameResolver->getName($parentClassMethodParam);
$paramType = $this->resolveParamType($parentClassMethodParam);

$node->params[$key] = new Param(
new Variable($paramName),
$paramDefault,
$parentClassMethodParam->type,
$paramType,
$parentClassMethodParam->byRef,
$parentClassMethodParam->variadic,
[],
Expand All @@ -223,6 +228,18 @@ function (Node $subNode) use ($parentClassMethodParam): bool {
return $node;
}

private function resolveParamType(Param $param): null|Identifier|Name|ComplexType
{
if ($param->type === null) {
return null;
}

$paramType = $param->type;
$paramType->setAttribute(AttributeKey::ORIGINAL_NODE, null);

return $paramType;
}

/**
* @return string[]
*/
Expand Down

0 comments on commit 7fbf604

Please sign in to comment.