Skip to content

Commit

Permalink
[CodeQuality] Skip InlineIfToExplicitIfRector on constant definition (#…
Browse files Browse the repository at this point in the history
…422)

* [CodeQuality] Skip InlineIfToExplicitIfRector on constant definition

* handle boolean not left ! define()

* eol

* handle boolean not left ! define()

* clean up
  • Loading branch information
samsonasik authored Jul 11, 2021
1 parent b8488b6 commit fb9ad83
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Expression\InlineIfToExplicitIfRector\Fixture;

defined('SHOW_DEBUG_BACKTRACE') || define('SHOW_DEBUG_BACKTRACE', true);
! defined('SHOW_DEBUG_BACKTRACE') && define('SHOW_DEBUG_BACKTRACE', true);
10 changes: 10 additions & 0 deletions rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\BinaryOp\BooleanOr;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\BooleanType;
Expand Down Expand Up @@ -93,6 +95,14 @@ private function processExplicitIf(Expression $expression): ?Node
return null;
}

$exprLeft = $booleanExpr->left instanceof BooleanNot
? $booleanExpr->left->expr
: $booleanExpr->left;

if ($exprLeft instanceof FuncCall && $this->isName($exprLeft, 'defined')) {
return null;
}

/** @var Expr $expr */
$expr = $booleanExpr instanceof BooleanAnd
? $booleanExpr->left
Expand Down

0 comments on commit fb9ad83

Please # to comment.