Skip to content

Commit 85d180e

Browse files
authored
[DeadCode] Add RemoveRedundantTypeCheckRector (#8307)
1 parent cc13c15 commit 85d180e

14 files changed

Lines changed: 502 additions & 0 deletions
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
4+
5+
final class ArrayParamTruthyCheck
6+
{
7+
public function buildWhereClauseFromArray($query, array $clauses, $expr = null): void
8+
{
9+
if ($clauses && is_array($clauses)) {
10+
return;
11+
}
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
20+
21+
final class ArrayParamTruthyCheck
22+
{
23+
public function buildWhereClauseFromArray($query, array $clauses, $expr = null): void
24+
{
25+
if ($clauses) {
26+
return;
27+
}
28+
}
29+
}
30+
31+
?>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
4+
5+
final class NullableIntFromMethodCall
6+
{
7+
public function run(): void
8+
{
9+
$count = $this->resolveCount();
10+
if ($count === null || ! is_int($count)) {
11+
return;
12+
}
13+
14+
echo $count;
15+
}
16+
17+
private function resolveCount(): ?int
18+
{
19+
return 1;
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
28+
29+
final class NullableIntFromMethodCall
30+
{
31+
public function run(): void
32+
{
33+
$count = $this->resolveCount();
34+
if ($count === null) {
35+
return;
36+
}
37+
38+
echo $count;
39+
}
40+
41+
private function resolveCount(): ?int
42+
{
43+
return 1;
44+
}
45+
}
46+
47+
?>
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\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
4+
5+
final class NullableString
6+
{
7+
public function run(?string $securityExpression): void
8+
{
9+
if (null === $securityExpression || ! is_string($securityExpression)) {
10+
return;
11+
}
12+
13+
echo $securityExpression;
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
22+
23+
final class NullableString
24+
{
25+
public function run(?string $securityExpression): void
26+
{
27+
if (null === $securityExpression) {
28+
return;
29+
}
30+
31+
echo $securityExpression;
32+
}
33+
}
34+
35+
?>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
4+
5+
final class NullableStringTruthyCheck
6+
{
7+
public function run(?string $value): void
8+
{
9+
if ($value && is_string($value)) {
10+
echo $value;
11+
}
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
20+
21+
final class NullableStringTruthyCheck
22+
{
23+
public function run(?string $value): void
24+
{
25+
if ($value) {
26+
echo $value;
27+
}
28+
}
29+
}
30+
31+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
4+
5+
final class SkipAnotherVariable
6+
{
7+
public function run(?string $value, mixed $anotherValue): void
8+
{
9+
if (null === $value || ! is_string($anotherValue)) {
10+
return;
11+
}
12+
13+
echo $value;
14+
}
15+
}
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\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
4+
5+
final class SkipDocblockArrayParam
6+
{
7+
/**
8+
* @param mixed[] $clauses
9+
*/
10+
public function run($clauses): void
11+
{
12+
if ($clauses && is_array($clauses)) {
13+
return;
14+
}
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\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
4+
5+
final class SkipDocblockNullableParam
6+
{
7+
/**
8+
* @param ?string $value
9+
*/
10+
public function run($value): void
11+
{
12+
if (null === $value || ! is_string($value)) {
13+
return;
14+
}
15+
16+
echo $value;
17+
}
18+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
4+
5+
final class SkipTruthyCheckOnUnion
6+
{
7+
public function run(array|string $value): void
8+
{
9+
if ($value && is_array($value)) {
10+
return;
11+
}
12+
}
13+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
4+
5+
final class SkipUntypedParam
6+
{
7+
public function run($value): void
8+
{
9+
if (null === $value || ! is_string($value)) {
10+
return;
11+
}
12+
13+
echo $value;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\BinaryOp\RemoveRedundantTypeCheckRector\Fixture;
4+
5+
final class SkipWiderUnionType
6+
{
7+
public function run(string|int|null $value): void
8+
{
9+
if (null === $value || ! is_string($value)) {
10+
return;
11+
}
12+
13+
echo $value;
14+
}
15+
}

0 commit comments

Comments
 (0)