[PHP 8.0] [Downgrade] Add DowngradeNonCapturingCatchesRector (#4787)

This commit is contained in:
Tomas Votruba 2020-12-05 12:50:31 +01:00 committed by GitHub
parent 1f56646989
commit e627304df9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 198 additions and 0 deletions

View File

@ -3,6 +3,7 @@
declare(strict_types=1);
use Rector\Downgrade\Rector\LNumber\ChangePhpVersionInPlatformCheckRector;
use Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector;
use Rector\DowngradePhp80\Rector\Class_\DowngradePropertyPromotionToConstructorPropertyAssignRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeParamMixedTypeDeclarationRector;
use Rector\DowngradePhp80\Rector\FunctionLike\DowngradeReturnMixedTypeDeclarationRector;
@ -24,4 +25,5 @@ return static function (ContainerConfigurator $containerConfigurator): void {
ChangePhpVersionInPlatformCheckRector::TARGET_PHP_VERSION => 80000,
]]);
$services->set(DowngradePropertyPromotionToConstructorPropertyAssignRector::class);
$services->set(DowngradeNonCapturingCatchesRector::class);
};

View File

@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp80\Rector\Catch_;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Catch_;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://wiki.php.net/rfc/non-capturing_catches
*
* @see \Rector\DowngradePhp80\Tests\Rector\Catch_\DowngradeNonCapturingCatchesRector\DowngradeNonCapturingCatchesRectorTest
*/
final class DowngradeNonCapturingCatchesRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Downgrade catch () without variable to one', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
try {
// code
} catch (\Exception) {
// error
}
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
try {
// code
} catch (\Exception $exception) {
// error
}
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Catch_::class];
}
/**
* @param Catch_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node->var !== null) {
return null;
}
$node->var = new Variable('exception');
return $node;
}
}

View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp80\Tests\Rector\Catch_\DowngradeNonCapturingCatchesRector;
use Iterator;
use Rector\DowngradePhp80\Rector\Catch_\DowngradeNonCapturingCatchesRector;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DowngradeNonCapturingCatchesRectorTest extends AbstractRectorTestCase
{
/**
* @requires PHP 8.0
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
protected function getRectorClass(): string
{
return DowngradeNonCapturingCatchesRector::class;
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace Rector\DowngradePhp80\Tests\Rector\Catch_\DowngradeNonCapturingCatchesRector\Fixture;
class SomeClass
{
public function run()
{
try {
// code
} catch (\Exception) {
// error
}
}
}
?>
-----
<?php
namespace Rector\DowngradePhp80\Tests\Rector\Catch_\DowngradeNonCapturingCatchesRector\Fixture;
class SomeClass
{
public function run()
{
try {
// code
} catch (\Exception $exception) {
// error
}
}
}
?>

View File

@ -0,0 +1,35 @@
<?php
namespace Rector\DowngradePhp80\Tests\Rector\Catch_\DowngradeNonCapturingCatchesRector\Fixture;
class MultiCatch
{
public function run()
{
try {
// code
} catch (\Exception|\RuntimeException) {
// error
}
}
}
?>
-----
<?php
namespace Rector\DowngradePhp80\Tests\Rector\Catch_\DowngradeNonCapturingCatchesRector\Fixture;
class MultiCatch
{
public function run()
{
try {
// code
} catch (\Exception|\RuntimeException $exception) {
// error
}
}
}
?>

View File

@ -0,0 +1,15 @@
<?php
namespace Rector\DowngradePhp80\Tests\Rector\Catch_\DowngradeNonCapturingCatchesRector\Fixture;
class SkipAlready
{
public function run()
{
try {
// code
} catch (\Exception $exception) {
// error
}
}
}