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/dead-code/dead-code.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ services:
Rector\DeadCode\Rector\If_\SimplifyIfElseWithSameContentRector: null
Rector\DeadCode\Rector\Ternary\TernaryToBooleanOrFalseToBooleanAndRector: null
Rector\PHPUnit\Rector\ClassMethod\RemoveEmptyTestMethodRector: null
Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector: null

26 changes: 25 additions & 1 deletion docs/AllRectorsOverview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# All 449 Rectors Overview
# All 450 Rectors Overview

- [Projects](#projects)
- [General](#general)
Expand Down Expand Up @@ -2464,6 +2464,30 @@ Removes dead code statements

<br>

### `RemoveDeadTryCatchRector`

- class: `Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector`

Remove dead try/catch

```diff
class SomeClass
{
public function run()
{
- try {
- // some code
- }
- catch (Throwable $throwable) {
- throw $throwable;
- }
+ // some code
}
}
```

<br>

### `RemoveDeadZeroAndOneOperationRector`

- class: `Rector\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ public function createFromRectorRecipe(array $rectorRecipe): Configuration
$rectorRecipe['description'],
$this->normalizeCode($rectorRecipe['code_before']),
$this->normalizeCode($rectorRecipe['code_after']),
$this->normalizeCode($rectorRecipe['extra_file_content'] ?? ''),
$rectorRecipe['extra_file_name'],
isset($rectorRecipe['extra_file_content']) ? $this->normalizeCode(
$rectorRecipe['extra_file_content']
) : null,
$rectorRecipe['extra_file_name'] ?? null,
array_filter((array) $rectorRecipe['source']),
$this->configResolver->resolveSetConfig($rectorRecipe['set']),
$this->detectPhpSnippet($rectorRecipe['code_before'])
Expand Down
3 changes: 2 additions & 1 deletion packages/rector-generator/src/TemplateVariablesFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;
use Rector\Core\PhpParser\Printer\BetterStandardPrinter;
use Rector\Core\Util\RectorStrings;
use Rector\RectorGenerator\ValueObject\Configuration;

final class TemplateVariablesFactory
Expand All @@ -31,7 +32,7 @@ public function createFromConfiguration(Configuration $configuration): array
{
$data = [
'_Package_' => $configuration->getPackage(),
'_package_' => strtolower($configuration->getPackage()),
'_package_' => RectorStrings::camelCaseToDashes($configuration->getPackage()),
'_Category_' => $configuration->getCategory(),
'_Description_' => $configuration->getDescription(),
'_Name_' => $configuration->getName(),
Expand Down
93 changes: 93 additions & 0 deletions rules/dead-code/src/Rector/TryCatch/RemoveDeadTryCatchRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\Rector\TryCatch;

use PhpParser\Node;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Throw_;
use PhpParser\Node\Stmt\TryCatch;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;

/**
* @see \Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector\RemoveDeadTryCatchRectorTest
*/
final class RemoveDeadTryCatchRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Remove dead try/catch', [
new CodeSample(
<<<'PHP'
class SomeClass
{
public function run()
{
try {
// some code
}
catch (Throwable $throwable) {
throw $throwable;
}
}
}
PHP
,
<<<'PHP'
class SomeClass
{
public function run()
{
// some code
}
}
PHP

),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [TryCatch::class];
}

/**
* @param TryCatch $node
*/
public function refactor(Node $node): ?Node
{
if (count($node->catches) !== 1) {
return null;
}

/** @var Catch_ $onlyCatch */
$onlyCatch = $node->catches[0];
if (count($onlyCatch->stmts) !== 1) {
return null;
}

$onlyCatchStmt = $onlyCatch->stmts[0];
if (! $onlyCatchStmt instanceof Throw_) {
return null;
}

if (! $this->areNamesEqual($onlyCatch->var, $onlyCatchStmt->expr)) {
return null;
}

foreach ($node->stmts as $tryStmt) {
$this->addNodeAfterNode($tryStmt, $node);
}

$this->removeNode($node);

return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;

class SomeClass
{
public function run()
{
try {
// some code
}
catch (Throwable $throwable) {
throw $throwable;
}
}
}

?>
-----
<?php

namespace Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;

class SomeClass
{
public function run()
{
// some code

}
}

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

namespace Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;

class MultiLines
{
public function run()
{
try {
$one = 1;
$two = 1;
if ($one + $two > 1) {
return 155;
}
}
catch (Throwable $throwable) {
throw $throwable;
}
}
}

?>
-----
<?php

namespace Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;

class MultiLines
{
public function run()
{
$one = 1;
$two = 1;
if ($one + $two > 1) {
return 155;
}
}
}

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

namespace Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector\Fixture;

use InvalidArgumentException;

class SkipCatchElse
{
public function run()
{
try {
$one = 1;
}
catch (Throwable $throwable) {
$throwable = new InvalidArgumentException();
throw $throwable;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Rector\DeadCode\Tests\Rector\TryCatch\RemoveDeadTryCatchRector;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\DeadCode\Rector\TryCatch\RemoveDeadTryCatchRector;

final class RemoveDeadTryCatchRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $file): void
{
$this->doTestFile($file);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

protected function getRectorClass(): string
{
return RemoveDeadTryCatchRector::class;
}
}