Skip to content

Commit

Permalink
[CodingStyle] Skip has static call call non static method on StaticAr…
Browse files Browse the repository at this point in the history
…rowFunctionRector and StaticClosureRector (#2713)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jul 26, 2022
1 parent ce22a82 commit f0df3c5
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 28 deletions.
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();
}
}
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(); };
}
}
50 changes: 50 additions & 0 deletions rules/CodingStyle/Guard/StaticGuard.php
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();
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Variable;
use Rector\CodingStyle\Guard\StaticGuard;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -16,6 +16,10 @@
*/
final class StaticArrowFunctionRector extends AbstractRector
{
public function __construct(private readonly StaticGuard $staticGuard)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -47,23 +51,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
if (! $this->staticGuard->isLegal($node)) {
return null;
}

$node->static = true;
return $node;
}

private function shouldSkip(ArrowFunction $arrowFunction): bool
{
if ($arrowFunction->static) {
return true;
}

return (bool) $this->betterNodeFinder->findFirst(
$arrowFunction->expr,
static fn (Node $subNode): bool => $subNode instanceof Variable && $subNode->name === 'this'
);
}
}
20 changes: 6 additions & 14 deletions rules/CodingStyle/Rector/Closure/StaticClosureRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use Rector\CodingStyle\Guard\StaticGuard;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -16,6 +16,10 @@
*/
final class StaticClosureRector extends AbstractRector
{
public function __construct(private readonly StaticGuard $staticGuard)
{
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
Expand Down Expand Up @@ -59,23 +63,11 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
if (! $this->staticGuard->isLegal($node)) {
return null;
}

$node->static = true;
return $node;
}

private function shouldSkip(Closure $closure): bool
{
if ($closure->static) {
return true;
}

return (bool) $this->betterNodeFinder->findFirst(
$closure->stmts,
static fn (Node $subNode): bool => $subNode instanceof Variable && $subNode->name === 'this'
);
}
}

0 comments on commit f0df3c5

Please sign in to comment.