From 8332d8cc107740ef1e5e51567f6869e2cca2ea6e Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Thu, 28 Apr 2022 23:47:38 +0200 Subject: [PATCH] remove unused unwrapExpression(), use direct instanceof compare instead (#2186) --- .../If_/SimplifyIfElseToTernaryRector.php | 24 +++++++++++++------ .../NewlineBeforeNewAssignSetRector.php | 15 ++++++++---- .../RemoveDelegatingParentCallRector.php | 7 +++++- src/Rector/AbstractRector.php | 9 ------- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php b/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php index 6b402b52b4c..de83774a908 100644 --- a/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php +++ b/rules/CodeQuality/Rector/If_/SimplifyIfElseToTernaryRector.php @@ -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; } @@ -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; } /** @@ -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; } /** diff --git a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php index 6220a24442e..2cea7b2b8d2 100644 --- a/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php +++ b/rules/CodingStyle/Rector/ClassMethod/NewlineBeforeNewAssignSetRector.php @@ -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; @@ -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); } } diff --git a/rules/DeadCode/Rector/ClassMethod/RemoveDelegatingParentCallRector.php b/rules/DeadCode/Rector/ClassMethod/RemoveDelegatingParentCallRector.php index 168d5a789d3..72f89f03642 100644 --- a/rules/DeadCode/Rector/ClassMethod/RemoveDelegatingParentCallRector.php +++ b/rules/DeadCode/Rector/ClassMethod/RemoveDelegatingParentCallRector.php @@ -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; @@ -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; } } diff --git a/src/Rector/AbstractRector.php b/src/Rector/AbstractRector.php index 0604a2003d1..2d6b3f3bded 100644 --- a/src/Rector/AbstractRector.php +++ b/src/Rector/AbstractRector.php @@ -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);