Skip to content

Commit 4f3864c

Browse files
committed
skip direct on if cond
1 parent b86bb54 commit 4f3864c

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed
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\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class ComparedToEmptyString
6+
{
7+
public function run(string $name)
8+
{
9+
return substr($name, 2) === '';
10+
}
11+
12+
public function run2(string $name)
13+
{
14+
return substr($name, 2) !== '';
15+
}
16+
17+
public function run3(string $name)
18+
{
19+
return substr($name, 2) == '';
20+
}
21+
}
22+
23+
?>
24+
-----
25+
<?php
26+
27+
namespace Rector\Tests\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
28+
29+
class ComparedToEmptyString
30+
{
31+
public function run(string $name)
32+
{
33+
return (string) substr($name, 2) === '';
34+
}
35+
36+
public function run2(string $name)
37+
{
38+
return (string) substr($name, 2) !== '';
39+
}
40+
41+
public function run3(string $name)
42+
{
43+
return (string) substr($name, 2) == '';
44+
}
45+
}
46+
47+
?>
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\DowngradePhp80\Rector\FuncCall\DowngradeSubstrFalsyRector\Fixture;
4+
5+
class SkipDirectCond
6+
{
7+
public function run(string $name)
8+
{
9+
if (substr($name, 2)) {
10+
return true;
11+
}
12+
}
13+
}

rules/DowngradePhp80/Rector/FuncCall/DowngradeSubstrFalsyRector.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpParser\Node\Expr\MethodCall;
2020
use PhpParser\Node\Expr\StaticCall;
2121
use PhpParser\Node\Expr\Ternary;
22+
use PhpParser\Node\Stmt\If_;
2223
use PHPStan\Reflection\FunctionReflection;
2324
use PHPStan\Reflection\MethodReflection;
2425
use PHPStan\Type\Constant\ConstantIntegerType;
@@ -71,12 +72,13 @@ public function getNodeTypes(): array
7172
MethodCall::class,
7273
StaticCall::class,
7374
AssignOp::class,
75+
If_::class,
7476
FuncCall::class,
7577
];
7678
}
7779

7880
/**
79-
* @param Cast|Empty_|BooleanNot|Ternary|Identical|Concat|MethodCall|StaticCall|AssignOp|FuncCall $node
81+
* @param Cast|Empty_|BooleanNot|Ternary|Identical|Concat|MethodCall|StaticCall|AssignOp|If_|FuncCall $node
8082
*/
8183
public function refactor(Node $node): ?Node
8284
{
@@ -111,6 +113,11 @@ public function refactor(Node $node): ?Node
111113
return null;
112114
}
113115

116+
if ($node instanceof If_) {
117+
$node->cond->setAttribute(self::IS_UNCASTABLE, true);
118+
return null;
119+
}
120+
114121
if ($node instanceof CallLike) {
115122
if ($node->isFirstClassCallable()) {
116123
return null;

0 commit comments

Comments
 (0)