-
-
Notifications
You must be signed in to change notification settings - Fork 742
[NetteUtilsCodeQuality] Fixes #4686 Add SubstrMinusToStringEndsWithRector #4687
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
0b350e4
[NetteUtilsCodeQuality] Fixes #4686 Add SubstrMinusToStringEndsWithRe…
samsonasik 8f2109a
use FullyQualified
samsonasik 2fa5bd2
handle identical
samsonasik a28b196
failing fixture inside if
samsonasik 878014b
implemented
samsonasik 6dcc9fc
clean up
samsonasik e124590
clean up
samsonasik c37a6ae
clean up
samsonasik 40c9cb9
check different length for "string" value
samsonasik 4fed266
fixture check different length for "string" value
samsonasik b82e3dd
[ci-review] Rector Rectify
rector-bot 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
97 changes: 97 additions & 0 deletions
97
rules/nette-code-quality/src/Rector/Identical/SubstrMinusToStringEndsWithRector.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,97 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\NetteCodeQuality\Rector\Identical; | ||
|
|
||
| use Nette\Utils\Strings; | ||
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr\BinaryOp\Identical; | ||
| use PhpParser\Node\Expr\BinaryOp\NotIdentical; | ||
| use PhpParser\Node\Expr\BooleanNot; | ||
| use PhpParser\Node\Expr\UnaryMinus; | ||
| use PhpParser\Node\Scalar\LNumber; | ||
| use PhpParser\Node\Scalar\String_; | ||
| use Rector\Core\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\NetteCodeQuality\Tests\Rector\Identical\SubstrMinusToStringEndsWithRector\SubstrMinusToStringEndsWithRectorTest | ||
| */ | ||
| final class SubstrMinusToStringEndsWithRector extends AbstractRector | ||
| { | ||
| /** | ||
| * @var string | ||
| */ | ||
| private const SUBSTR = 'substr'; | ||
|
|
||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition( | ||
| 'Change substr function with minus to Strings::endsWith()', | ||
| [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| substr($var, -4) !== 'Test'; | ||
| substr($var, -4) === 'Test'; | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| ! \Nette\Utils\Strings::endsWith($var, 'Test'); | ||
| \Nette\Utils\Strings::endsWith($var, 'Test'); | ||
| CODE_SAMPLE | ||
| ), | ||
|
|
||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [Identical::class, NotIdentical::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param Identical|NotIdentical $node | ||
| */ | ||
| public function refactor(Node $node): ?Node | ||
| { | ||
| if (! $this->isFuncCallName($node->left, self::SUBSTR) && ! $this->isFuncCallName($node->right, self::SUBSTR)) { | ||
| return null; | ||
| } | ||
|
|
||
| $substr = $this->isFuncCallName($node->left, self::SUBSTR) | ||
| ? $node->left | ||
| : $node->right; | ||
|
|
||
| if (! $substr->args[1]->value instanceof UnaryMinus) { | ||
| return null; | ||
| } | ||
|
|
||
| /** @var UnaryMinus $unaryMinus */ | ||
| $unaryMinus = $substr->args[1]->value; | ||
| if (! $unaryMinus->expr instanceof LNumber) { | ||
| return null; | ||
| } | ||
|
|
||
| $string = $this->isFuncCallName($node->left, self::SUBSTR) | ||
| ? $node->right | ||
| : $node->left; | ||
|
|
||
| $wordLength = $unaryMinus->expr->value; | ||
| if ($string instanceof String_ && strlen($string->value) !== $wordLength) { | ||
| return null; | ||
| } | ||
|
|
||
| $staticCall = $this->createStaticCall(Strings::class, 'endsWith', [$substr->args[0]->value, $string]); | ||
|
|
||
| if ($node instanceof Identical) { | ||
| return $staticCall; | ||
| } | ||
|
|
||
| return new BooleanNot($staticCall); | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
...-quality/tests/Rector/Identical/SubstrMinusToStringEndsWithRector/Fixture/fixture.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,27 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\NetteCodeQuality\Tests\Rector\Identical\SubstrMinusToStringEndsWithRector\Fixture; | ||
|
|
||
| function check($var) | ||
| { | ||
| substr($var, -4) !== 'Test'; | ||
| 'Test' !== substr($var, -4); | ||
| substr($var, -4) === 'Test'; | ||
| 'Test' === substr($var, -4); | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\NetteCodeQuality\Tests\Rector\Identical\SubstrMinusToStringEndsWithRector\Fixture; | ||
|
|
||
| function check($var) | ||
| { | ||
| !\Nette\Utils\Strings::endsWith($var, 'Test'); | ||
| !\Nette\Utils\Strings::endsWith($var, 'Test'); | ||
| \Nette\Utils\Strings::endsWith($var, 'Test'); | ||
| \Nette\Utils\Strings::endsWith($var, 'Test'); | ||
| } | ||
|
|
||
| ?> |
49 changes: 49 additions & 0 deletions
49
...uality/tests/Rector/Identical/SubstrMinusToStringEndsWithRector/Fixture/inside_if.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,49 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\NetteCodeQuality\Tests\Rector\Identical\SubstrMinusToStringEndsWithRector\Fixture; | ||
|
|
||
| function insideIf($var) | ||
| { | ||
| if (substr($var, -4) !== 'Test') { | ||
|
|
||
| } | ||
|
|
||
| if ('Test' !== substr($var, -4)) { | ||
|
|
||
| } | ||
|
|
||
| if (substr($var, -4) === 'Test') { | ||
|
|
||
| } | ||
|
|
||
| if ('Test' === substr($var, -4)) { | ||
|
|
||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\NetteCodeQuality\Tests\Rector\Identical\SubstrMinusToStringEndsWithRector\Fixture; | ||
|
|
||
| function insideIf($var) | ||
| { | ||
| if (!\Nette\Utils\Strings::endsWith($var, 'Test')) { | ||
|
|
||
| } | ||
|
|
||
| if (!\Nette\Utils\Strings::endsWith($var, 'Test')) { | ||
|
|
||
| } | ||
|
|
||
| if (\Nette\Utils\Strings::endsWith($var, 'Test')) { | ||
|
|
||
| } | ||
|
|
||
| if (\Nette\Utils\Strings::endsWith($var, 'Test')) { | ||
|
|
||
| } | ||
| } | ||
|
|
||
| ?> |
10 changes: 10 additions & 0 deletions
10
.../Rector/Identical/SubstrMinusToStringEndsWithRector/Fixture/skip_different_length.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,10 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\NetteCodeQuality\Tests\Rector\Identical\SubstrMinusToStringEndsWithRector\Fixture; | ||
|
|
||
| function skipDifferentLength($var) | ||
| { | ||
| 'part' !== substr($var, -3); | ||
| } | ||
|
|
||
| ?> |
10 changes: 10 additions & 0 deletions
10
.../tests/Rector/Identical/SubstrMinusToStringEndsWithRector/Fixture/skip_not_substr.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,10 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\NetteCodeQuality\Tests\Rector\Identical\SubstrMinusToStringEndsWithRector\Fixture; | ||
|
|
||
| function skipNotSustr($var) | ||
| { | ||
| strlen($var) === 5; | ||
| } | ||
|
|
||
| ?> |
27 changes: 27 additions & 0 deletions
27
...y/tests/Rector/Identical/SubstrMinusToStringEndsWithRector/Fixture/with_var_value.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,27 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\NetteCodeQuality\Tests\Rector\Identical\SubstrMinusToStringEndsWithRector\Fixture; | ||
|
|
||
| function withVarValue($var, $value) | ||
| { | ||
| substr($var, -4) !== $value; | ||
| $value !== substr($var, -4); | ||
| substr($var, -4) === $value; | ||
| $value === substr($var, -4); | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\NetteCodeQuality\Tests\Rector\Identical\SubstrMinusToStringEndsWithRector\Fixture; | ||
|
|
||
| function withVarValue($var, $value) | ||
| { | ||
| !\Nette\Utils\Strings::endsWith($var, $value); | ||
| !\Nette\Utils\Strings::endsWith($var, $value); | ||
| \Nette\Utils\Strings::endsWith($var, $value); | ||
| \Nette\Utils\Strings::endsWith($var, $value); | ||
| } | ||
|
|
||
| ?> | ||
31 changes: 31 additions & 0 deletions
31
...tor/Identical/SubstrMinusToStringEndsWithRector/SubstrMinusToStringEndsWithRectorTest.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,31 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\NetteCodeQuality\Tests\Rector\Identical\SubstrMinusToStringEndsWithRector; | ||
|
|
||
| use Iterator; | ||
| use Rector\NetteCodeQuality\Rector\Identical\SubstrMinusToStringEndsWithRector; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
| use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
|
||
| final class SubstrMinusToStringEndsWithRectorTest extends AbstractRectorTestCase | ||
| { | ||
| /** | ||
| * @dataProvider provideData() | ||
| */ | ||
| public function test(SmartFileInfo $fileInfo): void | ||
| { | ||
| $this->doTestFileInfo($fileInfo); | ||
| } | ||
|
|
||
| public function provideData(): Iterator | ||
| { | ||
| return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| protected function getRectorClass(): string | ||
| { | ||
| return SubstrMinusToStringEndsWithRector::class; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.