Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

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

class SkipSameLocalMethodCallChain
{
public function run($asset)
{
$this->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;
}
}
49 changes: 35 additions & 14 deletions rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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;
}
}
Loading