Skip to content

Commit

Permalink
[DeadCode] Add support for removal readonly property on RemoveUnusedP…
Browse files Browse the repository at this point in the history
…romotedPropertyRector (#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
  • Loading branch information
samsonasik committed Jan 29, 2022
1 parent d31e223 commit 70d6465
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\Fixture;

use stdClass;

class SkipPublicSomePropertyReadonly
{
public function __construct(public readonly stdClass $someUnusedDependency)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\Fixture;

use stdClass;

class SomePrivatePropertyReadonly
{
public function __construct(private readonly stdClass $someUnusedDependency)
{
}
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\Fixture;

use stdClass;

class SomePrivatePropertyReadonly
{
public function __construct()
{
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
) {
}

Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit 70d6465

Please sign in to comment.