Skip to content

Commit d9c3582

Browse files
committed
Add str_contains() support to AssertTrueFalseToSpecificMethodRector
1 parent 4e82fcd commit d9c3582

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector\Fixture;
4+
5+
final class IncludeStrContains extends \PHPUnit\Framework\TestCase
6+
{
7+
public function test()
8+
{
9+
$this->assertTrue(str_contains('haystack', 'needle'));
10+
11+
$this->assertFalse(str_contains('haystack 2', 'needle 2'));
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector\Fixture;
20+
21+
final class IncludeStrContains extends \PHPUnit\Framework\TestCase
22+
{
23+
public function test()
24+
{
25+
$this->assertStringContainsString('needle', 'haystack');
26+
27+
$this->assertStringNotContainsString('needle 2', 'haystack 2');
28+
}
29+
}
30+
31+
?>

rules/CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ final class AssertTrueFalseToSpecificMethodRector extends AbstractRector
3939
'is_writable' => ['is_writable', 'assertIsWritable', 'assertNotIsWritable'],
4040
'is_nan' => ['is_nan', 'assertNan', ''],
4141
'is_a' => ['is_a', 'assertInstanceOf', 'assertNotInstanceOf'],
42+
'str_contains' => ['str_contains', 'assertStringContainsString', 'assertStringNotContainsString'],
4243
];
4344

4445
public function __construct(
@@ -181,6 +182,7 @@ private function moveFunctionArgumentsUp(MethodCall|StaticCall $node): void
181182
return;
182183
}
183184

185+
184186
$funcCallOrEmptyNodeArgs = $funcCallOrEmptyNode->getArgs();
185187
$oldArguments = $node->getArgs();
186188
unset($oldArguments[0]);
@@ -214,7 +216,8 @@ private function buildNewArguments(
214216
return [...$funcCallOrEmptyNodeArgs, ...$oldArguments];
215217
}
216218

217-
if ($funcCallOrEmptyNodeName === 'is_a') {
219+
if (in_array($funcCallOrEmptyNodeName, ['is_a', 'str_contains'])) {
220+
// flip arguments
218221
$newArgs = [$funcCallOrEmptyNodeArgs[1], $funcCallOrEmptyNodeArgs[0]];
219222

220223
return [...$newArgs, ...$oldArguments];

0 commit comments

Comments
 (0)