mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-26 12:52:36 +00:00
95 lines
2.0 KiB
PHP
95 lines
2.0 KiB
PHP
|
<?php
|
||
|
|
||
|
declare(strict_types=1);
|
||
|
|
||
|
namespace Rector\DeadCode\Rector\ClassMethod;
|
||
|
|
||
|
use PhpParser\Node;
|
||
|
use PhpParser\Node\Stmt\ClassMethod;
|
||
|
use Rector\Core\Rector\AbstractRector;
|
||
|
use Rector\Core\ValueObject\MethodName;
|
||
|
use Rector\NetteKdyby\NodeManipulator\ParamAnalyzer;
|
||
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
||
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
||
|
|
||
|
/**
|
||
|
* @see \Rector\DeadCode\Tests\Rector\ClassMethod\RemoveUnusedConstructorParamRector\RemoveUnusedConstructorParamRectorTest
|
||
|
*/
|
||
|
final class RemoveUnusedConstructorParamRector extends AbstractRector
|
||
|
{
|
||
|
/**
|
||
|
* @var ParamAnalyzer
|
||
|
*/
|
||
|
private $paramAnalyzer;
|
||
|
|
||
|
public function __construct(ParamAnalyzer $paramAnalyzer)
|
||
|
{
|
||
|
$this->paramAnalyzer = $paramAnalyzer;
|
||
|
}
|
||
|
|
||
|
public function getRuleDefinition(): RuleDefinition
|
||
|
{
|
||
|
return new RuleDefinition('Remove unused parameter in constructor', [
|
||
|
new CodeSample(
|
||
|
<<<'CODE_SAMPLE'
|
||
|
final class SomeClass
|
||
|
{
|
||
|
private $hey;
|
||
|
|
||
|
public function __construct($hey, $man)
|
||
|
{
|
||
|
$this->hey = $hey;
|
||
|
}
|
||
|
}
|
||
|
CODE_SAMPLE
|
||
|
|
||
|
,
|
||
|
<<<'CODE_SAMPLE'
|
||
|
final class SomeClass
|
||
|
{
|
||
|
private $hey;
|
||
|
|
||
|
public function __construct($hey)
|
||
|
{
|
||
|
$this->hey = $hey;
|
||
|
}
|
||
|
}
|
||
|
CODE_SAMPLE
|
||
|
|
||
|
),
|
||
|
]);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @return string[]
|
||
|
*/
|
||
|
public function getNodeTypes(): array
|
||
|
{
|
||
|
return [ClassMethod::class];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param ClassMethod $node
|
||
|
*/
|
||
|
public function refactor(Node $node): ?Node
|
||
|
{
|
||
|
if (! $this->isName($node, MethodName::CONSTRUCT)) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
if ($node->params === []) {
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
foreach ($node->params as $param) {
|
||
|
if ($this->paramAnalyzer->isParamUsedInClassMethod($node, $param)) {
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
$this->nodeRemover->removeParam($node, $param);
|
||
|
}
|
||
|
|
||
|
return null;
|
||
|
}
|
||
|
}
|