Skip to content

Commit

Permalink
Closes #5716 Fixes #5715
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Mar 1, 2021
1 parent c21ad29 commit 11e8e6e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ public function refactor(Node $node): ?Node
return null;
}

if ($this->isFoundInPreviousNode($node)) {
return null;
}

if ($this->isUsedNextOrPreviousAssignVar($node, $camelCaseName)) {
if ($this->isUsedNextPreviousAssignVar($node, $camelCaseName)) {
return null;
}

Expand All @@ -120,29 +116,48 @@ public function refactor(Node $node): ?Node
return $node;
}

private function isUsedNextOrPreviousAssignVar(Variable $variable, string $camelCaseName): bool
private function isUsedNextPreviousAssignVar(Variable $variable, string $camelCaseName): bool
{
$variableMethodNode = $variable->getAttribute(AttributeKey::METHOD_NAME);
$hasNext = (bool) $this->betterNodeFinder->findFirstNext($variable, function (Node $node) use ($variableMethodNode, $camelCaseName): bool {
$parent = $variable->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Assign) {
return false;
}

if ($parent->var !== $variable) {
return false;
}

$variableMethodNode = $variable->getAttribute(AttributeKey::METHOD_NODE);
if (! $variableMethodNode instanceof Node) {
return false;
}

$usedInNext = (bool) $this->betterNodeFinder->findFirstNext($variable, function (Node $node) use (
$variableMethodNode,
$camelCaseName
): bool {
return $this->hasEqualVariable($node, $variableMethodNode, $camelCaseName);
});

if ($hasNext) {
if ($usedInNext) {
return true;
}

return (bool) $this->betterNodeFinder->findFirstPreviousOfNode($variable, function (Node $node) use ($variableMethodNode, $camelCaseName): bool {
return (bool) $this->betterNodeFinder->findFirstPreviousOfNode($variable, function (Node $node) use (
$variableMethodNode,
$camelCaseName
): bool {
return $this->hasEqualVariable($node, $variableMethodNode, $camelCaseName);
});
}

private function hasEqualVariable(Node $node, ?string $variableMethodNode, string $camelCaseName): bool
private function hasEqualVariable(Node $node, ?Node $variableMethodNode, string $camelCaseName): bool
{
if (! $node instanceof Variable) {
return false;
}

$methodNode = $node->getAttribute(AttributeKey::METHOD_NAME);
$methodNode = $node->getAttribute(AttributeKey::METHOD_NODE);
if ($variableMethodNode !== $methodNode) {
return false;
}
Expand Down Expand Up @@ -191,13 +206,4 @@ private function isFoundInParentNode(Variable $variable): bool

return false;
}

private function isFoundInPreviousNode(Variable $variable): bool
{
$previousNode = $variable->getAttribute(AttributeKey::PREVIOUS_NODE);
if (! $previousNode instanceof Expr) {
return false;
}
return $this->isFoundInParentNode($variable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ namespace Rector\Naming\Tests\Rector\Variable\UnderscoreToCamelCaseLocalVariable

trait SomeTrait
{
public function run($a_b)
{
$some_value = 5;

$this->run($a_b);
}

public function execute()
{
$some_value = 5;
Expand All @@ -27,13 +20,6 @@ namespace Rector\Naming\Tests\Rector\Variable\UnderscoreToCamelCaseLocalVariable

trait SomeTrait
{
public function run($a_b)
{
$someValue = 5;

$this->run($a_b);
}

public function execute()
{
$someValue = 5;
Expand Down

0 comments on commit 11e8e6e

Please sign in to comment.