mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-20 01:52:22 +00:00
[DeadCode] Do not remove used parameter on RemoveUnusedPrivatePropertyRector (#1337)
* [DeadCode] Handle RemoveUnusedPromotedPropertyRector + RemoveUnusedPrivatePropertyRector * rem * move external to Source * eol * fix * eol * [ci-review] Rector Rectify * fix Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
4ad4d16ac6
commit
50bbdd1e93
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;
|
||||
|
||||
use Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Source\AbstractSetting;
|
||||
|
||||
final class DemoSetting extends AbstractSetting
|
||||
{
|
||||
private PageInterface $page;
|
||||
private SectionInterface $section;
|
||||
|
||||
public function __construct(PageInterface $page, SectionInterface $section)
|
||||
{
|
||||
$this->page = $page;
|
||||
$this->section = $section;
|
||||
|
||||
parent::__construct($page, $section);
|
||||
}
|
||||
|
||||
public function getPage()
|
||||
{
|
||||
return $this->page;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;
|
||||
|
||||
use Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Source\AbstractSetting;
|
||||
|
||||
final class DemoSetting extends AbstractSetting
|
||||
{
|
||||
private PageInterface $page;
|
||||
|
||||
public function __construct(PageInterface $page, SectionInterface $section)
|
||||
{
|
||||
$this->page = $page;
|
||||
|
||||
parent::__construct($page, $section);
|
||||
}
|
||||
|
||||
public function getPage()
|
||||
{
|
||||
return $this->page;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Source;
|
||||
|
||||
abstract class AbstractSetting
|
||||
{
|
||||
public function __construct(PageInterface $page, SectionInterface $section)
|
||||
{
|
||||
// do something with $page and $section
|
||||
$page->runSomething();
|
||||
$section->runSomething();
|
||||
}
|
||||
}
|
|
@ -8,6 +8,7 @@ use PhpParser\Node;
|
|||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Expr\StaticPropertyFetch;
|
||||
use PhpParser\Node\Expr\Variable;
|
||||
use PhpParser\Node\Param;
|
||||
use PhpParser\Node\Stmt\Class_;
|
||||
use PhpParser\Node\Stmt\ClassLike;
|
||||
|
@ -153,6 +154,9 @@ final class ComplexNodeRemover
|
|||
|
||||
$params = $constructClassMethod->getParams();
|
||||
$paramKeysToBeRemoved = [];
|
||||
|
||||
/** @var Variable[] $variables */
|
||||
$variables = $this->resolveVariables($constructClassMethod);
|
||||
foreach ($params as $key => $param) {
|
||||
$variable = $this->betterNodeFinder->findFirst(
|
||||
(array) $constructClassMethod->stmts,
|
||||
|
@ -171,12 +175,47 @@ final class ComplexNodeRemover
|
|||
continue;
|
||||
}
|
||||
|
||||
if ($this->isInVariables($variables, $assign)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$paramKeysToBeRemoved[] = $key;
|
||||
}
|
||||
|
||||
$this->processRemoveParamWithKeys($params, $paramKeysToBeRemoved);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Variable[]
|
||||
*/
|
||||
private function resolveVariables(ClassMethod $classMethod): array
|
||||
{
|
||||
return $this->betterNodeFinder->find(
|
||||
(array) $classMethod->stmts,
|
||||
function (Node $subNode): bool {
|
||||
if (! $subNode instanceof Variable) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->isExpressionVariableNotAssign($subNode);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Variable[] $variables
|
||||
*/
|
||||
private function isInVariables(array $variables, Assign $assign): bool
|
||||
{
|
||||
foreach ($variables as $variable) {
|
||||
if ($this->nodeComparator->areNodesEqual($assign->expr, $variable)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Param[] $params
|
||||
* @param int[] $paramKeysToBeRemoved
|
||||
|
|
Loading…
Reference in New Issue
Block a user