mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-03 18:00:50 +00:00
96112cb1f0
2da49992cc
[Downgrade] [PHP 7.2] Make DowngradeParameterTypeWideningRector always downgrade to phpdoc type (#390)
91 lines
2.6 KiB
PHP
91 lines
2.6 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\DeadCode\Rector\ClassMethod;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
use PhpParser\Node\Stmt\Interface_;
|
|
use Rector\Core\NodeAnalyzer\ParamAnalyzer;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\Core\ValueObject\MethodName;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector\RemoveUnusedConstructorParamRectorTest
|
|
*/
|
|
final class RemoveUnusedConstructorParamRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
/**
|
|
* @var \Rector\Core\NodeAnalyzer\ParamAnalyzer
|
|
*/
|
|
private $paramAnalyzer;
|
|
public function __construct(\Rector\Core\NodeAnalyzer\ParamAnalyzer $paramAnalyzer)
|
|
{
|
|
$this->paramAnalyzer = $paramAnalyzer;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove unused parameter in constructor', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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 array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\ClassMethod::class];
|
|
}
|
|
/**
|
|
* @param ClassMethod $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (!$this->isName($node, \Rector\Core\ValueObject\MethodName::CONSTRUCT)) {
|
|
return null;
|
|
}
|
|
if ($node->params === []) {
|
|
return null;
|
|
}
|
|
if ($this->paramAnalyzer->hasPropertyPromotion($node->params)) {
|
|
return null;
|
|
}
|
|
$classLike = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
|
|
if ($classLike instanceof \PhpParser\Node\Stmt\Interface_) {
|
|
return null;
|
|
}
|
|
if ($node->isAbstract()) {
|
|
return null;
|
|
}
|
|
foreach ($node->params as $param) {
|
|
if ($this->paramAnalyzer->isParamUsedInClassMethod($node, $param)) {
|
|
continue;
|
|
}
|
|
$this->nodeRemover->removeParam($node, $param);
|
|
}
|
|
return null;
|
|
}
|
|
}
|