Skip to content

Commit

Permalink
[CodeQuality] Do not add \ on static on CallableThisArrayToAnonymousF…
Browse files Browse the repository at this point in the history
…unctionRector (#1671)

* Add failing test fixture for CallableThisArrayToAnonymousFunctionRector

# Failing Test for CallableThisArrayToAnonymousFunctionRector

Based on https://getrector.org/demo/1ec74754-9153-677e-92f6-f53ce8cf82e4

rectorphp/rector#6935

* rename fixture

* Closes #1668

* phpstan

Co-authored-by: Alexander Schranz <alexander@sulu.io>
  • Loading branch information
samsonasik and alexander-schranz committed Jan 14, 2022
1 parent a84d951 commit 6ea0032
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector\Fixture;

final class DoNotAddBackslashStatic
{
public function run(callable $transform)
{
$transform();
}

public function run2()
{
$this->run([static::class, 'test']);
}

public static function test(): void
{
echo 'test';
}
}
?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Array_\CallableThisArrayToAnonymousFunctionRector\Fixture;

final class DoNotAddBackslashStatic
{
public function run(callable $transform)
{
$transform();
}

public function run2()
{
$this->run(function () : void {
static::test();
});
}

public static function test(): void
{
echo 'test';
}
}
?>
19 changes: 13 additions & 6 deletions rules/Php72/NodeFactory/AnonymousFunctionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,12 +201,14 @@ private function applyNestedUses(Closure $anonymousFunctionNode, Variable $useVa
{
$parent = $this->betterNodeFinder->findParentType($useVariable, Closure::class);

if ($parent instanceof Closure) {
$paramNames = $this->nodeNameResolver->getNames($parent->params);
if (! $parent instanceof Closure) {
return $anonymousFunctionNode;
}

if ($this->nodeNameResolver->isNames($useVariable, $paramNames)) {
return $anonymousFunctionNode;
}
$paramNames = $this->nodeNameResolver->getNames($parent->params);

if ($this->nodeNameResolver->isNames($useVariable, $paramNames)) {
return $anonymousFunctionNode;
}

$anonymousFunctionNode = clone $anonymousFunctionNode;
Expand Down Expand Up @@ -347,7 +349,7 @@ private function createInnerMethodCall(
return $innerMethodCall;
}

private function normalizeClassConstFetchForStatic(Expr $expr): null | FullyQualified | Expr
private function normalizeClassConstFetchForStatic(Expr $expr): null | Name | FullyQualified | Expr
{
if (! $expr instanceof ClassConstFetch) {
return $expr;
Expand All @@ -363,6 +365,11 @@ private function normalizeClassConstFetchForStatic(Expr $expr): null | FullyQual
return null;
}

$name = new Name($className);
if ($name->isSpecialClassName()) {
return $name;
}

return new FullyQualified($className);
}

Expand Down

0 comments on commit 6ea0032

Please sign in to comment.