Skip to content

Commit

Permalink
[CodeQuality] Skip ArrayKeysAndInArrayToArrayKeyExistsRector on keys …
Browse files Browse the repository at this point in the history
…variable used (#57)
  • Loading branch information
samsonasik committed May 17, 2021
1 parent 367879e commit 2bc6ba8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\Fixture;

class SkipKeysUsed
{
public function run($packageName, $values)
{
$keys = array_keys($values);

foreach($keys as $field) {
if ($field === 'foo') {
return false;
}
}

return in_array($packageName, $keys, true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function refactor(Node $node): ?Node
$arrayVariable = $node->args[1]->value;

/** @var Assign|Node|null $previousAssignArraysKeysFuncCall */
$previousAssignArraysKeysFuncCall = $this->betterNodeFinder->findFirstPrevious($node, function (Node $node) use (
$previousAssignArraysKeysFuncCall = $this->betterNodeFinder->findFirstPreviousOfNode($node, function (Node $node) use (
$arrayVariable
): bool {
// breaking out of scope
Expand All @@ -78,7 +78,9 @@ public function refactor(Node $node): ?Node
}

if (! $node instanceof Assign) {
return false;
return ! (bool) $this->betterNodeFinder->find($node, function (Node $n) use ($arrayVariable) {
return $this->nodeComparator->areNodesEqual($arrayVariable, $n);
});
}

if (! $this->nodeComparator->areNodesEqual($arrayVariable, $node->var)) {
Expand Down

0 comments on commit 2bc6ba8

Please sign in to comment.