-
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add SimplifyFalseComparisonToNegateRector
- Loading branch information
1 parent
5be9bcc
commit a7afe1f
Showing
9 changed files
with
151 additions
and
13 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
packages/CodeQuality/src/Rector/Identical/SimplifyIdenticalFalseToBooleanNotRector.php
This file contains 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,81 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\Identical; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\BinaryOp; | ||
use PhpParser\Node\Expr\BinaryOp\Identical; | ||
use PhpParser\Node\Expr\BooleanNot; | ||
use Rector\NodeAnalyzer\ConstFetchAnalyzer; | ||
use Rector\Rector\AbstractRector; | ||
use Rector\RectorDefinition\CodeSample; | ||
use Rector\RectorDefinition\RectorDefinition; | ||
|
||
final class SimplifyIdenticalFalseToBooleanNotRector extends AbstractRector | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
TomasVotruba
Author
Member
|
||
{ | ||
/** | ||
* @var ConstFetchAnalyzer | ||
*/ | ||
private $constFetchAnalyzer; | ||
|
||
public function __construct(ConstFetchAnalyzer $constFetchAnalyzer) | ||
{ | ||
$this->constFetchAnalyzer = $constFetchAnalyzer; | ||
} | ||
|
||
public function getDefinition(): RectorDefinition | ||
{ | ||
return new RectorDefinition('Changes === false to negate !', [ | ||
new CodeSample('if ($something === false) {}', 'if (! $something) {}'), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [Identical::class]; | ||
} | ||
|
||
/** | ||
* @param Identical $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->constFetchAnalyzer->isFalse($node->right)) { | ||
$comparedNode = $node->left; | ||
$shouldUnwrap = $node->left instanceof BooleanNot; | ||
} elseif ($this->constFetchAnalyzer->isFalse($node->left)) { | ||
$comparedNode = $node->right; | ||
$shouldUnwrap = $node->right instanceof BooleanNot; | ||
} else { | ||
return $node; | ||
} | ||
|
||
if ($shouldUnwrap) { | ||
/** @var BooleanNot $comparedNode */ | ||
$comparedNode = $comparedNode->expr; | ||
if ($this->shouldSkip($comparedNode)) { | ||
return $node; | ||
} | ||
|
||
return $comparedNode; | ||
} | ||
|
||
if ($this->shouldSkip($comparedNode)) { | ||
return $node; | ||
} | ||
|
||
return new BooleanNot($comparedNode); | ||
} | ||
|
||
private function shouldSkip(Node $node): bool | ||
{ | ||
if ($node instanceof BinaryOp) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...y/tests/Rector/Identical/SimplifyIdenticalFalseToBooleanNotRector/Correct/correct.php.inc
This file contains 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,11 @@ | ||
<?php | ||
|
||
if (!$something) {} | ||
|
||
if (!$something) {} | ||
|
||
if ($something) {} | ||
|
||
if ((! $one && $two) === false) { | ||
return null; | ||
} |
30 changes: 30 additions & 0 deletions
30
...SimplifyIdenticalFalseToBooleanNotRector/SimplifyIdenticalFalseToBooleanNotRectorTest.php
This file contains 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,30 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Tests\Rector\Identical\SimplifyIdenticalFalseToBooleanNotRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
/** | ||
* @covers \Rector\CodeQuality\Rector\Identical\SimplifyIdenticalFalseToBooleanNotRector | ||
*/ | ||
final class SimplifyIdenticalFalseToBooleanNotRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideWrongToFixedFiles() | ||
*/ | ||
public function test(string $wrong, string $fixed): void | ||
{ | ||
$this->doTestFileMatchesExpectedContent($wrong, $fixed); | ||
} | ||
|
||
public function provideWrongToFixedFiles(): Iterator | ||
{ | ||
yield [__DIR__ . '/Wrong/wrong.php.inc', __DIR__ . '/Correct/correct.php.inc']; | ||
} | ||
|
||
protected function provideConfig(): string | ||
{ | ||
return __DIR__ . '/config.yml'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ality/tests/Rector/Identical/SimplifyIdenticalFalseToBooleanNotRector/Wrong/wrong.php.inc
This file contains 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,11 @@ | ||
<?php | ||
|
||
if ($something === false) {} | ||
|
||
if (false === $something) {} | ||
|
||
if (! $something === false) {} | ||
|
||
if ((! $one && $two) === false) { | ||
return null; | ||
} |
2 changes: 2 additions & 0 deletions
2
...es/CodeQuality/tests/Rector/Identical/SimplifyIdenticalFalseToBooleanNotRector/config.yml
This file contains 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,2 @@ | ||
services: | ||
Rector\CodeQuality\Rector\Identical\SimplifyIdenticalFalseToBooleanNotRector: ~ |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This is pure Coding Standards, but okay