Skip to content

Commit b4e4c38

Browse files
authored
[DeadCode] Add RemoveUselessTernaryRector (#7961)
* [DeadCode] Add RemoveUselessTernaryRector * skip negated * rename fixture * comment * early check * final touch: more fixture * final touch: handle true false on bool * final touch: handle true false on bool
1 parent ffbf103 commit b4e4c38

File tree

14 files changed

+332
-0
lines changed

14 files changed

+332
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class Fixture
6+
{
7+
public function go(bool $value)
8+
{
9+
return $value ?: false;
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
18+
19+
class Fixture
20+
{
21+
public function go(bool $value)
22+
{
23+
return $value;
24+
}
25+
}
26+
27+
?>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class SkipDifferentType
6+
{
7+
public function go(bool $value)
8+
{
9+
return $value ?: 0;
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class SkipDocblockType
6+
{
7+
/**
8+
* @param bool $value
9+
*/
10+
public function go($value)
11+
{
12+
return $value ?: false;
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class SkipNegatedCond
6+
{
7+
public function go(bool $value)
8+
{
9+
return ! $value ?: false;
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class SkipUnion
6+
{
7+
public function go(bool|string $value)
8+
{
9+
return $value ?: false;
10+
}
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class SkipWithIfNotEqual
6+
{
7+
public function go(bool $value)
8+
{
9+
return $value ? 1 : false;
10+
}
11+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class TrueFalseOnBool
6+
{
7+
public function go(bool $value)
8+
{
9+
return $value ? true : false;
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
18+
19+
class TrueFalseOnBool
20+
{
21+
public function go(bool $value)
22+
{
23+
return $value;
24+
}
25+
}
26+
27+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class WithArrayEmpty
6+
{
7+
public function go(array $value)
8+
{
9+
return $value ?: [];
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
18+
19+
class WithArrayEmpty
20+
{
21+
public function go(array $value)
22+
{
23+
return $value;
24+
}
25+
}
26+
27+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class WithIfEqual
6+
{
7+
public function go(bool $value)
8+
{
9+
return $value ? $value : false;
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
18+
19+
class WithIfEqual
20+
{
21+
public function go(bool $value)
22+
{
23+
return $value;
24+
}
25+
}
26+
27+
?>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
4+
5+
class WithIntegerZero
6+
{
7+
public function go(int $value)
8+
{
9+
return $value ?: 0;
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\DeadCode\Rector\Ternary\RemoveUselessTernaryRector\Fixture;
18+
19+
class WithIntegerZero
20+
{
21+
public function go(int $value)
22+
{
23+
return $value;
24+
}
25+
}
26+
27+
?>

0 commit comments

Comments
 (0)