-
-
Notifications
You must be signed in to change notification settings - Fork 351
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Add new rule - ReduceAlwaysFalseIfOrRector (#5750)
* [DeadCode] Add ReduceAlwaysFalseIfOrRector * add to dead-code ruleset
- Loading branch information
1 parent
98a2a6f
commit 0dd1a43
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/Fixture/some_class.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,35 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run(int $number) | ||
{ | ||
if (! is_int($number) || $number > 50) { | ||
return 'yes'; | ||
} | ||
|
||
return 'no'; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run(int $number) | ||
{ | ||
if ($number > 50) { | ||
return 'yes'; | ||
} | ||
|
||
return 'no'; | ||
} | ||
} | ||
|
||
?> |
28 changes: 28 additions & 0 deletions
28
...tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/ReduceAlwaysFalseIfOrRectorTest.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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector; | ||
|
||
use Iterator; | ||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class ReduceAlwaysFalseIfOrRectorTest 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
rules-tests/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector/config/configured_rule.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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\Config\RectorConfig; | ||
use Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(ReduceAlwaysFalseIfOrRector::class); | ||
}; |
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,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\Rector\If_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\BinaryOp\BooleanOr; | ||
use PhpParser\Node\Stmt\If_; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use Rector\Rector\AbstractRector; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\ReduceAlwaysFalseIfOrRectorTest | ||
*/ | ||
final class ReduceAlwaysFalseIfOrRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Reduce always false in a if ( || ) condition', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run(int $number) | ||
{ | ||
if (! is_int($number) || $number > 50) { | ||
return 'yes'; | ||
} | ||
return 'no'; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run(int $number) | ||
{ | ||
if ($number > 50) { | ||
return 'yes'; | ||
} | ||
return 'no'; | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [If_::class]; | ||
} | ||
|
||
/** | ||
* @param If_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if (! $node->cond instanceof BooleanOr) { | ||
return null; | ||
} | ||
|
||
$booleanOr = $node->cond; | ||
|
||
$conditionStaticType = $this->getType($booleanOr->left); | ||
if (! $conditionStaticType instanceof ConstantBooleanType) { | ||
return null; | ||
} | ||
|
||
if ($conditionStaticType->getValue()) { | ||
return null; | ||
} | ||
|
||
$node->cond = $booleanOr->right; | ||
|
||
return $node; | ||
} | ||
} |
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