Skip to content

Commit 0639221

Browse files
authored
[DeadCode] Add RemoveOverriddenAssignBeforeIfElseRector (#8505)
1 parent 2f0f649 commit 0639221

12 files changed

Lines changed: 467 additions & 0 deletions
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector\Fixture;
4+
5+
final class OverrideInBothBranches
6+
{
7+
public function run(string $state, array $present, int $id): array
8+
{
9+
$settings = [];
10+
if ($state === 'enabled') {
11+
$settings = $this->getDefaults();
12+
foreach ($settings as $key => $value) {
13+
if (array_key_exists($key, $present)) {
14+
unset($settings[$key]);
15+
}
16+
}
17+
18+
$settings['enabled'] = 'yes';
19+
} else {
20+
$settings = ['enabled' => 'no', 'id' => $id];
21+
}
22+
23+
return $settings;
24+
}
25+
26+
private function getDefaults(): array
27+
{
28+
return [];
29+
}
30+
}
31+
32+
?>
33+
-----
34+
<?php
35+
36+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector\Fixture;
37+
38+
final class OverrideInBothBranches
39+
{
40+
public function run(string $state, array $present, int $id): array
41+
{
42+
if ($state === 'enabled') {
43+
$settings = $this->getDefaults();
44+
foreach ($settings as $key => $value) {
45+
if (array_key_exists($key, $present)) {
46+
unset($settings[$key]);
47+
}
48+
}
49+
50+
$settings['enabled'] = 'yes';
51+
} else {
52+
$settings = ['enabled' => 'no', 'id' => $id];
53+
}
54+
55+
return $settings;
56+
}
57+
58+
private function getDefaults(): array
59+
{
60+
return [];
61+
}
62+
}
63+
64+
?>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector\Fixture;
4+
5+
final class SkipBranchWithoutOverride
6+
{
7+
public function run(bool $value): array
8+
{
9+
$result = [];
10+
if ($value) {
11+
$result = [1, 2, 3];
12+
} else {
13+
echo 'nothing';
14+
}
15+
16+
return $result;
17+
}
18+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector\Fixture;
4+
5+
final class SkipCompactReadsValue
6+
{
7+
public function run(bool $value): array
8+
{
9+
$result = 'x';
10+
if ($value) {
11+
$data = compact('result');
12+
$result = [1, 2, 3];
13+
14+
return $data;
15+
} else {
16+
$result = [4, 5, 6];
17+
}
18+
19+
return $result;
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector\Fixture;
4+
5+
final class SkipCondUsesVariable
6+
{
7+
public function run(): array
8+
{
9+
$result = [1];
10+
if ($result !== []) {
11+
$result = [1, 2, 3];
12+
} else {
13+
$result = [4, 5, 6];
14+
}
15+
16+
return $result;
17+
}
18+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector\Fixture;
4+
5+
final class SkipNoElse
6+
{
7+
public function run(bool $value): array
8+
{
9+
$result = [];
10+
if ($value) {
11+
$result = [1, 2, 3];
12+
}
13+
14+
return $result;
15+
}
16+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector\Fixture;
4+
5+
final class SkipPartialOverride
6+
{
7+
public function run(bool $value): array
8+
{
9+
$result = [];
10+
if ($value) {
11+
$result[] = 1;
12+
} else {
13+
$result = [4, 5, 6];
14+
}
15+
16+
return $result;
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector\Fixture;
4+
5+
final class SkipReadInOverride
6+
{
7+
public function run(bool $value): array
8+
{
9+
$result = [1];
10+
if ($value) {
11+
$result = array_merge($result, [2]);
12+
} else {
13+
$result = [4, 5, 6];
14+
}
15+
16+
return $result;
17+
}
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector\Fixture;
4+
5+
final class SkipSideEffectInitial
6+
{
7+
public function run(bool $value): array
8+
{
9+
$result = $this->init();
10+
if ($value) {
11+
$result = [1, 2, 3];
12+
} else {
13+
$result = [4, 5, 6];
14+
}
15+
16+
return $result;
17+
}
18+
19+
private function init(): array
20+
{
21+
echo 'side effect';
22+
23+
return [];
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveOverriddenAssignBeforeIfElseRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DeadCode\Rector\If_\RemoveOverriddenAssignBeforeIfElseRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([RemoveOverriddenAssignBeforeIfElseRector::class]);

0 commit comments

Comments
 (0)