Skip to content

Commit 945f308

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

6 files changed

Lines changed: 42 additions & 28 deletions

File tree

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: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,20 @@ 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+
}
137+
138+
// $node->stmts[0]->setAttribute(AttributeKey::HAS_MERGED_COMMENTS, true);
139+
140+
// dump($node->stmts[0]->getDocComment());
141+
// die;
142+
143+
// $ifStmts = $node->stmts;
135144

136145
return $node->stmts;
137146
}

rules/DeadCode/Rector/If_/UnwrapFutureCompatibleIfPhpVersionRector.php

Lines changed: 19 additions & 2 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,15 @@ private function refactorIsMatch(If_ $if): ?array
100101
return null;
101102
}
102103

103-
return $if->stmts;
104+
$ifStmts = $if->stmts;
105+
if (count($ifStmts) > 0) {
106+
$ifStmts[0]->setAttribute(
107+
AttributeKey::COMMENTS,
108+
array_merge($if->getComments(), $ifStmts[0]->getComments())
109+
);
110+
}
111+
112+
return $ifStmts;
104113
}
105114

106115
/**
@@ -114,6 +123,14 @@ private function refactorIsNotMatch(If_ $if): array|int
114123
}
115124

116125
// else is always used
117-
return $if->else->stmts;
126+
$elseStmts = $if->else->stmts;
127+
if (count($elseStmts) > 0) {
128+
$elseStmts[0]->setAttribute(
129+
AttributeKey::COMMENTS,
130+
array_merge($if->getComments(), $elseStmts[0]->getComments())
131+
);
132+
}
133+
134+
return $elseStmts;
118135
}
119136
}

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)