Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CodingStyle] Add StaticClosureRector #2658

Merged
merged 2 commits into from Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'
);
}
}