Skip to content

Commit eda7704

Browse files
[Php85] Add ColonAfterSwitchCaseRector (#7157)
* [Php85] Add ColonAfterSwitchCaseRector * register * [ci-review] Rector Rectify * next start * [ci-review] Rector Rectify * next start * Fix * Fix --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent c9f77f7 commit eda7704

File tree

13 files changed

+309
-20
lines changed

13 files changed

+309
-20
lines changed

config/set/php85.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector;
1212
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
1313
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
14+
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
1415
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
1516
use Rector\Removing\ValueObject\RemoveFuncCallArg;
1617
use Rector\Renaming\Rector\Cast\RenameCastRector;
@@ -30,6 +31,7 @@
3031
RemoveFinfoBufferContextArgRector::class,
3132
NullDebugInfoReturnRector::class,
3233
DeprecatedAnnotationToDeprecatedAttributeRector::class,
34+
ColonAfterSwitchCaseRector::class,
3335
]
3436
);
3537

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class ColonAfterSwitchCaseRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\Fixture;
6+
7+
final class Fixture
8+
{
9+
public function run()
10+
{
11+
switch ($value) {
12+
case 'baz';
13+
echo 'baz';
14+
}
15+
}
16+
17+
public function spaceBeforeSemiColon()
18+
{
19+
switch ($value) {
20+
case 'baz' ;
21+
echo 'baz';
22+
}
23+
}
24+
}
25+
26+
?>
27+
-----
28+
<?php
29+
30+
declare(strict_types=1);
31+
32+
namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\Fixture;
33+
34+
final class Fixture
35+
{
36+
public function run()
37+
{
38+
switch ($value) {
39+
case 'baz':
40+
echo 'baz';
41+
}
42+
}
43+
44+
public function spaceBeforeSemiColon()
45+
{
46+
switch ($value) {
47+
case 'baz' :
48+
echo 'baz';
49+
}
50+
}
51+
}
52+
53+
?>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\Fixture;
6+
7+
final class SkipWithColon
8+
{
9+
public function run()
10+
{
11+
switch ($value) {
12+
case 'baz':
13+
echo 'baz';
14+
}
15+
}
16+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\Fixture;
6+
7+
final class WithoutStmt
8+
{
9+
public function run()
10+
{
11+
switch ($value) {
12+
case 'baz';
13+
}
14+
}
15+
16+
public function indirectStmt()
17+
{
18+
switch ($value) {
19+
case 'baz';
20+
case 'bar';
21+
echo 'test';
22+
}
23+
}
24+
}
25+
26+
?>
27+
-----
28+
<?php
29+
30+
declare(strict_types=1);
31+
32+
namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\Fixture;
33+
34+
final class WithoutStmt
35+
{
36+
public function run()
37+
{
38+
switch ($value) {
39+
case 'baz':
40+
}
41+
}
42+
43+
public function indirectStmt()
44+
{
45+
switch ($value) {
46+
case 'baz':
47+
case 'bar':
48+
echo 'test';
49+
}
50+
}
51+
}
52+
53+
?>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->rule(ColonAfterSwitchCaseRector::class);
10+
};

rules-tests/Transform/Rector/FuncCall/WrapFuncCallWithPhpVersionIdCheckerRector/WrapFuncCallWithPhpVersionIdCheckerRectorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector;
46

57
use Iterator;

rules/Php84/Rector/Class_/DeprecatedAnnotationToDeprecatedAttributeRector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface
2222
{
2323
public function __construct(
24-
private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $converter,
24+
private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $deprecatedAnnotationToDeprecatedAttributeConverter,
2525
) {
2626
}
2727

@@ -75,7 +75,7 @@ public function getNodeTypes(): array
7575
*/
7676
public function refactor(Node $node): ?Node
7777
{
78-
return $this->converter->convert($node);
78+
return $this->deprecatedAnnotationToDeprecatedAttributeConverter->convert($node);
7979
}
8080

8181
public function provideMinPhpVersion(): int

rules/Php85/Rector/Const_/DeprecatedAnnotationToDeprecatedAttributeRector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface
2020
{
2121
public function __construct(
22-
private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $converter,
22+
private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $deprecatedAnnotationToDeprecatedAttributeConverter,
2323
) {
2424
}
2525

@@ -52,7 +52,7 @@ public function getNodeTypes(): array
5252
*/
5353
public function refactor(Node $node): ?Node
5454
{
55-
return $this->converter->convert($node);
55+
return $this->deprecatedAnnotationToDeprecatedAttributeConverter->convert($node);
5656
}
5757

5858
public function provideMinPhpVersion(): int
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Php85\Rector\Switch_;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Stmt\Switch_;
10+
use Rector\Rector\AbstractRector;
11+
use Rector\ValueObject\PhpVersionFeature;
12+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
13+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
14+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
15+
16+
/**
17+
* @see \Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\ColonAfterSwitchCaseRectorTest
18+
*/
19+
final class ColonAfterSwitchCaseRector extends AbstractRector implements MinPhpVersionInterface
20+
{
21+
public function getRuleDefinition(): RuleDefinition
22+
{
23+
return new RuleDefinition('Change deprecated semicolon to colon after switch case', [
24+
new CodeSample(
25+
<<<'CODE_SAMPLE'
26+
switch ($value) {
27+
case 'baz';
28+
echo 'baz';
29+
}
30+
CODE_SAMPLE
31+
,
32+
<<<'CODE_SAMPLE'
33+
switch ($value) {
34+
case 'baz':
35+
echo 'baz';
36+
}
37+
CODE_SAMPLE
38+
),
39+
]);
40+
}
41+
42+
public function getNodeTypes(): array
43+
{
44+
return [Switch_::class];
45+
}
46+
47+
/**
48+
* @param Switch_ $node
49+
*/
50+
public function refactor(Node $node): ?Node
51+
{
52+
$hasChanged = false;
53+
$oldTokens = $this->file->getOldTokens();
54+
55+
foreach ($node->cases as $key => $case) {
56+
$cond = $case->cond;
57+
58+
if (! $cond instanceof Expr) {
59+
continue;
60+
}
61+
62+
$endTokenPos = $cond->getEndTokenPos();
63+
64+
if ($endTokenPos < 0) {
65+
continue;
66+
}
67+
68+
$startCaseStmtsPos = count($case->stmts) === 0
69+
? (
70+
isset($node->cases[$key + 1])
71+
? $node->cases[$key + 1]->getStartTokenPos()
72+
: $node->getEndTokenPos()
73+
)
74+
: $case->stmts[0]->getStartTokenPos();
75+
76+
if ($startCaseStmtsPos < 0) {
77+
continue;
78+
}
79+
80+
$nextTokenPos = $endTokenPos;
81+
while (++$nextTokenPos < $startCaseStmtsPos) {
82+
if (! isset($oldTokens[$nextTokenPos])) {
83+
continue 2;
84+
}
85+
86+
$nextToken = $oldTokens[$nextTokenPos];
87+
if (trim($nextToken->text) === '') {
88+
continue;
89+
}
90+
91+
if ($nextToken->text === ':') {
92+
continue 2;
93+
}
94+
95+
if ($nextToken->text === ';') {
96+
$hasChanged = true;
97+
$nextToken->text = ':';
98+
continue 2;
99+
}
100+
}
101+
}
102+
103+
if (! $hasChanged) {
104+
return null;
105+
}
106+
107+
return $node;
108+
}
109+
110+
public function provideMinPhpVersion(): int
111+
{
112+
return PhpVersionFeature::COLON_AFTER_SWITCH_CASE;
113+
}
114+
}

0 commit comments

Comments
 (0)