Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/set/php85.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Rector\Php85\Rector\ClassMethod\NullDebugInfoReturnRector;
use Rector\Php85\Rector\Const_\DeprecatedAnnotationToDeprecatedAttributeRector;
use Rector\Php85\Rector\FuncCall\RemoveFinfoBufferContextArgRector;
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;
use Rector\Removing\Rector\FuncCall\RemoveFuncCallArgRector;
use Rector\Removing\ValueObject\RemoveFuncCallArg;
use Rector\Renaming\Rector\Cast\RenameCastRector;
Expand All @@ -30,6 +31,7 @@
RemoveFinfoBufferContextArgRector::class,
NullDebugInfoReturnRector::class,
DeprecatedAnnotationToDeprecatedAttributeRector::class,
ColonAfterSwitchCaseRector::class,
]
);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ColonAfterSwitchCaseRectorTest 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';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\Fixture;

final class Fixture
{
public function run()
{
switch ($value) {
case 'baz';
echo 'baz';
}
}

public function spaceBeforeSemiColon()
{
switch ($value) {
case 'baz' ;
echo 'baz';
}
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\Fixture;

final class Fixture
{
public function run()
{
switch ($value) {
case 'baz':
echo 'baz';
}
}

public function spaceBeforeSemiColon()
{
switch ($value) {
case 'baz' :
echo 'baz';
}
}
}

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

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\Fixture;

final class SkipWithColon
{
public function run()
{
switch ($value) {
case 'baz':
echo 'baz';
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\Fixture;

final class WithoutStmt
{
public function run()
{
switch ($value) {
case 'baz';
}
}

public function indirectStmt()
{
switch ($value) {
case 'baz';
case 'bar';
echo 'test';
}
}
}

?>
-----
<?php

declare(strict_types=1);

namespace Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\Fixture;

final class WithoutStmt
{
public function run()
{
switch ($value) {
case 'baz':
}
}

public function indirectStmt()
{
switch ($value) {
case 'baz':
case 'bar':
echo 'test';
}
}
}

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

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php85\Rector\Switch_\ColonAfterSwitchCaseRector;

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

declare(strict_types=1);

namespace Rector\Tests\Transform\Rector\FuncCall\WrapFuncCallWithPhpVersionIdCheckerRector;

use Iterator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $converter,
private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $deprecatedAnnotationToDeprecatedAttributeConverter,
) {
}

Expand Down Expand Up @@ -75,7 +75,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
return $this->converter->convert($node);
return $this->deprecatedAnnotationToDeprecatedAttributeConverter->convert($node);
}

public function provideMinPhpVersion(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
final class DeprecatedAnnotationToDeprecatedAttributeRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $converter,
private readonly DeprecatedAnnotationToDeprecatedAttributeConverter $deprecatedAnnotationToDeprecatedAttributeConverter,
) {
}

Expand Down Expand Up @@ -52,7 +52,7 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
return $this->converter->convert($node);
return $this->deprecatedAnnotationToDeprecatedAttributeConverter->convert($node);
}

public function provideMinPhpVersion(): int
Expand Down
114 changes: 114 additions & 0 deletions rules/Php85/Rector/Switch_/ColonAfterSwitchCaseRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

declare(strict_types=1);

namespace Rector\Php85\Rector\Switch_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Stmt\Switch_;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\Php85\Rector\Switch_\ColonAfterSwitchCaseRector\ColonAfterSwitchCaseRectorTest
*/
final class ColonAfterSwitchCaseRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change deprecated semicolon to colon after switch case', [
new CodeSample(
<<<'CODE_SAMPLE'
switch ($value) {
case 'baz';
echo 'baz';
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
switch ($value) {
case 'baz':
echo 'baz';
}
CODE_SAMPLE
),
]);
}

public function getNodeTypes(): array
{
return [Switch_::class];
}

/**
* @param Switch_ $node
*/
public function refactor(Node $node): ?Node
{
$hasChanged = false;
$oldTokens = $this->file->getOldTokens();

foreach ($node->cases as $key => $case) {
$cond = $case->cond;

if (! $cond instanceof Expr) {
continue;
}

$endTokenPos = $cond->getEndTokenPos();

if ($endTokenPos < 0) {
continue;
}

$startCaseStmtsPos = count($case->stmts) === 0
? (
isset($node->cases[$key + 1])
? $node->cases[$key + 1]->getStartTokenPos()
: $node->getEndTokenPos()
)
: $case->stmts[0]->getStartTokenPos();

if ($startCaseStmtsPos < 0) {
continue;
}

$nextTokenPos = $endTokenPos;
while (++$nextTokenPos < $startCaseStmtsPos) {
if (! isset($oldTokens[$nextTokenPos])) {
continue 2;
}

$nextToken = $oldTokens[$nextTokenPos];
if (trim($nextToken->text) === '') {
continue;
}

if ($nextToken->text === ':') {
continue 2;
}

if ($nextToken->text === ';') {
$hasChanged = true;
$nextToken->text = ':';
continue 2;
}
}
}

if (! $hasChanged) {
return null;
}

return $node;
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::COLON_AFTER_SWITCH_CASE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ public function refactor(Node $node): null|Node|int

$hasChanged = false;
foreach ($node->stmts as $key => $stmt) {
if (! $stmt instanceof Expression || ! $stmt->expr instanceof FuncCall) {
if (! $stmt instanceof Expression) {
continue;
}

if (! $stmt->expr instanceof FuncCall) {
continue;
}

Expand Down Expand Up @@ -121,32 +125,33 @@ public function configure(array $configuration): void
$this->wrapFuncCallWithPhpVersionIdCheckers = $configuration;
}

private function isWrappedFuncCall(StmtsAwareInterface $node): bool
private function isWrappedFuncCall(StmtsAwareInterface $stmtsAware): bool
{
if (! $node instanceof If_) {
if (! $stmtsAware instanceof If_) {
return false;
}

$phpVersionId = $this->getPhpVersionId($node->cond);
if ($phpVersionId === null) {
$phpVersionId = $this->getPhpVersionId($stmtsAware->cond);
if (!$phpVersionId instanceof Int_) {
return false;
}

if (count($node->stmts) !== 1) {
if (count($stmtsAware->stmts) !== 1) {
return false;
}

$childStmt = $node->stmts[0];
$childStmt = $stmtsAware->stmts[0];

if (! $childStmt instanceof Expression || ! $childStmt->expr instanceof FuncCall) {
return false;
}

foreach ($this->wrapFuncCallWithPhpVersionIdCheckers as $wrapFuncCallWithPhpVersionIdChecker) {
if (
$this->getName($childStmt->expr) !== $wrapFuncCallWithPhpVersionIdChecker->getFunctionName()
|| $phpVersionId->value !== $wrapFuncCallWithPhpVersionIdChecker->getPhpVersionId()
) {
if ($this->getName($childStmt->expr) !== $wrapFuncCallWithPhpVersionIdChecker->getFunctionName()) {
continue;
}

if ($phpVersionId->value !== $wrapFuncCallWithPhpVersionIdChecker->getPhpVersionId()) {
continue;
}

Expand Down
Loading
Loading