Skip to content

Commit 4d8940b

Browse files
authored
[Php74] Keep comments above if on IfToNullCoalescingAssignRector (#8388)
1 parent 19b140a commit 4d8940b

2 files changed

Lines changed: 39 additions & 1 deletion

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php74\Rector\If_\IfToNullCoalescingAssignRector\Fixture;
4+
5+
/**
6+
* Fallback value description
7+
*
8+
* @var int $limit
9+
*/
10+
if (! isset($limit)) {
11+
$limit = 100;
12+
}
13+
14+
// simple comment
15+
if (! isset($name)) {
16+
$name = 'default';
17+
}
18+
19+
?>
20+
-----
21+
<?php
22+
23+
namespace Rector\Tests\Php74\Rector\If_\IfToNullCoalescingAssignRector\Fixture;
24+
25+
/**
26+
* Fallback value description
27+
*
28+
* @var int $limit
29+
*/
30+
$limit ??= 100;
31+
32+
// simple comment
33+
$name ??= 'default';
34+
35+
?>

‎rules/Php74/Rector/If_/IfToNullCoalescingAssignRector.php‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ public function refactor(Node $node): ?Expression
113113
return null;
114114
}
115115

116-
return new Expression(new AssignCoalesce($assign->var, $assign->expr));
116+
$expression = new Expression(new AssignCoalesce($assign->var, $assign->expr));
117+
$this->mirrorComments($expression, $node);
118+
119+
return $expression;
117120
}
118121

119122
public function provideMinPhpVersion(): int

0 commit comments

Comments
 (0)