Skip to content

Commit d3b9088

Browse files
authored
[code-quality] Add StringCastAssertStringContainsStringRector (#498)
1 parent 3bc079d commit d3b9088

File tree

7 files changed

+195
-0
lines changed

7 files changed

+195
-0
lines changed

config/sets/phpunit-code-quality.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\NarrowSingleWillReturnCallbackRector;
3636
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\RemoveExpectAnyFromMockRector;
3737
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\SingleWithConsecutiveToWithRector;
38+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector;
3839
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWillMethodRector;
3940
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\UseSpecificWithMethodRector;
4041

@@ -63,6 +64,7 @@
6364

6465
// type declarations
6566
TypeWillReturnCallableArrowFunctionRector::class,
67+
StringCastAssertStringContainsStringRector::class,
6668

6769
NarrowUnusedSetUpDefinedPropertyRector::class,
6870

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class MyTest2 extends TestCase
8+
{
9+
public function test()
10+
{
11+
$this->assertTrue(\array_search($foo, $this->bar->toArray()));
12+
$this->assertNotFalse(\array_search($foo, $this->bar->toArray()));
13+
}
14+
}
15+
16+
?>
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\StringCastAssertStringContainsStringRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class SomeTest extends TestCase
8+
{
9+
public function testSomething(?string $value)
10+
{
11+
$this->assertStringContainsString('foo', $value);
12+
}
13+
}
14+
15+
?>
16+
-----
17+
<?php
18+
19+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\Fixture;
20+
21+
use PHPUnit\Framework\TestCase;
22+
23+
class SomeTest extends TestCase
24+
{
25+
public function testSomething(?string $value)
26+
{
27+
$this->assertStringContainsString('foo', (string) $value);
28+
}
29+
}
30+
31+
?>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class SkipKnownString extends TestCase
8+
{
9+
public function testSomething(string $value)
10+
{
11+
$this->assertStringContainsString('foo', $value);
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class StringCastAssertStringContainsStringRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(StringCastAssertStringContainsStringRector::class);
10+
};
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\Cast\String_;
9+
use PhpParser\Node\Expr\MethodCall;
10+
use PhpParser\Node\Expr\StaticCall;
11+
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
12+
use Rector\Rector\AbstractRector;
13+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
16+
/**
17+
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\StringCastAssertStringContainsStringRectorTest
18+
*/
19+
final class StringCastAssertStringContainsStringRector extends AbstractRector
20+
{
21+
public function __construct(
22+
private readonly TestsNodeAnalyzer $testsNodeAnalyzer
23+
) {
24+
}
25+
26+
public function getRuleDefinition(): RuleDefinition
27+
{
28+
return new RuleDefinition(
29+
'Cast 2nd argument in assertStringContainsString() to a string if not yet',
30+
[
31+
new CodeSample(
32+
<<<'CODE_SAMPLE'
33+
use PHPUnit\Framework\TestCase;
34+
35+
class SomeTest extends TestCase
36+
{
37+
public function testSomething(?string $value)
38+
{
39+
$this->assertStringContainsString('foo', $value);
40+
}
41+
}
42+
CODE_SAMPLE
43+
,
44+
<<<'CODE_SAMPLE'
45+
use PHPUnit\Framework\TestCase;
46+
47+
class SomeTest extends TestCase
48+
{
49+
public function testSomething(?string $value)
50+
{
51+
$this->assertStringContainsString('foo', (string) $value);
52+
}
53+
}
54+
CODE_SAMPLE
55+
),
56+
]
57+
);
58+
}
59+
60+
/**
61+
* @return array<class-string<MethodCall|StaticCall>>
62+
*/
63+
public function getNodeTypes(): array
64+
{
65+
return [MethodCall::class, StaticCall::class];
66+
}
67+
68+
/**
69+
* @param MethodCall|StaticCall $node
70+
*/
71+
public function refactor(Node $node): MethodCall|StaticCall|null
72+
{
73+
if ($node->isFirstClassCallable()) {
74+
return null;
75+
}
76+
77+
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames(
78+
$node,
79+
['assertStringContainsString', 'assertStringNotContainsString']
80+
)) {
81+
return null;
82+
}
83+
84+
$secondArg = $node->getArgs()[1];
85+
$secondArgType = $this->getType($secondArg->value);
86+
87+
if ($secondArgType->isString()->yes()) {
88+
return null;
89+
}
90+
91+
$secondArg->value = new String_($secondArg->value);
92+
93+
return null;
94+
}
95+
}

0 commit comments

Comments
 (0)