Skip to content

Commit

Permalink
[CodingStyle] Add StaticArrowFunctionRector (#2657)
Browse files Browse the repository at this point in the history
* [CodingStyle] Add StaticArrowFunctionRector

* final touch: clean up
  • Loading branch information
samsonasik committed Jul 13, 2022
1 parent b1ae9a1 commit 743fef0
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 0 deletions.
2 changes: 2 additions & 0 deletions config/set/coding-style.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
use Rector\CodingStyle\Rector\Assign\PHPStormVarAnnotationRector;
use Rector\CodingStyle\Rector\Assign\SplitDoubleAssignRector;
use Rector\CodingStyle\Rector\Catch_\CatchExceptionNameMatchingTypeRector;
Expand Down Expand Up @@ -67,5 +68,6 @@
MakeInheritedMethodVisibilitySameAsParentRector::class,
CallUserFuncArrayToVariadicRector::class,
VersionCompareFuncCallToConstantRector::class,
StaticArrowFunctionRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\Fixture;

fn(): string => 'test';

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\Fixture;

static fn(): string => 'test';

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\Fixture;

static fn(): string => 'test';
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\Fixture;

class SkipWithThis
{
private $data = 'data';

public function run()
{
fn(): string => $this->data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class StaticArrowFunctionRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(StaticArrowFunctionRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

namespace Rector\CodingStyle\Rector\ArrowFunction;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Variable;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector\StaticArrowFunctionRectorTest
*/
final class StaticArrowFunctionRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes ArrowFunction to be static when possible',
[
new CodeSample(
<<<'CODE_SAMPLE'
fn (): string => 'test';
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
static fn (): string => 'test';
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ArrowFunction::class];
}

/**
* @param ArrowFunction $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($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'
);
}
}

0 comments on commit 743fef0

Please sign in to comment.