Skip to content

Commit

Permalink
[Php80] Add null default value when child more params no default valu…
Browse files Browse the repository at this point in the history
…e AddParamBasedOnParentClassMethodRector (#1454)
  • Loading branch information
samsonasik authored Dec 11, 2021
1 parent f2e58a2 commit fa92ec2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

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

// ref https://3v4l.org/BPFsU
class ChildMoreParamsNoDefaultValue extends ParentWithParam{
public function execute($foo, $bar)
{
}
}

?>
-----
<?php

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

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

// ref https://3v4l.org/BPFsU
class ChildMoreParamsNoDefaultValue extends ParentWithParam{
public function execute($foo, $bar = null)
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ namespace Rector\Tests\Php80\Rector\ClassMethod\AddParamBasedOnParentClassMethod

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

// ref https://3v4l.org/op5lE
class SkipChildMoreParams extends ParentWithParam{
public function execute($foo, $bar)
// ref https://3v4l.org/gOHV0
class SkipChildMoreParamsHasDefaultValue extends ParentWithParam{
public function execute($foo, $bar = false)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,53 @@ public function refactor(Node $node): ?Node
$currentClassMethodParams = $node->getParams();
$parentClassMethodParams = $parentClassMethod->getParams();

if (count($currentClassMethodParams) >= count($parentClassMethodParams)) {
$countCurrentClassMethodParams = count($currentClassMethodParams);
$countParentClassMethodParams = count($parentClassMethodParams);

if ($countCurrentClassMethodParams === $countParentClassMethodParams) {
return null;
}

if ($countCurrentClassMethodParams < $countParentClassMethodParams) {
return $this->processReplaceClassMethodParams(
$node,
$parentClassMethod,
$currentClassMethodParams,
$parentClassMethodParams
);
}

return $this->processAddNullDefaultParam($node, $currentClassMethodParams, $parentClassMethodParams);
}

/**
* @param Param[] $currentClassMethodParams
* @param Param[] $parentClassMethodParams
*/
private function processAddNullDefaultParam(
ClassMethod $classMethod,
array $currentClassMethodParams,
array $parentClassMethodParams
): ?ClassMethod {
$hasChanged = false;
foreach ($currentClassMethodParams as $key => $currentClassMethodParam) {
if (isset($parentClassMethodParams[$key])) {
continue;
}

if ($currentClassMethodParam->default instanceof Expr) {
continue;
}

$currentClassMethodParams[$key]->default = $this->nodeFactory->createNull();
$hasChanged = true;
}

if (! $hasChanged) {
return null;
}

return $this->processReplaceClassMethodParams(
$node,
$parentClassMethod,
$currentClassMethodParams,
$parentClassMethodParams
);
return $classMethod;
}

/**
Expand Down

0 comments on commit fa92ec2

Please sign in to comment.