Skip to content

Commit

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

* regenerate docs
  • Loading branch information
samsonasik committed Jul 13, 2022
1 parent 743fef0 commit ae7648e
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 2 deletions.
36 changes: 34 additions & 2 deletions build/target-repository/docs/rector_rules_overview.md
@@ -1,4 +1,4 @@
# 412 Rules Overview
# 414 Rules Overview

<br>

Expand All @@ -8,7 +8,7 @@

- [CodeQuality](#codequality) (73)

- [CodingStyle](#codingstyle) (35)
- [CodingStyle](#codingstyle) (37)

- [Compatibility](#compatibility) (1)

Expand Down Expand Up @@ -2339,6 +2339,38 @@ Separate constant and properties to own lines

<br>

### StaticArrowFunctionRector

Changes ArrowFunction to be static when possible

- class: [`Rector\CodingStyle\Rector\ArrowFunction\StaticArrowFunctionRector`](../rules/CodingStyle/Rector/ArrowFunction/StaticArrowFunctionRector.php)

```diff
-fn (): string => 'test';
+static fn (): string => 'test';
```

<br>

### StaticClosureRector

Changes Closure to be static when possible

- class: [`Rector\CodingStyle\Rector\Closure\StaticClosureRector`](../rules/CodingStyle/Rector/Closure/StaticClosureRector.php)

```diff
-function () {
+static function () {
if (rand(0, 1)) {
return 1;
}

return 2;
}
```

<br>

### StrictArraySearchRector

Makes array_search search for identical elements
Expand Down
2 changes: 2 additions & 0 deletions config/set/coding-style.php
Expand Up @@ -14,6 +14,7 @@
use Rector\CodingStyle\Rector\ClassMethod\NewlineBeforeNewAssignSetRector;
use Rector\CodingStyle\Rector\ClassMethod\RemoveDoubleUnderscoreInMethodNameRector;
use Rector\CodingStyle\Rector\ClassMethod\UnSpreadOperatorRector;
use Rector\CodingStyle\Rector\Closure\StaticClosureRector;
use Rector\CodingStyle\Rector\Encapsed\EncapsedStringsToSprintfRector;
use Rector\CodingStyle\Rector\Encapsed\WrapEncapsedVariableInCurlyBracesRector;
use Rector\CodingStyle\Rector\FuncCall\CallUserFuncArrayToVariadicRector;
Expand Down Expand Up @@ -69,5 +70,6 @@
CallUserFuncArrayToVariadicRector::class,
VersionCompareFuncCallToConstantRector::class,
StaticArrowFunctionRector::class,
StaticClosureRector::class,
]);
};
@@ -0,0 +1,25 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture;

function () {
if (rand(0, 1)) {
return 1;
}
return 2;
};

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture;

static function () {
if (rand(0, 1)) {
return 1;
}
return 2;
};

?>
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture;

static function () {
if (rand(0, 1)) {
return 1;
}

return 2;
};
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector\Fixture;

class SkipWithThis
{
private $data = 'data';

public function run()
{
function () {
if (rand(0, 1)) {
return $this->data;
}

return strtoupper($this->data);
};
}
}
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodingStyle\Rector\Closure\StaticClosureRector;

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

final class StaticClosureRectorTest 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';
}
}
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\CodingStyle\Rector\Closure\StaticClosureRector;
use Rector\Config\RectorConfig;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(StaticClosureRector::class);
};
81 changes: 81 additions & 0 deletions rules/CodingStyle/Rector/Closure/StaticClosureRector.php
@@ -0,0 +1,81 @@
<?php

declare(strict_types=1);

namespace Rector\CodingStyle\Rector\Closure;

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
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\Closure\StaticClosureRector\StaticClosureRectorTest
*/
final class StaticClosureRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Changes Closure to be static when possible',
[
new CodeSample(
<<<'CODE_SAMPLE'
function () {
if (rand(0, 1)) {
return 1;
}
return 2;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
static function () {
if (rand(0, 1)) {
return 1;
}
return 2;
}
CODE_SAMPLE
),
]
);
}

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

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

Please sign in to comment.