Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions rules/CodeQuality/NodeAnalyzer/ExplicitBoolConditionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
namespace Rector\CodeQuality\NodeAnalyzer;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Cast\Bool_;
use PhpParser\Node\Expr\Empty_;
use PhpParser\Node\Expr\Instanceof_;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
Expand Down Expand Up @@ -40,11 +44,24 @@ public function resolve(If_|ElseIf_|Ternary $node): ?ExplicitBoolCondition
return null;
}

// cheap bail - these shapes can never be a nullable object, skip costly native type resolution
if ($this->isNeverNullableObject($conditionNode)) {
return null;
}

$conditionStaticType = $this->nodeTypeResolver->getNativeType($conditionNode);
if ($conditionStaticType instanceof MixedType || $conditionStaticType->isBoolean()->yes()) {
return null;
}

return new ExplicitBoolCondition($conditionNode, $isNegated);
}

private function isNeverNullableObject(Expr $expr): bool
{
return $expr instanceof BinaryOp
|| $expr instanceof Instanceof_
|| $expr instanceof Empty_
|| $expr instanceof Isset_;
}
}
Loading