Skip to content

Commit

Permalink
[Naming] Remove BreakingVariableRenameGuard::isUsedInIfAndOtherBranch…
Browse files Browse the repository at this point in the history
…es() to remove previous by types (#4319)

* [Naming] Remove BreakingVariableRenameGuard::isUsedInIfAndOtherBranches() to remove previous by types

* [ci-review] Rector Rectify

* remove also

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Jun 22, 2023
1 parent 1311a71 commit 5c1fd4e
Showing 1 changed file with 1 addition and 63 deletions.
64 changes: 1 addition & 63 deletions rules/Naming/Guard/BreakingVariableRenameGuard.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@

use DateTimeInterface;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Error;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PHPStan\Analyser\Scope;
use PHPStan\Type\ObjectType;
use PHPStan\Type\TypeWithClassName;
Expand Down Expand Up @@ -78,15 +74,7 @@ public function shouldSkipVariable(
return true;
}

if ($this->isUsedInClosureUsesName($expectedName, $functionLike)) {
return true;
}

if ($this->isUsedInForeachKeyValueVar($variable, $currentName)) {
return true;
}

return $this->isUsedInIfAndOtherBranches($variable, $currentName);
return $this->isUsedInClosureUsesName($expectedName, $functionLike);
}

public function shouldSkipParam(
Expand Down Expand Up @@ -176,56 +164,6 @@ private function isUsedInClosureUsesName(
return $this->betterNodeFinder->hasVariableOfName($functionLike->uses, $expectedName);
}

private function isUsedInForeachKeyValueVar(Variable $variable, string $currentName): bool
{
$previousForeach = $this->betterNodeFinder->findFirstPreviousOfTypes($variable, [Foreach_::class]);
if ($previousForeach instanceof Foreach_) {
if ($previousForeach->keyVar === $variable) {
return false;
}

if ($previousForeach->valueVar === $variable) {
return false;
}

if ($this->nodeNameResolver->isName($previousForeach->valueVar, $currentName)) {
return true;
}

if (! $previousForeach->keyVar instanceof Expr) {
return false;
}

if ($this->nodeNameResolver->isName($previousForeach->keyVar, $currentName)) {
return true;
}
}

return false;
}

private function isUsedInIfAndOtherBranches(Variable $variable, string $currentVariableName): bool
{
// is in if branches?
$previousIf = $this->betterNodeFinder->findFirstPreviousOfTypes($variable, [If_::class]);
if ($previousIf instanceof If_) {
$variableUses = [];

$variableUses[] = $this->betterNodeFinder->findVariableOfName($previousIf->stmts, $currentVariableName);

$previousStmts = $previousIf->else instanceof Else_ ? $previousIf->else->stmts : [];
$variableUses[] = $this->betterNodeFinder->findVariableOfName($previousStmts, $currentVariableName);
$variableUses[] = $this->betterNodeFinder->findVariableOfName($previousIf->elseifs, $currentVariableName);

$variableUses = array_filter($variableUses);
if (count($variableUses) > 1) {
return true;
}
}

return false;
}

private function isRamseyUuidInterface(Param $param): bool
{
return $this->nodeTypeResolver->isObjectType($param, new ObjectType('Ramsey\Uuid\UuidInterface'));
Expand Down

0 comments on commit 5c1fd4e

Please sign in to comment.