From 1ba3d75d0d64e6ec1769537957693703acf1f082 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 30 Aug 2026 11:07:55 +0200 Subject: [PATCH] [CodeQuality] Skip same local method call in InlineIfToExplicitIfRector Skip when both sides of the boolean expression are `$this->foo(...)` calls of the same name, e.g. $this->validateMimeType($asset->getRemoteMimeTypeFromHeader()) && $this->validateMimeType($asset->getRemoteMimeTypeFromMagicBytes()); Claude-Session: https://claude.ai/code/session_01D93PXDaqua1utm7H8KyJAN --- .../skip_same_local_method_call.php.inc | 17 ++++++++++++ .../Expression/InlineIfToExplicitIfRector.php | 27 +++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 rules-tests/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector/Fixture/skip_same_local_method_call.php.inc diff --git a/rules-tests/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector/Fixture/skip_same_local_method_call.php.inc b/rules-tests/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector/Fixture/skip_same_local_method_call.php.inc new file mode 100644 index 00000000000..bb3ecd9cdc0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector/Fixture/skip_same_local_method_call.php.inc @@ -0,0 +1,17 @@ +validateMimeType($asset->getRemoteMimeTypeFromHeader()) + && $this->validateMimeType($asset->getRemoteMimeTypeFromMagicBytes()); + } + + private function validateMimeType($mimeType): bool + { + return $mimeType !== null; + } +} diff --git a/rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php b/rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php index cd7d2268b20..62c423f46e7 100644 --- a/rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php +++ b/rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php @@ -10,6 +10,8 @@ use PhpParser\Node\Expr\BinaryOp\BooleanOr; use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\FuncCall; +use PhpParser\Node\Expr\MethodCall; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Stmt\Expression; use PhpParser\Node\Stmt\If_; use Rector\NodeManipulator\BinaryOpManipulator; @@ -97,6 +99,10 @@ private function processExplicitIf(Expression $expression): ?Node return null; } + if ($this->isSameLocalMethodCall($booleanExpr->left, $booleanExpr->right)) { + return null; + } + $leftStaticType = $this->getType($booleanExpr->left); if (! $leftStaticType->isBoolean()->yes()) { return null; @@ -112,4 +118,25 @@ private function processExplicitIf(Expression $expression): ?Node $this->mirrorComments($if, $expression); return $if; } + + private function isSameLocalMethodCall(Expr $left, Expr $right): bool + { + if (! $left instanceof MethodCall) { + return false; + } + + if (! $right instanceof MethodCall) { + return false; + } + + if (! $left->var instanceof Variable || ! $this->isName($left->var, 'this')) { + return false; + } + + if (! $right->var instanceof Variable || ! $this->isName($right->var, 'this')) { + return false; + } + + return $this->nodeNameResolver->areNamesEqual($left->name, $right->name); + } }