-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[CodeQuality] Add CompleteMissingIfElseBracketRector
- Loading branch information
1 parent
6aed089
commit 2304d78
Showing
7 changed files
with
224 additions
and
17 deletions.
There are no files selected for viewing
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
28 changes: 28 additions & 0 deletions
28
.../Rector/If_/CompleteMissingIfElseBracketRector/CompleteMissingIfElseBracketRectorTest.php
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class CompleteMissingIfElseBracketRectorTest extends AbstractRectorTestCase | ||
{ | ||
#[DataProvider('provideData')] | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
public static function provideData(): Iterator | ||
{ | ||
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...ests/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector/Fixture/some_class.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,30 @@ | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) | ||
return 1; | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Tests\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector\Fixture; | ||
|
||
class SomeClass | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) { | ||
return 1; | ||
} | ||
} | ||
} | ||
|
||
?> |
10 changes: 10 additions & 0 deletions
10
...ests/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector/config/configured_rule.php
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Rector\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector; | ||
use Rector\Config\RectorConfig; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->rule(CompleteMissingIfElseBracketRector::class); | ||
}; |
116 changes: 116 additions & 0 deletions
116
rules/CodeQuality/Rector/If_/CompleteMissingIfElseBracketRector.php
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,116 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\CodeQuality\Rector\If_; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Stmt\If_; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
||
/** | ||
* @see \Rector\Tests\CodeQuality\Rector\If_\CompleteMissingIfElseBracketRector\CompleteMissingIfElseBracketRectorTest | ||
*/ | ||
final class CompleteMissingIfElseBracketRector extends AbstractRector | ||
{ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Complete missing if/else brackets', [ | ||
new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) | ||
return 1; | ||
} | ||
} | ||
CODE_SAMPLE | ||
|
||
, | ||
<<<'CODE_SAMPLE' | ||
class SomeClass | ||
{ | ||
public function run($value) | ||
{ | ||
if ($value) { | ||
return 1; | ||
} | ||
} | ||
} | ||
CODE_SAMPLE | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [If_::class]; | ||
} | ||
|
||
/** | ||
* @param If_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->isBareNewNode($node)) { | ||
return null; | ||
} | ||
|
||
$oldTokens = $this->file->getOldTokens(); | ||
if ($this->isIfConditionFollowedByOpeningCurlyBracket($node, $oldTokens)) { | ||
return null; | ||
} | ||
|
||
// invoke reprint with brackets | ||
$node->setAttribute(AttributeKey::ORIGINAL_NODE, null); | ||
|
||
return $node; | ||
} | ||
|
||
/** | ||
* @param mixed[] $oldTokens | ||
*/ | ||
private function isIfConditionFollowedByOpeningCurlyBracket(If_ $if, array $oldTokens): bool | ||
{ | ||
for ($i = $if->getStartTokenPos(); $i < $if->getEndTokenPos(); ++$i) { | ||
if ($oldTokens[$i] !== ')') { | ||
continue; | ||
} | ||
|
||
// first closing bracket must be followed by curly opening brackets | ||
// what is next token? | ||
$nextToken = $oldTokens[$i + 1]; | ||
|
||
if (is_array($nextToken) && trim((string) $nextToken[1]) === '') { | ||
// next token is whitespace | ||
$nextToken = $oldTokens[$i + 2]; | ||
} | ||
|
||
if ($nextToken === '{') { | ||
// all good | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
private function isBareNewNode(If_ $if): bool | ||
{ | ||
$originalNode = $if->getAttribute(AttributeKey::ORIGINAL_NODE); | ||
if (! $originalNode instanceof Node) { | ||
return true; | ||
} | ||
|
||
// not defined, probably new if | ||
return $if->getStartTokenPos() === -1; | ||
} | ||
} |
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