2021-02-09 03:55:21 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\Rector\ClassMethod;
|
2021-02-09 03:55:21 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\ValueObject\MethodName;
|
|
|
|
use Rector\Removing\NodeManipulator\ComplexNodeRemover;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-02-09 03:55:21 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector\RemoveUnusedConstructorParamRectorTest
|
2021-02-09 03:55:21 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveUnusedConstructorParamRector extends AbstractRector
|
2021-02-09 03:55:21 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\NodeAnalyzer\ParamAnalyzer
|
2021-02-09 03:55:21 +00:00
|
|
|
*/
|
|
|
|
private $paramAnalyzer;
|
2022-01-22 16:02:37 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\Removing\NodeManipulator\ComplexNodeRemover
|
|
|
|
*/
|
|
|
|
private $complexNodeRemover;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ParamAnalyzer $paramAnalyzer, ComplexNodeRemover $complexNodeRemover)
|
2021-02-09 03:55:21 +00:00
|
|
|
{
|
|
|
|
$this->paramAnalyzer = $paramAnalyzer;
|
2022-01-22 16:02:37 +00:00
|
|
|
$this->complexNodeRemover = $complexNodeRemover;
|
2021-02-09 03:55:21 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-02-09 03:55:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove unused parameter in constructor', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-02-09 03:55:21 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
private $hey;
|
|
|
|
|
|
|
|
public function __construct($hey, $man)
|
|
|
|
{
|
|
|
|
$this->hey = $hey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2021-02-09 03:55:21 +00:00
|
|
|
final class SomeClass
|
|
|
|
{
|
|
|
|
private $hey;
|
|
|
|
|
|
|
|
public function __construct($hey)
|
|
|
|
{
|
|
|
|
$this->hey = $hey;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2021-02-09 03:55:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-02-09 03:55:21 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2021-02-09 03:55:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassMethod::class];
|
2021-02-09 03:55:21 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClassMethod $node
|
2021-02-09 03:55:21 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2021-02-09 03:55:21 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$this->isName($node, MethodName::CONSTRUCT)) {
|
2021-02-09 03:55:21 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($node->params === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2021-02-13 20:05:43 +00:00
|
|
|
if ($this->paramAnalyzer->hasPropertyPromotion($node->params)) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$class = $this->betterNodeFinder->findParentType($node, Class_::class);
|
|
|
|
if (!$class instanceof Class_) {
|
2021-02-12 00:34:10 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($node->isAbstract()) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-01-22 16:02:37 +00:00
|
|
|
return $this->processRemoveParams($node);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function processRemoveParams(ClassMethod $classMethod) : ?ClassMethod
|
2022-01-22 16:02:37 +00:00
|
|
|
{
|
|
|
|
$paramKeysToBeRemoved = [];
|
|
|
|
foreach ($classMethod->params as $key => $param) {
|
|
|
|
if ($this->paramAnalyzer->isParamUsedInClassMethod($classMethod, $param)) {
|
2021-02-09 03:55:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-01-22 16:02:37 +00:00
|
|
|
$paramKeysToBeRemoved[] = $key;
|
|
|
|
}
|
|
|
|
$removedParamKeys = $this->complexNodeRemover->processRemoveParamWithKeys($classMethod->params, $paramKeysToBeRemoved);
|
|
|
|
if ($removedParamKeys === []) {
|
|
|
|
return null;
|
2021-02-09 03:55:21 +00:00
|
|
|
}
|
2022-01-22 16:02:37 +00:00
|
|
|
return $classMethod;
|
2021-02-09 03:55:21 +00:00
|
|
|
}
|
|
|
|
}
|