From 70d6465a4b1bcad94fe56878c6e90533473e7847 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sun, 30 Jan 2022 01:03:42 +0700 Subject: [PATCH] [DeadCode] Add support for removal readonly property on RemoveUnusedPromotedPropertyRector (#1741) * Add support for removal readonly property on RemoveUnusedPromotedPropertyRector * add fixture for skip public readonly * implemented :tada * final touch: eol * final touch: utilize VisibilityManipulator service is working --- ...skip_public_some_property_readonly.php.inc | 12 ++++++++ .../some_private_roperty_readonly.php.inc | 29 +++++++++++++++++++ .../RemoveUnusedPromotedPropertyRector.php | 5 +++- 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector/Fixture/skip_public_some_property_readonly.php.inc create mode 100644 rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector/Fixture/some_private_roperty_readonly.php.inc diff --git a/rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector/Fixture/skip_public_some_property_readonly.php.inc b/rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector/Fixture/skip_public_some_property_readonly.php.inc new file mode 100644 index 00000000000..cc17bc37988 --- /dev/null +++ b/rules-tests/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector/Fixture/skip_public_some_property_readonly.php.inc @@ -0,0 +1,12 @@ + +----- + diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php index 538d6c8c13d..014c88cd8ae 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveUnusedPromotedPropertyRector.php @@ -13,6 +13,8 @@ use Rector\Core\Rector\AbstractRector; use Rector\Core\ValueObject\MethodName; use Rector\Core\ValueObject\PhpVersionFeature; +use Rector\Core\ValueObject\Visibility; +use Rector\Privatization\NodeManipulator\VisibilityManipulator; use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -25,6 +27,7 @@ final class RemoveUnusedPromotedPropertyRector extends AbstractRector implements public function __construct( private readonly PropertyFetchFinder $propertyFetchFinder, private readonly PropertyManipulator $propertyManipulator, + private readonly VisibilityManipulator $visibilityManipulator ) { } @@ -93,7 +96,7 @@ public function refactor(Node $node): ?Node foreach ($node->getParams() as $param) { // only private local scope; removing public property might be dangerous - if ($param->flags !== Class_::MODIFIER_PRIVATE) { + if (! $this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) { continue; }