mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-07 03:40:50 +00:00
[DeadCode] Handle empty try and catch on RemoveDeadTryCatchRector (#1860)
Co-authored-by: GitHub Action <action@github.com>
This commit is contained in:
parent
44b84f44cb
commit
3e33dd1aa7
|
@ -25,7 +25,6 @@ class DropWithEmptyFinally
|
|||
{
|
||||
public function run()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;
|
||||
|
||||
class EmptyTryAndCatch
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
try {
|
||||
// some code
|
||||
}
|
||||
catch (Throwable $throwable) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;
|
||||
|
||||
class EmptyTryAndCatch
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -25,7 +25,6 @@ class Fixture
|
|||
{
|
||||
public function run()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;
|
||||
|
||||
class SkipMultiLinesDifferentThrowVariable
|
||||
{
|
||||
public function run($variable)
|
||||
{
|
||||
try {
|
||||
$this->doSomething();
|
||||
$this->doSomethingElse();
|
||||
}
|
||||
catch (Throwable $throwable) {
|
||||
throw $variable;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Rector\Tests\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;
|
||||
|
||||
class SkipHasStmtEmptyCatchThrow
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
try {
|
||||
$this->call();
|
||||
}
|
||||
catch (Throwable $throwable) {
|
||||
}
|
||||
}
|
||||
}
|
|
@ -6,7 +6,8 @@ namespace Rector\DeadCode\Rector\TryCatch;
|
|||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Stmt;
|
||||
use PhpParser\Node\Stmt\Catch_;
|
||||
use PhpParser\Node\Stmt\Finally_;
|
||||
use PhpParser\Node\Stmt\Nop;
|
||||
use PhpParser\Node\Stmt\Throw_;
|
||||
use PhpParser\Node\Stmt\TryCatch;
|
||||
use Rector\Core\Rector\AbstractRector;
|
||||
|
@ -42,7 +43,6 @@ class SomeClass
|
|||
{
|
||||
public function run()
|
||||
{
|
||||
// some code
|
||||
}
|
||||
}
|
||||
CODE_SAMPLE
|
||||
|
@ -60,21 +60,28 @@ CODE_SAMPLE
|
|||
|
||||
/**
|
||||
* @param TryCatch $node
|
||||
* @return Stmt[]|null
|
||||
* @return Stmt[]|null|TryCatch
|
||||
*/
|
||||
public function refactor(Node $node): ?array
|
||||
public function refactor(Node $node): array|null|TryCatch
|
||||
{
|
||||
$isEmptyFinallyStmts = ! $node->finally instanceof Finally_ || $this->isEmpty($node->finally->stmts);
|
||||
|
||||
// not empty stmts on finally always executed
|
||||
if (! $isEmptyFinallyStmts) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->isEmpty($node->stmts)) {
|
||||
$this->removeNode($node);
|
||||
return $node;
|
||||
}
|
||||
|
||||
if (count($node->catches) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var Catch_ $onlyCatch */
|
||||
$onlyCatch = $node->catches[0];
|
||||
if (count($onlyCatch->stmts) !== 1) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($node->finally !== null && $node->finally->stmts !== []) {
|
||||
if ($this->isEmpty($onlyCatch->stmts)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -89,4 +96,20 @@ CODE_SAMPLE
|
|||
|
||||
return $node->stmts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Stmt[] $stmts
|
||||
*/
|
||||
private function isEmpty(array $stmts): bool
|
||||
{
|
||||
if ($stmts === []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (count($stmts) > 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $stmts[0] instanceof Nop;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -138,15 +138,11 @@ return [
|
|||
|
||||
// fixes https://github.com/rectorphp/rector/issues/7017
|
||||
function (string $filePath, string $prefix, string $content): string {
|
||||
if (!str_ends_with($filePath, 'vendor/symfony/string/ByteString.php')) {
|
||||
if (! str_ends_with($filePath, 'vendor/symfony/string/ByteString.php')) {
|
||||
return $content;
|
||||
}
|
||||
|
||||
return Strings::replace(
|
||||
$content,
|
||||
'#' . $prefix . '\\\\\\\\1_\\\\\\\\2#',
|
||||
'\\\\1_\\\\2'
|
||||
);
|
||||
return Strings::replace($content, '#' . $prefix . '\\\\\\\\1_\\\\\\\\2#', '\\\\1_\\\\2');
|
||||
},
|
||||
|
||||
// unprefixed ContainerConfigurator
|
||||
|
|
Loading…
Reference in New Issue
Block a user