mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-29 07:20:52 +00:00
[PHP 8.0] [Downgrade] Add DowngradeNonCapturingCatchesRector (#4787)
This commit is contained in:
parent
1f56646989
commit
e627304df9
|
@ -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);
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\DowngradePhp80\Tests\Rector\Catch_\DowngradeNonCapturingCatchesRector\Fixture;
|
||||
|
||||
class SkipAlready
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
try {
|
||||
// code
|
||||
} catch (\Exception $exception) {
|
||||
// error
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user