Skip to content

Commit

Permalink
[DeadCode] Skip From docblock on ReduceAlwaysFalseIfOrRector (#5752)
Browse files Browse the repository at this point in the history
* [DeadCode] Skip From docblock on ReduceAlwaysFalseIfOrRector

* Fix

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Mar 21, 2024
1 parent aaf006c commit f32dff3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\Tests\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector\Fixture;

class SkipFromDocblock
{
/**
* @param int $number
*/
public function run($number)
{
if (! is_int($number) || $number > 50) {
return 'yes';
}

return 'no';
}
}
19 changes: 19 additions & 0 deletions rules/DeadCode/Rector/If_/ReduceAlwaysFalseIfOrRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\Constant\ConstantBooleanType;
use Rector\NodeAnalyzer\ExprAnalyzer;
use Rector\PhpParser\Node\BetterNodeFinder;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -17,6 +20,13 @@
*/
final class ReduceAlwaysFalseIfOrRector extends AbstractRector
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly ExprAnalyzer $exprAnalyzer
)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Reduce always false in a if ( || ) condition', [
Expand Down Expand Up @@ -81,6 +91,15 @@ public function refactor(Node $node): ?Node
return null;
}

$hasNonTypedFromParam = $this->betterNodeFinder->findFirst(
$booleanOr->left,
fn (Node $node): bool => $node instanceof Variable && $this->exprAnalyzer->isNonTypedFromParam($node)
);

if ($hasNonTypedFromParam instanceof Node) {
return null;
}

$node->cond = $booleanOr->right;

return $node;
Expand Down

0 comments on commit f32dff3

Please sign in to comment.