Skip to content

Commit

Permalink
Skip attribute in RemoveUnusedPromotedPropertyRector (#3280)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 13, 2023
1 parent 3b3ee7e commit fe7ed4f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

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

#[\Attribute]
final class SkipAttribute
{
public function __construct(
private $someUnusedDependency,
) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,44 +75,45 @@ public function getUsedDependency()
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
return [Class_::class];
}

/**
* @param ClassMethod $node
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$hasRemovedProperty = false;

if (! $this->isName($node, MethodName::CONSTRUCT)) {
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
if (! $constructClassMethod instanceof ClassMethod) {
return null;
}

$class = $this->betterNodeFinder->findParentType($node, Class_::class);
if (! $class instanceof Class_) {
// is attribute? skip it
if ($node->attrGroups !== []) {
return null;
}

foreach ($node->getParams() as $param) {
$hasRemovedProperty = false;

foreach ($constructClassMethod->getParams() as $param) {
// only private local scope; removing public property might be dangerous
if (! $this->visibilityManipulator->hasVisibility($param, Visibility::PRIVATE)) {
continue;
}

if ($this->propertyManipulator->isPropertyUsedInReadContext($class, $param)) {
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $param)) {
continue;
}

$paramName = $this->getName($param);

$propertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($class, $paramName);
$propertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($node, $paramName);
if ($propertyFetches !== []) {
continue;
}

// is variable used? only remove property, keep param
$variable = $this->betterNodeFinder->findVariableOfName((array) $node->stmts, $paramName);
$variable = $this->betterNodeFinder->findVariableOfName((array) $constructClassMethod->stmts, $paramName);
if ($variable instanceof Variable) {
$param->flags = 0;
continue;
Expand Down

0 comments on commit fe7ed4f

Please sign in to comment.