-
Notifications
You must be signed in to change notification settings - Fork 60
[code-quality] Add StringCastAssertStringContainsStringRector #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
.../CodeQuality/Rector/MethodCall/AssertTrueFalseToSpecificMethodRector/Fixture/fixture2.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\AssertTrueFalseToSpecificMethodRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
final class MyTest2 extends TestCase | ||
{ | ||
public function test() | ||
{ | ||
$this->assertTrue(\array_search($foo, $this->bar->toArray())); | ||
$this->assertNotFalse(\array_search($foo, $this->bar->toArray())); | ||
} | ||
} | ||
|
||
?> |
31 changes: 31 additions & 0 deletions
31
...ector/MethodCall/StringCastAssertStringContainsStringRector/Fixture/repeated_same.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
public function testSomething(?string $value) | ||
{ | ||
$this->assertStringContainsString('foo', $value); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
public function testSomething(?string $value) | ||
{ | ||
$this->assertStringContainsString('foo', (string) $value); | ||
} | ||
} | ||
|
||
?> |
13 changes: 13 additions & 0 deletions
13
...r/MethodCall/StringCastAssertStringContainsStringRector/Fixture/skip_known_string.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\Fixture; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class SkipKnownString extends TestCase | ||
{ | ||
public function testSomething(string $value) | ||
{ | ||
$this->assertStringContainsString('foo', $value); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...ngCastAssertStringContainsStringRector/StringCastAssertStringContainsStringRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class StringCastAssertStringContainsStringRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...y/Rector/MethodCall/StringCastAssertStringContainsStringRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\PHPUnit\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(StringCastAssertStringContainsStringRector::class); | ||
}; |
95 changes: 95 additions & 0 deletions
95
rules/CodeQuality/Rector/MethodCall/StringCastAssertStringContainsStringRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\PHPUnit\CodeQuality\Rector\MethodCall; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Cast\String_; | ||
use PhpParser\Node\Expr\MethodCall; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\StringCastAssertStringContainsStringRector\StringCastAssertStringContainsStringRectorTest | ||
*/ | ||
final class StringCastAssertStringContainsStringRector extends AbstractRector | ||
{ | ||
public function __construct( | ||
private readonly TestsNodeAnalyzer $testsNodeAnalyzer | ||
) { | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition( | ||
'Cast 2nd argument in assertStringContainsString() to a string if not yet', | ||
[ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
public function testSomething(?string $value) | ||
{ | ||
$this->assertStringContainsString('foo', $value); | ||
} | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class SomeTest extends TestCase | ||
{ | ||
public function testSomething(?string $value) | ||
{ | ||
$this->assertStringContainsString('foo', (string) $value); | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @return array<class-string<MethodCall|StaticCall>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class, StaticCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall|StaticCall $node | ||
*/ | ||
public function refactor(Node $node): MethodCall|StaticCall|null | ||
{ | ||
if ($node->isFirstClassCallable()) { | ||
return null; | ||
} | ||
|
||
if (! $this->testsNodeAnalyzer->isPHPUnitMethodCallNames( | ||
$node, | ||
['assertStringContainsString', 'assertStringNotContainsString'] | ||
)) { | ||
return null; | ||
} | ||
|
||
$secondArg = $node->getArgs()[1]; | ||
$secondArgType = $this->getType($secondArg->value); | ||
|
||
if ($secondArgType->isString()->yes()) { | ||
return null; | ||
} | ||
|
||
$secondArg->value = new String_($secondArg->value); | ||
|
||
return null; | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TomasVotruba Was this file added intentionally? Due to its missing
.inc
extension, it is always removed after running tests locally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That seems due to run phpunit, and cut in the middle of process, then next is committed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #507