From 3c09e815ff210843887654471b37442b66d13bd6 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 30 Aug 2026 12:17:23 +0200 Subject: [PATCH] [CodeQuality] Skip same local method call chain in InlineIfToExplicitIfRector Extend the skip to flattened `&&`/`||` chains where every operand is a `$this->foo(...)` call of the same name, e.g. $this->validateExtensionAndMimeType($asset->getExtension(), $asset->loadFile()) && $this->validateExtensionAndMimeType($this->parseExtension($asset->getTempName()), $asset->loadFile(true)) && $this->validateExtensionAndMimeType($this->parseExtension($asset->getOriginalFileName()), null); Claude-Session: https://claude.ai/code/session_01D93PXDaqua1utm7H8KyJAN --- .../skip_same_local_method_call_chain.php.inc | 23 +++++++++ .../Expression/InlineIfToExplicitIfRector.php | 49 +++++++++++++------ 2 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 rules-tests/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector/Fixture/skip_same_local_method_call_chain.php.inc diff --git a/rules-tests/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector/Fixture/skip_same_local_method_call_chain.php.inc b/rules-tests/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector/Fixture/skip_same_local_method_call_chain.php.inc new file mode 100644 index 00000000000..7331cc7edd7 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector/Fixture/skip_same_local_method_call_chain.php.inc @@ -0,0 +1,23 @@ +validateExtensionAndMimeType($asset->getExtension(), $asset->loadFile()) + && $this->validateExtensionAndMimeType($this->parseExtension($asset->getTempName()), $asset->loadFile(true)) + && $this->validateExtensionAndMimeType($this->parseExtension($asset->getOriginalFileName()), null); + } + + private function validateExtensionAndMimeType($extension, $mimeType): bool + { + return $extension !== null && $mimeType !== null; + } + + private function parseExtension($name) + { + return $name; + } +} diff --git a/rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php b/rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php index 62c423f46e7..eabaf9b59b3 100644 --- a/rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php +++ b/rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php @@ -99,7 +99,7 @@ private function processExplicitIf(Expression $expression): ?Node return null; } - if ($this->isSameLocalMethodCall($booleanExpr->left, $booleanExpr->right)) { + if ($this->isSameLocalMethodCallChain($booleanExpr)) { return null; } @@ -119,24 +119,45 @@ private function processExplicitIf(Expression $expression): ?Node return $if; } - private function isSameLocalMethodCall(Expr $left, Expr $right): bool + private function isSameLocalMethodCallChain(BooleanAnd|BooleanOr $booleanExpr): bool { - if (! $left instanceof MethodCall) { - return false; + $leaves = []; + $this->collectOperands($booleanExpr, $leaves); + + $firstMethodCall = null; + foreach ($leaves as $leaf) { + if (! $leaf instanceof MethodCall) { + return false; + } + + if (! $leaf->var instanceof Variable || ! $this->isName($leaf->var, 'this')) { + return false; + } + + if (! $firstMethodCall instanceof MethodCall) { + $firstMethodCall = $leaf; + continue; + } + + if (! $this->nodeNameResolver->areNamesEqual($firstMethodCall->name, $leaf->name)) { + return false; + } } - if (! $right instanceof MethodCall) { - return false; - } - - if (! $left->var instanceof Variable || ! $this->isName($left->var, 'this')) { - return false; - } + return $firstMethodCall instanceof MethodCall; + } - if (! $right->var instanceof Variable || ! $this->isName($right->var, 'this')) { - return false; + /** + * @param Expr[] $leaves + */ + private function collectOperands(Expr $expr, array &$leaves): void + { + if ($expr instanceof BooleanAnd || $expr instanceof BooleanOr) { + $this->collectOperands($expr->left, $leaves); + $this->collectOperands($expr->right, $leaves); + return; } - return $this->nodeNameResolver->areNamesEqual($left->name, $right->name); + $leaves[] = $expr; } }