Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class MultibyteOffsetStrpos
{
public function run()
{
$isMatch = mb_strpos('happy 😊 euro €', 'e', 8) !== false;
}
}
?>
-----
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class MultibyteOffsetStrpos
{
public function run()
{
$isMatch = str_contains(mb_substr('happy 😊 euro €', 8), 'e');
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class MultibyteStrposFunction
{
public function run()
{
$isMatch = mb_strpos('happy 😊 euro €', 'a') !== false;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class MultibyteStrposFunction
{
public function run()
{
$isMatch = str_contains('happy 😊 euro €', 'a');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class MultibyteStrstrFunction
{
public function run()
{
$isMatch = mb_strstr('happy 😊 euro €', 'a') === false;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class MultibyteStrstrFunction
{
public function run()
{
$isMatch = !str_contains('happy 😊 euro €', 'a');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class OffsetVariableMultibyteStrpos
{
public function run()
{
$offset = 1;
$isMatch = mb_strpos('happy 😊 euro €', 'a', $offset) != false;
}
}
?>
-----
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class OffsetVariableMultibyteStrpos
{
public function run()
{
$offset = 1;
$isMatch = str_contains(mb_substr('happy 😊 euro €', $offset), 'a');
}
}
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class ReplaceMultibyteStrposWithEncodingNull
{
public function run()
{
$isMatch = mb_strpos('happy 😊 euro €', 'a', 0, null) !== false;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class ReplaceMultibyteStrposWithEncodingNull
{
public function run()
{
$isMatch = str_contains('happy 😊 euro €', 'a');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class ReplaceMultibyteStrstrWithEncodingNull
{
public function run()
{
$isMatch = mb_strstr('happy 😊 euro €', 'a', false, null) !== false;
}
}

?>
-----
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class ReplaceMultibyteStrstrWithEncodingNull
{
public function run()
{
$isMatch = str_contains('happy 😊 euro €', 'a');
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class SkipMultibyteStrposWithEncoding
{
public function run()
{
$isMatch = mb_strpos('happy 😊 euro €', 'a', 0, 'UTF-8') !== false;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class SkipMultibyteStrstrWithDynamicEncoding
{
public function run(?string $encoding = 'UTF-8')
{
$isMatch = mb_strstr('happy 😊 euro €', 'a', false, $encoding) !== false;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\Php80\Rector\NotIdentical\StrContainsRector\Fixture;

class SkipMultibyteStrstrWithEncoding
{
public function run()
{
$isMatch = mb_strstr('happy 😊 euro €', 'a', false, 'UTF-8') !== false;
}
}

?>
16 changes: 12 additions & 4 deletions rules/Php80/Rector/NotIdentical/StrContainsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class StrContainsRector extends AbstractRector implements MinPhpVersionInt
/**
* @var string[]
*/
private const OLD_STR_NAMES = ['strpos', 'strstr'];
private const OLD_STR_NAMES = ['mb_strpos', 'mb_strstr', 'strpos', 'strstr'];

public function __construct(
private readonly ValueResolver $valueResolver
Expand All @@ -47,7 +47,7 @@ public function provideMinPhpVersion(): int
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace strpos() !== false and strstr() with str_contains()',
'Replace strpos|mb_strpos() !== false and strstr()|mb_strstr() with str_contains()',
[
new CodeSample(
<<<'CODE_SAMPLE'
Expand Down Expand Up @@ -97,12 +97,20 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->isNames($funcCall->name, ['mb_strpos', 'mb_strstr']) && isset($funcCall->getArgs()[3])) {
if (! $this->valueResolver->isNull($funcCall->getArgs()[3]->value)) {
return null;
}

unset($funcCall->args[3]);
}

if (isset($funcCall->getArgs()[2])) {
$secondArg = $funcCall->getArgs()[2];

if ($this->isName($funcCall->name, 'strpos') && ! $this->isIntegerZero($secondArg->value)) {
if ($this->isNames($funcCall->name, ['strpos', 'mb_strpos']) && ! $this->isIntegerZero($secondArg->value)) {
$funcCall->args[0] = new Arg($this->nodeFactory->createFuncCall(
'substr',
$this->isName($funcCall->name, 'strpos') ? 'substr' : 'mb_substr',
[$funcCall->args[0], $secondArg]
));
}
Expand Down