Skip to content

Commit bfb234c

Browse files
committed
keep merge comments decoupled from abstract rector, remove unnecessary key to trigger comment merge
1 parent 453bd56 commit bfb234c

File tree

6 files changed

+35
-29
lines changed

6 files changed

+35
-29
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
"rector/type-perfect": "^2.1",
5757
"shipmonk/composer-dependency-analyser": "^1.8",
5858
"symplify/phpstan-extensions": "^12.0.2",
59-
"symplify/phpstan-rules": "dev-main",
59+
"symplify/phpstan-rules": "^14.9.4",
6060
"symplify/vendor-patches": "^11.5",
6161
"tomasvotruba/class-leak": "^2.1",
6262
"tomasvotruba/unused-public": "^2.1",

rules-tests/DeadCode/Rector/Expression/RemoveDeadStmtRector/FixtureRemovedComments/array_dim_fetch.php.inc

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
namespace Rector\Tests\CodeQuality\Rector\Stmt\DeadCodeRemovingRector\Fixture\ArrayDimFetch;
44

5-
function wrapToPreventPhpStanCallingMethods ()
5+
function wrapToPreventPhpStanCallingMethods()
66
{
77
$var[0] = 1;
88

99
$var[0];
1010

11-
//comment
11+
//comment 1
1212
$var[methodCall()];
1313

14-
//comment
14+
//comment 2
1515
${methodCall()}[0];
1616

17-
//comment
17+
//comment 3
1818
${methodCall1()}[methodCall2()];
1919
}
2020
?>
@@ -23,19 +23,18 @@ function wrapToPreventPhpStanCallingMethods ()
2323

2424
namespace Rector\Tests\CodeQuality\Rector\Stmt\DeadCodeRemovingRector\Fixture\ArrayDimFetch;
2525

26-
function wrapToPreventPhpStanCallingMethods ()
26+
function wrapToPreventPhpStanCallingMethods()
2727
{
2828
$var[0] = 1;
2929

30-
//comment
30+
//comment 1
3131
methodCall();
3232

33-
//comment
33+
//comment 2
3434
methodCall();
35-
//comment
3635
methodCall2();
3736

38-
//comment
37+
//comment 3
3938
methodCall1();
4039
}
4140
?>

rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,13 @@ public function refactor(Node $node): int|null|array|If_
127127
return NodeVisitor::REMOVE_NODE;
128128
}
129129

130-
$node->stmts[0]->setAttribute(AttributeKey::COMMENTS, array_merge(
131-
$node->getComments(),
132-
$node->stmts[0]->getComments(),
133-
));
134-
$node->stmts[0]->setAttribute(AttributeKey::HAS_MERGED_COMMENTS, true);
130+
// keep original comments
131+
if ($node->getComments() !== []) {
132+
$node->stmts[0]->setAttribute(AttributeKey::COMMENTS, array_merge(
133+
$node->getComments(),
134+
$node->stmts[0]->getComments(),
135+
));
136+
}
135137

136138
return $node->stmts;
137139
}

rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Rector\DeadCode\ConditionEvaluator;
1313
use Rector\DeadCode\ConditionResolver;
1414
use Rector\DeadCode\Contract\ConditionInterface;
15+
use Rector\NodeTypeResolver\Node\AttributeKey;
1516
use Rector\Rector\AbstractRector;
1617
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1718
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
@@ -100,7 +101,7 @@ private function refactorIsMatch(If_ $if): ?array
100101
return null;
101102
}
102103

103-
return $if->stmts;
104+
return $this->keepIfLevelComments($if, $if->stmts);
104105
}
105106

106107
/**
@@ -113,7 +114,22 @@ private function refactorIsNotMatch(If_ $if): array|int
113114
return NodeVisitor::REMOVE_NODE;
114115
}
115116

116-
// else is always used
117-
return $if->else->stmts;
117+
return $this->keepIfLevelComments($if, $if->else->stmts);
118+
}
119+
120+
/**
121+
* @param Stmt[] $stmts
122+
* @return Stmt[]
123+
*/
124+
private function keepIfLevelComments(If_ $if, array $stmts): array
125+
{
126+
if (count($stmts) > 0) {
127+
$stmts[0]->setAttribute(
128+
AttributeKey::COMMENTS,
129+
array_merge($if->getComments(), $stmts[0]->getComments())
130+
);
131+
}
132+
133+
return $stmts;
118134
}
119135
}

src/NodeTypeResolver/Node/AttributeKey.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,6 @@ final class AttributeKey
257257
*/
258258
public const IS_FIRST_LEVEL_STATEMENT = 'first_level_stmt';
259259

260-
/**
261-
* @var string
262-
*/
263-
public const HAS_MERGED_COMMENTS = 'has_merged_comments';
264-
265260
public const IS_DEFAULT_PROPERTY_VALUE = 'is_default_property_value';
266261

267262
public const IS_CLASS_CONST_VALUE = 'is_default_class_const_value';

src/Rector/AbstractRector.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,6 @@ private function postRefactorProcess(
291291
$currentScope = $node->getAttribute(AttributeKey::SCOPE);
292292

293293
if (is_array($refactoredNode)) {
294-
$firstNode = current($refactoredNode);
295-
296-
if ($firstNode->getAttribute(AttributeKey::HAS_MERGED_COMMENTS, false) === false) {
297-
$this->mirrorComments($firstNode, $originalNode);
298-
}
299-
300294
$this->refreshScopeNodes($refactoredNode, $filePath, $currentScope);
301295

302296
// search "infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown

0 commit comments

Comments
 (0)