Skip to content

Commit 540fbfa

Browse files
authored
[DeadCode] Merge comment on RemoveNextSameValueConditionRector (#7810)
* [DeadCode] Merge comment on RemoveNextSameValueConditionRector * fix
1 parent a72c622 commit 540fbfa

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveNextSameValueConditionRector\Fixture;
4+
5+
final class MergeComment
6+
{
7+
public function __construct(array $items)
8+
{
9+
$count = 100;
10+
11+
// 1
12+
if ($items === []) {
13+
$count = 0;
14+
}
15+
16+
// 2
17+
if ($items === []) {
18+
return $count;
19+
}
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
namespace Rector\Tests\DeadCode\Rector\Stmt\RemoveNextSameValueConditionRector\Fixture;
28+
29+
final class MergeComment
30+
{
31+
public function __construct(array $items)
32+
{
33+
$count = 100;
34+
35+
// 1
36+
// 2
37+
if ($items === []) {
38+
$count = 0;
39+
return $count;
40+
}
41+
}
42+
}
43+
44+
?>

rules/DeadCode/Rector/Stmt/RemoveNextSameValueConditionRector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Stmt\Else_;
1010
use PhpParser\Node\Stmt\If_;
1111
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
12+
use Rector\NodeTypeResolver\Node\AttributeKey;
1213
use Rector\PhpParser\Enum\NodeGroup;
1314
use Rector\PhpParser\Node\BetterNodeFinder;
1415
use Rector\Rector\AbstractRector;
@@ -127,6 +128,10 @@ public function refactor(Node $node): ?Node
127128
continue;
128129
}
129130

131+
$stmt->setAttribute(AttributeKey::COMMENTS, array_merge(
132+
$stmt->getAttribute(AttributeKey::COMMENTS) ?? [],
133+
$nextStmt->getAttribute(AttributeKey::COMMENTS) ?? []
134+
));
130135
$stmt->stmts = array_merge($stmt->stmts, $nextStmt->stmts);
131136

132137
// remove next node

0 commit comments

Comments
 (0)