2019-10-13 05:59:52 +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\Property;
|
2018-12-25 16:00:06 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Class_;
|
|
|
|
use PhpParser\Node\Stmt\Property;
|
|
|
|
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
|
|
|
|
use Rector\Core\NodeManipulator\PropertyManipulator;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Removing\NodeManipulator\ComplexNodeRemover;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\RemoveUnusedPrivatePropertyRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveUnusedPrivatePropertyRector extends AbstractRector implements AllowEmptyConfigurableRectorInterface
|
2018-12-25 16:00:06 +00:00
|
|
|
{
|
2022-03-20 19:48:16 +00:00
|
|
|
/**
|
2022-06-30 13:57:27 +00:00
|
|
|
* @api
|
2022-03-20 19:48:16 +00:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
public const REMOVE_ASSIGN_SIDE_EFFECT = 'remove_assign_side_effect';
|
|
|
|
/**
|
|
|
|
* Default to true, which apply remove assign even has side effect.
|
|
|
|
* Set to false will allow to skip when assign has side effect.
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private $removeAssignSideEffect = \true;
|
2018-12-25 16:00:06 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\NodeManipulator\PropertyManipulator
|
2018-12-25 16:00:06 +00:00
|
|
|
*/
|
2019-02-27 21:54:39 +00:00
|
|
|
private $propertyManipulator;
|
2021-02-08 21:00:45 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Removing\NodeManipulator\ComplexNodeRemover
|
2021-02-08 21:00:45 +00:00
|
|
|
*/
|
|
|
|
private $complexNodeRemover;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(PropertyManipulator $propertyManipulator, ComplexNodeRemover $complexNodeRemover)
|
2018-12-25 16:00:06 +00:00
|
|
|
{
|
2019-02-27 21:54:39 +00:00
|
|
|
$this->propertyManipulator = $propertyManipulator;
|
2021-02-08 21:00:45 +00:00
|
|
|
$this->complexNodeRemover = $complexNodeRemover;
|
2018-12-25 16:00:06 +00:00
|
|
|
}
|
2022-05-04 22:24:14 +00:00
|
|
|
/**
|
|
|
|
* @param mixed[] $configuration
|
|
|
|
*/
|
2022-03-20 19:48:16 +00:00
|
|
|
public function configure(array $configuration) : void
|
|
|
|
{
|
|
|
|
$this->removeAssignSideEffect = $configuration[self::REMOVE_ASSIGN_SIDE_EFFECT] ?? (bool) \current($configuration);
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-25 16:00:06 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove unused private properties', [new ConfiguredCodeSample(<<<'CODE_SAMPLE'
|
2018-12-25 16:00:06 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
private $property;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-25 16:00:06 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2022-03-26 19:22:15 +00:00
|
|
|
, [self::REMOVE_ASSIGN_SIDE_EFFECT => \true])]);
|
2018-12-25 16:00:06 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-25 16:00:06 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-25 16:00:06 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2018-12-25 16:00:06 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-05-04 22:24:14 +00:00
|
|
|
* @param Class_ $node
|
2018-12-25 16:00:06 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2018-12-25 16:00:06 +00:00
|
|
|
{
|
2022-06-11 12:21:18 +00:00
|
|
|
$hasRemoved = \false;
|
2022-05-04 22:24:14 +00:00
|
|
|
foreach ($node->getProperties() as $property) {
|
|
|
|
if ($this->shouldSkipProperty($property)) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-05-05 09:39:03 +00:00
|
|
|
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $property)) {
|
2022-05-04 22:24:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-06-11 12:21:18 +00:00
|
|
|
// use different variable to avoid re-assign back $hasRemoved to false
|
|
|
|
// when already asssigned to true
|
|
|
|
$isRemoved = $this->complexNodeRemover->removePropertyAndUsages($node, $property, $this->removeAssignSideEffect);
|
|
|
|
if ($isRemoved) {
|
|
|
|
$hasRemoved = \true;
|
|
|
|
}
|
2018-12-25 16:00:06 +00:00
|
|
|
}
|
2022-06-11 12:21:18 +00:00
|
|
|
return $hasRemoved ? $node : null;
|
2018-12-25 16:00:06 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkipProperty(Property $property) : bool
|
2019-08-29 19:22:52 +00:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($property->props) !== 1) {
|
|
|
|
return \true;
|
2020-04-29 11:42:11 +00:00
|
|
|
}
|
2022-05-04 22:24:14 +00:00
|
|
|
return !$property->isPrivate();
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2018-12-25 16:00:06 +00:00
|
|
|
}
|