-
-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodingStyle] Skip has static call call non static method on StaticAr…
…rowFunctionRector and StaticClosureRector (#2713) Co-authored-by: GitHub Action <action@github.com>
- Loading branch information
1 parent
ce22a82
commit f0df3c5
Showing
5 changed files
with
100 additions
and
28 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...owFunction/StaticArrowFunctionRector/Fixture/skip_has_static_call_call_non_static.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,19 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\Fixture; | ||
|
||
/** | ||
* @see https://3v4l.org/rQfZd | ||
*/ | ||
class SkipHasStaticCallCallNonStatic | ||
{ | ||
public function bar() | ||
{ | ||
return 'test'; | ||
} | ||
|
||
public function foobar() | ||
{ | ||
return fn() => self::bar(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...e/Rector/Closure/StaticClosureRector/Fixture/skip_has_static_call_call_non_static.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,19 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture; | ||
|
||
/** | ||
* @see https://3v4l.org/SJSoj | ||
*/ | ||
class SkipHasStaticCallCallNonStatic | ||
{ | ||
public function bar() | ||
{ | ||
return 'test'; | ||
} | ||
|
||
public function foobar() | ||
{ | ||
return function() { return self::bar(); }; | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodingStyle\Guard; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrowFunction; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Reflection\MethodReflection; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\Core\Reflection\ReflectionResolver; | ||
|
||
final class StaticGuard | ||
{ | ||
public function __construct( | ||
private readonly BetterNodeFinder $betterNodeFinder, | ||
private readonly ReflectionResolver $reflectionResolver | ||
) { | ||
} | ||
|
||
public function isLegal(Closure|ArrowFunction $node): bool | ||
{ | ||
if ($node->static) { | ||
return false; | ||
} | ||
|
||
$nodes = $node instanceof Closure | ||
? $node->stmts | ||
: [$node->expr]; | ||
|
||
return ! (bool) $this->betterNodeFinder->findFirst( | ||
$nodes, | ||
function (Node $subNode): bool { | ||
if (! $subNode instanceof StaticCall) { | ||
return $subNode instanceof Variable && $subNode->name === 'this'; | ||
} | ||
|
||
$methodReflection = $this->reflectionResolver->resolveMethodReflectionFromStaticCall($subNode); | ||
if (! $methodReflection instanceof MethodReflection) { | ||
return false; | ||
} | ||
|
||
return ! $methodReflection->isStatic(); | ||
} | ||
); | ||
} | ||
} |
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