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)
84 lines
2.7 KiB
PHP
84 lines
2.7 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\DeadCode\Rector\Property;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Stmt\Class_;
|
|
use PhpParser\Node\Stmt\Interface_;
|
|
use PhpParser\Node\Stmt\Property;
|
|
use PhpParser\Node\Stmt\Trait_;
|
|
use Rector\Core\NodeManipulator\PropertyManipulator;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Rector\Removing\NodeManipulator\ComplexNodeRemover;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\RemoveUnusedPrivatePropertyRectorTest
|
|
*/
|
|
final class RemoveUnusedPrivatePropertyRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
/**
|
|
* @var \Rector\Core\NodeManipulator\PropertyManipulator
|
|
*/
|
|
private $propertyManipulator;
|
|
/**
|
|
* @var \Rector\Removing\NodeManipulator\ComplexNodeRemover
|
|
*/
|
|
private $complexNodeRemover;
|
|
public function __construct(\Rector\Core\NodeManipulator\PropertyManipulator $propertyManipulator, \Rector\Removing\NodeManipulator\ComplexNodeRemover $complexNodeRemover)
|
|
{
|
|
$this->propertyManipulator = $propertyManipulator;
|
|
$this->complexNodeRemover = $complexNodeRemover;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove unused private properties', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
private $property;
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\Property::class];
|
|
}
|
|
/**
|
|
* @param Property $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if ($this->shouldSkipProperty($node)) {
|
|
return null;
|
|
}
|
|
if ($this->propertyManipulator->isPropertyUsedInReadContext($node)) {
|
|
return null;
|
|
}
|
|
$this->complexNodeRemover->removePropertyAndUsages($node);
|
|
return $node;
|
|
}
|
|
private function shouldSkipProperty(\PhpParser\Node\Stmt\Property $property) : bool
|
|
{
|
|
if (\count($property->props) !== 1) {
|
|
return \true;
|
|
}
|
|
if (!$property->isPrivate()) {
|
|
return \true;
|
|
}
|
|
/** @var Class_|Interface_|Trait_|null $classLike */
|
|
$classLike = $property->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
|
|
return !$classLike instanceof \PhpParser\Node\Stmt\Class_;
|
|
}
|
|
}
|