Skip to content

Commit

Permalink
[DeadCode] Remove property comment same line on RemoveUnusedPrivatePr…
Browse files Browse the repository at this point in the history
…opertyRector (#3547)

Co-authored-by: Markus Staab <markus.staab@redaxo.de>
Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
3 people committed Apr 1, 2023
1 parent a66b295 commit 53f549d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

final class RemoveSamelineCommentProperty
{

protected $_distributoradaids = [];

private $_max_fileage = 3600;// 1h
}

?>
-----
<?php

namespace Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\Fixture;

final class RemoveSamelineCommentProperty
{

protected $_distributoradaids = [];
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Property;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\NodeManipulator\PropertyManipulator;
Expand Down Expand Up @@ -86,7 +87,11 @@ public function refactor(Node $node): ?Node
{
$hasRemoved = false;

foreach ($node->getProperties() as $property) {
foreach ($node->stmts as $key => $property) {
if (! $property instanceof Property) {
continue;
}

if ($this->shouldSkipProperty($property)) {
continue;
}
Expand All @@ -104,13 +109,31 @@ public function refactor(Node $node): ?Node
);

if ($isRemoved) {
$this->processRemoveSameLineComment($node, $property, $key);
$hasRemoved = true;
}
}

return $hasRemoved ? $node : null;
}

private function processRemoveSameLineComment(Class_ $class, Property $property, int $key): void
{
if (!isset($class->stmts[$key + 1])) {
return;
}

if (!$class->stmts[$key + 1] instanceof Nop) {
return;
}

if ($class->stmts[$key + 1]->getEndLine() !== $property->getStartLine()) {
return;
}

unset($class->stmts[$key + 1]);
}

private function shouldSkipProperty(Property $property): bool
{
if (count($property->props) !== 1) {
Expand Down

0 comments on commit 53f549d

Please sign in to comment.