Skip to content

Commit b9d85b9

Browse files
authored
[Php80] Skip empty string needle in StrContainsRector (#8460)
1 parent 3e10647 commit b9d85b9

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

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\Php80\Rector\NotIdentical\StrContainsRector\Fixture;
4+
5+
class SkipEmptyNeedleStrpos
6+
{
7+
public function run()
8+
{
9+
return strpos('abc', '') !== 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\Php80\Rector\NotIdentical\StrContainsRector\Fixture;
4+
5+
class SkipEmptyNeedleStrstr
6+
{
7+
public function run()
8+
{
9+
return strstr('abc', '') !== false;
10+
}
11+
}

‎rules/Php80/Rector/NotIdentical/StrContainsRector.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PhpParser\Node\Expr\FuncCall;
1616
use PhpParser\Node\Name;
1717
use PhpParser\Node\Scalar\Int_;
18+
use PhpParser\Node\Scalar\String_;
1819
use Rector\Php80\NodeResolver\StrFalseComparisonResolver;
1920
use Rector\Rector\AbstractRector;
2021
use Rector\ValueObject\PhpVersionFeature;
@@ -97,6 +98,11 @@ public function refactor(Node $node): ?Node
9798
return null;
9899
}
99100

101+
$args = $funcCall->getArgs();
102+
if (isset($args[1]) && $this->isEmptyStringNeedle($args[1]->value)) {
103+
return null;
104+
}
105+
100106
if (isset($funcCall->getArgs()[2])) {
101107
$secondArg = $funcCall->getArgs()[2];
102108

@@ -132,4 +138,13 @@ private function isIntegerZero(Expr $expr): bool
132138

133139
return $expr->value === 0;
134140
}
141+
142+
private function isEmptyStringNeedle(Expr $expr): bool
143+
{
144+
if (! $expr instanceof String_) {
145+
return false;
146+
}
147+
148+
return $expr->value === '';
149+
}
135150
}

0 commit comments

Comments
 (0)