Skip to content

Commit 99190b5

Browse files
committed
[DeadCode] Add RemoveRedundantNullableTypeCheckRector
1 parent 2a551e7 commit 99190b5

9 files changed

Lines changed: 323 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\BooleanOr\RemoveRedundantNullableTypeCheckRector\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\BooleanOr\RemoveRedundantNullableTypeCheckRector\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\BooleanOr\RemoveRedundantNullableTypeCheckRector\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\BooleanOr\RemoveRedundantNullableTypeCheckRector\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: 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\BooleanOr\RemoveRedundantNullableTypeCheckRector\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: 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\BooleanOr\RemoveRedundantNullableTypeCheckRector\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\BooleanOr\RemoveRedundantNullableTypeCheckRector\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+
}
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\BooleanOr\RemoveRedundantNullableTypeCheckRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveRedundantNullableTypeCheckRectorTest 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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DeadCode\Rector\BooleanOr\RemoveRedundantNullableTypeCheckRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(RemoveRedundantNullableTypeCheckRector::class);
10+
};
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DeadCode\Rector\BooleanOr;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
10+
use PhpParser\Node\Expr\BinaryOp\Identical;
11+
use PhpParser\Node\Expr\BooleanNot;
12+
use PhpParser\Node\Expr\FuncCall;
13+
use PhpParser\Node\Expr\Variable;
14+
use PHPStan\Type\Type;
15+
use PHPStan\Type\TypeCombinator;
16+
use Rector\PhpParser\Node\Value\ValueResolver;
17+
use Rector\Rector\AbstractRector;
18+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
19+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
20+
21+
/**
22+
* @see \Rector\Tests\DeadCode\Rector\BooleanOr\RemoveRedundantNullableTypeCheckRector\RemoveRedundantNullableTypeCheckRectorTest
23+
*/
24+
final class RemoveRedundantNullableTypeCheckRector extends AbstractRector
25+
{
26+
public function __construct(
27+
private readonly ValueResolver $valueResolver
28+
) {
29+
}
30+
31+
public function getRuleDefinition(): RuleDefinition
32+
{
33+
return new RuleDefinition(
34+
'Remove is_<type>() check that can never fail next to null compare on a nullable value',
35+
[
36+
new CodeSample(
37+
<<<'CODE_SAMPLE'
38+
class SomeClass
39+
{
40+
public function run(?string $value)
41+
{
42+
if ($value === null || ! is_string($value)) {
43+
return;
44+
}
45+
}
46+
}
47+
CODE_SAMPLE
48+
,
49+
<<<'CODE_SAMPLE'
50+
class SomeClass
51+
{
52+
public function run(?string $value)
53+
{
54+
if ($value === null) {
55+
return;
56+
}
57+
}
58+
}
59+
CODE_SAMPLE
60+
),
61+
]
62+
);
63+
}
64+
65+
/**
66+
* @return array<class-string<Node>>
67+
*/
68+
public function getNodeTypes(): array
69+
{
70+
return [BooleanOr::class];
71+
}
72+
73+
/**
74+
* @param BooleanOr $node
75+
*/
76+
public function refactor(Node $node): ?Expr
77+
{
78+
if (! $node->left instanceof Identical) {
79+
return null;
80+
}
81+
82+
$nullComparedExpr = $this->matchNullComparedExpr($node->left);
83+
if (! $nullComparedExpr instanceof Variable) {
84+
return null;
85+
}
86+
87+
if (! $node->right instanceof BooleanNot) {
88+
return null;
89+
}
90+
91+
$funcCall = $node->right->expr;
92+
if (! $funcCall instanceof FuncCall) {
93+
return null;
94+
}
95+
96+
if ($funcCall->isFirstClassCallable()) {
97+
return null;
98+
}
99+
100+
if (count($funcCall->getArgs()) !== 1) {
101+
return null;
102+
}
103+
104+
if (! $this->nodeComparator->areNodesEqual($funcCall->getArgs()[0]->value, $nullComparedExpr)) {
105+
return null;
106+
}
107+
108+
$funcCallName = $this->getName($funcCall);
109+
if ($funcCallName === null) {
110+
return null;
111+
}
112+
113+
$comparedType = $this->getType($nullComparedExpr);
114+
if (! TypeCombinator::containsNull($comparedType)) {
115+
return null;
116+
}
117+
118+
if (! $this->isAlwaysMatchingType($funcCallName, TypeCombinator::removeNull($comparedType))) {
119+
return null;
120+
}
121+
122+
return $node->left;
123+
}
124+
125+
private function matchNullComparedExpr(Identical $identical): ?Expr
126+
{
127+
if ($this->valueResolver->isNull($identical->left)) {
128+
return $identical->right;
129+
}
130+
131+
if ($this->valueResolver->isNull($identical->right)) {
132+
return $identical->left;
133+
}
134+
135+
return null;
136+
}
137+
138+
private function isAlwaysMatchingType(string $funcCallName, Type $type): bool
139+
{
140+
return match ($funcCallName) {
141+
'is_string' => $type->isString()
142+
->yes(),
143+
'is_int', 'is_integer', 'is_long' => $type->isInteger()
144+
->yes(),
145+
'is_float', 'is_double' => $type->isFloat()
146+
->yes(),
147+
'is_bool' => $type->isBoolean()
148+
->yes(),
149+
'is_array' => $type->isArray()
150+
->yes(),
151+
'is_object' => $type->isObject()
152+
->yes(),
153+
default => false,
154+
};
155+
}
156+
}

‎src/Config/Level/DeadCodeLevel.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
1717
use Rector\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector;
1818
use Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector;
19+
use Rector\DeadCode\Rector\BooleanOr\RemoveRedundantNullableTypeCheckRector;
1920
use Rector\DeadCode\Rector\Cast\RecastingRemovalRector;
2021
use Rector\DeadCode\Rector\ClassConst\RemoveUnusedPrivateClassConstantRector;
2122
use Rector\DeadCode\Rector\ClassMethod\RemoveArgumentFromDefaultParentCallRector;
@@ -148,6 +149,7 @@ final class DeadCodeLevel
148149

149150
RemoveAlwaysTrueIfConditionRector::class,
150151
ReduceAlwaysFalseIfOrRector::class,
152+
RemoveRedundantNullableTypeCheckRector::class,
151153
RemoveUnusedPrivateClassConstantRector::class,
152154
RemoveUnusedPrivatePropertyRector::class,
153155
RemoveUnusedClosureVariableUseRector::class,

0 commit comments

Comments
 (0)