-
-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b31c654
commit 8cc1277
Showing
5 changed files
with
163 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...Rector/Array_/CallableThisArrayToAnonymousFunctionRector/Fixture/default_of_array.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector\Fixture; | ||
|
||
final class DefaultOfArray | ||
{ | ||
public function run($values) | ||
{ | ||
usort($values, [$this, 'sortMe']); | ||
} | ||
|
||
public function sortMe($values = []) | ||
{ | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector\Fixture; | ||
|
||
final class DefaultOfArray | ||
{ | ||
public function run($values) | ||
{ | ||
usort($values, function ($values = []) { | ||
return $this->sortMe($values); | ||
}); | ||
} | ||
|
||
public function sortMe($values = []) | ||
{ | ||
} | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
rules/Php72/NodeManipulator/ClosureNestedUsesDecorator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Php72\NodeManipulator; | ||
|
||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\Expr\ClosureUse; | ||
use PhpParser\Node\Expr\Variable; | ||
use Rector\Core\PhpParser\Comparing\NodeComparator; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
|
||
final class ClosureNestedUsesDecorator | ||
{ | ||
public function __construct( | ||
private readonly BetterNodeFinder $betterNodeFinder, | ||
private readonly NodeNameResolver $nodeNameResolver, | ||
private readonly NodeComparator $nodeComparator, | ||
) { | ||
} | ||
|
||
public function applyNestedUses(Closure $anonymousFunctionNode, Variable $useVariable): Closure | ||
{ | ||
$parent = $this->betterNodeFinder->findParentType($useVariable, Closure::class); | ||
|
||
if (! $parent instanceof Closure) { | ||
return $anonymousFunctionNode; | ||
} | ||
|
||
$paramNames = $this->nodeNameResolver->getNames($parent->params); | ||
|
||
if ($this->nodeNameResolver->isNames($useVariable, $paramNames)) { | ||
return $anonymousFunctionNode; | ||
} | ||
|
||
$anonymousFunctionNode = clone $anonymousFunctionNode; | ||
while ($parent instanceof Closure) { | ||
$parentOfParent = $this->betterNodeFinder->findParentType($parent, Closure::class); | ||
|
||
$uses = []; | ||
while ($parentOfParent instanceof Closure) { | ||
$uses = $this->collectUsesEqual($parentOfParent, $uses, $useVariable); | ||
$parentOfParent = $this->betterNodeFinder->findParentType($parentOfParent, Closure::class); | ||
} | ||
|
||
$uses = array_merge($parent->uses, $uses); | ||
$uses = $this->cleanClosureUses($uses); | ||
$parent->uses = $uses; | ||
|
||
$parent = $this->betterNodeFinder->findParentType($parent, Closure::class); | ||
} | ||
|
||
return $anonymousFunctionNode; | ||
} | ||
|
||
/** | ||
* @param ClosureUse[] $uses | ||
* @return ClosureUse[] | ||
*/ | ||
private function collectUsesEqual(Closure $closure, array $uses, Variable $useVariable): array | ||
{ | ||
foreach ($closure->params as $param) { | ||
if ($this->nodeComparator->areNodesEqual($param->var, $useVariable)) { | ||
$uses[] = new ClosureUse($param->var); | ||
} | ||
} | ||
|
||
return $uses; | ||
} | ||
|
||
/** | ||
* @param ClosureUse[] $uses | ||
* @return ClosureUse[] | ||
*/ | ||
private function cleanClosureUses(array $uses): array | ||
{ | ||
$uniqueUses = []; | ||
foreach ($uses as $use) { | ||
if (! is_string($use->var->name)) { | ||
continue; | ||
} | ||
|
||
$variableName = $use->var->name; | ||
if (array_key_exists($variableName, $uniqueUses)) { | ||
continue; | ||
} | ||
|
||
$uniqueUses[$variableName] = $use; | ||
} | ||
|
||
return array_values($uniqueUses); | ||
} | ||
} |