Updated Rector to commit 0dd1a43904e7d12c76e810b1bbd87d99a0f835c3

0dd1a43904 [DeadCode] Add new rule - ReduceAlwaysFalseIfOrRector (#5750)
This commit is contained in:
Tomas Votruba 2024-03-21 11:36:43 +00:00
parent 7a69e542ca
commit 50c2e61f1d
5 changed files with 80 additions and 2 deletions

View File

@ -0,0 +1,74 @@
<?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;
}
}

View File

@ -19,12 +19,12 @@ final class VersionResolver
* @api
* @var string
*/
public const PACKAGE_VERSION = '98a2a6fd6c6d1e2db5c0143e3e66e8421904b428';
public const PACKAGE_VERSION = '0dd1a43904e7d12c76e810b1bbd87d99a0f835c3';
/**
* @api
* @var string
*/
public const RELEASE_DATE = '2024-03-21 12:21:09';
public const RELEASE_DATE = '2024-03-21 12:34:15';
/**
* @var int
*/

View File

@ -28,6 +28,7 @@ use Rector\DeadCode\Rector\For_\RemoveDeadIfForeachForRector;
use Rector\DeadCode\Rector\For_\RemoveDeadLoopRector;
use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector;
use Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector;
use Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector;
use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector;
use Rector\DeadCode\Rector\If_\RemoveDeadInstanceOfRector;
use Rector\DeadCode\Rector\If_\RemoveTypedPropertyDeadInstanceOfRector;
@ -89,6 +90,7 @@ final class DeadCodeLevel
RemoveUselessVarTagRector::class,
RemovePhpVersionIdCheckRector::class,
RemoveAlwaysTrueIfConditionRector::class,
ReduceAlwaysFalseIfOrRector::class,
RemoveUnusedPrivateClassConstantRector::class,
RemoveUnusedPrivatePropertyRector::class,
RemoveDuplicatedCaseInSwitchRector::class,

View File

@ -1272,6 +1272,7 @@ return array(
'Rector\\DeadCode\\Rector\\For_\\RemoveDeadLoopRector' => $baseDir . '/rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php',
'Rector\\DeadCode\\Rector\\Foreach_\\RemoveUnusedForeachKeyRector' => $baseDir . '/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php',
'Rector\\DeadCode\\Rector\\FunctionLike\\RemoveDeadReturnRector' => $baseDir . '/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php',
'Rector\\DeadCode\\Rector\\If_\\ReduceAlwaysFalseIfOrRector' => $baseDir . '/rules/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveAlwaysTrueIfConditionRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveDeadInstanceOfRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveTypedPropertyDeadInstanceOfRector' => $baseDir . '/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php',

View File

@ -1491,6 +1491,7 @@ class ComposerStaticInit67be42e0079886f0083b7116ae1de531
'Rector\\DeadCode\\Rector\\For_\\RemoveDeadLoopRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/For_/RemoveDeadLoopRector.php',
'Rector\\DeadCode\\Rector\\Foreach_\\RemoveUnusedForeachKeyRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/Foreach_/RemoveUnusedForeachKeyRector.php',
'Rector\\DeadCode\\Rector\\FunctionLike\\RemoveDeadReturnRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php',
'Rector\\DeadCode\\Rector\\If_\\ReduceAlwaysFalseIfOrRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveAlwaysTrueIfConditionRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveAlwaysTrueIfConditionRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveDeadInstanceOfRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveDeadInstanceOfRector.php',
'Rector\\DeadCode\\Rector\\If_\\RemoveTypedPropertyDeadInstanceOfRector' => __DIR__ . '/../..' . '/rules/DeadCode/Rector/If_/RemoveTypedPropertyDeadInstanceOfRector.php',