Skip to content

Commit 6e37640

Browse files
committed
[StrContainsRector] also replace multibyte functions fro strpos and strstr
1 parent 8e60090 commit 6e37640

5 files changed

Lines changed: 83 additions & 8 deletions

File tree

config/set/php85.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,5 @@
66
use Rector\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector;
77

88
return static function (RectorConfig $rectorConfig): void {
9-
$rectorConfig->rules(
10-
[
11-
ArrayFirstLastRector::class,
12-
]
13-
);
9+
$rectorConfig->rules([ArrayFirstLastRector::class]);
1410
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;
4+
5+
class MultibyteOffsetStrpos
6+
{
7+
public function run()
8+
{
9+
$isMatch = mb_strpos('abc', 'a', 1) !== false;
10+
}
11+
}
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;
17+
18+
class MultibyteOffsetStrpos
19+
{
20+
public function run()
21+
{
22+
$isMatch = str_contains(substr('abc', 1), 'a');
23+
}
24+
}
25+
?>
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\Php80\Rector\NotIdentical\StrContainsRector\Fixture;
4+
5+
class MultibyteStrposFunction
6+
{
7+
public function run()
8+
{
9+
$isMatch = mb_strpos('abc', 'a') !== false;
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;
18+
19+
class MultibyteStrposFunction
20+
{
21+
public function run()
22+
{
23+
$isMatch = str_contains('abc', 'a');
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\Php80\Rector\NotIdentical\StrContainsRector\Fixture;
4+
5+
class MultibyteStrstrFunction
6+
{
7+
public function run()
8+
{
9+
$isMatch = mb_strstr('abc', 'a') === false;
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;
18+
19+
class MultibyteStrstrFunction
20+
{
21+
public function run()
22+
{
23+
$isMatch = !str_contains('abc', 'a');
24+
}
25+
}
26+
27+
?>

rules/Php80/Rector/NotIdentical/StrContainsRector.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class StrContainsRector extends AbstractRector implements MinPhpVersionInt
3232
/**
3333
* @var string[]
3434
*/
35-
private const OLD_STR_NAMES = ['strpos', 'strstr'];
35+
private const OLD_STR_NAMES = ['mb_strpos', 'mb_strstr', 'strpos', 'strstr'];
3636

3737
public function __construct(
3838
private readonly ValueResolver $valueResolver
@@ -47,7 +47,7 @@ public function provideMinPhpVersion(): int
4747
public function getRuleDefinition(): RuleDefinition
4848
{
4949
return new RuleDefinition(
50-
'Replace strpos() !== false and strstr() with str_contains()',
50+
'Replace strpos|mb_strpos() !== false and strstr()|mb_strstr() with str_contains()',
5151
[
5252
new CodeSample(
5353
<<<'CODE_SAMPLE'
@@ -100,7 +100,7 @@ public function refactor(Node $node): ?Node
100100
if (isset($funcCall->getArgs()[2])) {
101101
$secondArg = $funcCall->getArgs()[2];
102102

103-
if ($this->isName($funcCall->name, 'strpos') && ! $this->isIntegerZero($secondArg->value)) {
103+
if ($this->isNames($funcCall->name, ['strpos', 'mb_strpos']) && ! $this->isIntegerZero($secondArg->value)) {
104104
$funcCall->args[0] = new Arg($this->nodeFactory->createFuncCall(
105105
'substr',
106106
[$funcCall->args[0], $secondArg]

0 commit comments

Comments
 (0)