Skip to content

Commit 45e7ee3

Browse files
committed
[DeadCode] Skip early return in constructor in RemoveDefaultValueFromAssignedPropertyRector
1 parent 009a5e4 commit 45e7ee3

3 files changed

Lines changed: 63 additions & 2 deletions

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\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;
4+
5+
final class ReturnInClosure
6+
{
7+
private ?string $name = null;
8+
9+
public function __construct(string $name)
10+
{
11+
$this->name = (function () use ($name): string {
12+
return $name;
13+
})();
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;
22+
23+
final class ReturnInClosure
24+
{
25+
private ?string $name;
26+
27+
public function __construct(string $name)
28+
{
29+
$this->name = (function () use ($name): string {
30+
return $name;
31+
})();
32+
}
33+
}
34+
35+
?>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Property\RemoveDefaultValueFromAssignedPropertyRector\Fixture;
4+
5+
final class SkipEarlyReturnInConstructor
6+
{
7+
public int $x = 0;
8+
9+
public function __construct(bool $shouldReturn = false)
10+
{
11+
if ($shouldReturn) {
12+
return;
13+
}
14+
15+
$this->x = 100;
16+
}
17+
}

‎rules/DeadCode/Rector/Property/RemoveDefaultValueFromAssignedPropertyRector.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PhpParser\Node\Expr;
99
use PhpParser\Node\Stmt\Class_;
1010
use PhpParser\Node\Stmt\ClassMethod;
11+
use PhpParser\Node\Stmt\Return_;
12+
use Rector\PhpParser\Node\BetterNodeFinder;
1113
use Rector\Rector\AbstractRector;
1214
use Rector\TypeDeclaration\AlreadyAssignDetector\ConstructorAssignDetector;
1315
use Rector\ValueObject\MethodName;
@@ -20,7 +22,8 @@
2022
final class RemoveDefaultValueFromAssignedPropertyRector extends AbstractRector
2123
{
2224
public function __construct(
23-
private readonly ConstructorAssignDetector $constructorAssignDetector
25+
private readonly ConstructorAssignDetector $constructorAssignDetector,
26+
private readonly BetterNodeFinder $betterNodeFinder
2427
) {
2528
}
2629

@@ -71,7 +74,13 @@ public function getNodeTypes(): array
7174
*/
7275
public function refactor(Node $node): ?Node
7376
{
74-
if (! $node->getMethod(MethodName::CONSTRUCT) instanceof ClassMethod) {
77+
$constructClassMethod = $node->getMethod(MethodName::CONSTRUCT);
78+
if (! $constructClassMethod instanceof ClassMethod) {
79+
return null;
80+
}
81+
82+
// early return can skip the assign, so the default value is still needed
83+
if ($this->betterNodeFinder->hasInstancesOfInFunctionLikeScoped($constructClassMethod, Return_::class)) {
7584
return null;
7685
}
7786

0 commit comments

Comments
 (0)