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,17 @@
<?php

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

class SkipSameLocalMethodCall
{
public function run($asset)
{
$this->validateMimeType($asset->getRemoteMimeTypeFromHeader())
&& $this->validateMimeType($asset->getRemoteMimeTypeFromMagicBytes());
}

private function validateMimeType($mimeType): bool
{
return $mimeType !== null;
}
}
27 changes: 27 additions & 0 deletions rules/CodeQuality/Rector/Expression/InlineIfToExplicitIfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
Loading