Skip to content

Commit

Permalink
remove unused unwrapExpression(), use direct instanceof compare inste…
Browse files Browse the repository at this point in the history
…ad (#2186)
  • Loading branch information
TomasVotruba authored Apr 28, 2022
1 parent fb722fc commit 8332d8c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 22 deletions.
24 changes: 17 additions & 7 deletions rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ public function refactor(Node $node): ?Node
}

$ifAssignVar = $this->resolveOnlyStmtAssignVar($node->stmts);
$elseAssignVar = $this->resolveOnlyStmtAssignVar($node->else->stmts);
if (! $ifAssignVar instanceof Expr) {
return null;
}

$elseAssignVar = $this->resolveOnlyStmtAssignVar($node->else->stmts);
if (! $elseAssignVar instanceof Expr) {
return null;
}
Expand Down Expand Up @@ -169,12 +169,17 @@ private function resolveOnlyStmtAssignVar(array $stmts): ?Expr
return null;
}

$onlyStmt = $this->unwrapExpression($stmts[0]);
if (! $onlyStmt instanceof Assign) {
$stmt = $stmts[0];
if (! $stmt instanceof Expression) {
return null;
}

$stmtExpr = $stmt->expr;
if (! $stmtExpr instanceof Assign) {
return null;
}

return $onlyStmt->var;
return $stmtExpr->var;
}

/**
Expand All @@ -186,12 +191,17 @@ private function resolveOnlyStmtAssignExpr(array $stmts): ?Expr
return null;
}

$onlyStmt = $this->unwrapExpression($stmts[0]);
if (! $onlyStmt instanceof Assign) {
$stmt = $stmts[0];
if (! $stmt instanceof Expression) {
return null;
}

$stmtExpr = $stmt->expr;
if (! $stmtExpr instanceof Assign) {
return null;
}

return $onlyStmt->expr;
return $stmtExpr->expr;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Nop;
use Rector\Core\Rector\AbstractRector;
Expand Down Expand Up @@ -112,15 +113,19 @@ private function reset(): void

private function resolveCurrentStmtVariableName(Stmt $stmt): ?string
{
$stmt = $this->unwrapExpression($stmt);
if (! $stmt instanceof Expression) {
return null;
}

$stmtExpr = $stmt->expr;

if ($stmt instanceof Assign || $stmt instanceof MethodCall) {
if ($this->shouldSkipLeftVariable($stmt)) {
if ($stmtExpr instanceof Assign || $stmtExpr instanceof MethodCall) {
if ($this->shouldSkipLeftVariable($stmtExpr)) {
return null;
}

if (! $stmt->var instanceof MethodCall && ! $stmt->var instanceof StaticCall) {
return $this->getName($stmt->var);
if (! $stmtExpr->var instanceof MethodCall && ! $stmtExpr->var instanceof StaticCall) {
return $this->getName($stmtExpr->var);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Rector\AbstractRector;
use Rector\DeadCode\Comparator\CurrentAndParentClassMethodComparator;
Expand Down Expand Up @@ -174,7 +175,11 @@ private function matchClassMethodOnlyStmt(ClassMethod $classMethod): null | Stmt

// recount empty notes
$stmtsValues = array_values($classMethodStmts);
$stmtValue = $stmtsValues[0];
if ($stmtValue instanceof Expression) {
return $stmtValue->expr;
}

return $this->unwrapExpression($stmtsValues[0]);
return $stmtValue;
}
}
9 changes: 0 additions & 9 deletions src/Rector/AbstractRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,6 @@ protected function appendArgs(array $currentArgs, array $appendingArgs): array
return $currentArgs;
}

protected function unwrapExpression(Stmt $stmt): Expr | Stmt
{
if ($stmt instanceof Expression) {
return $stmt->expr;
}

return $stmt;
}

protected function removeNode(Node $node): void
{
$this->nodeRemover->removeNode($node);
Expand Down

0 comments on commit 8332d8c

Please sign in to comment.