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
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"jetbrains/phpstorm-stubs": "^2019.3",
"nette/robot-loader": "^3.2",
"nette/utils": "^3.1",
"nikic/php-parser": "^4.4",
"nikic/php-parser": "^4.5",
"ondram/ci-detector": "^3.4",
"phpstan/phpdoc-parser": "^0.4.7",
"phpstan/phpstan": "^0.12.25",
Expand Down Expand Up @@ -279,7 +279,5 @@
"branch-alias": {
"dev-master": "0.8-dev"
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
}
1 change: 1 addition & 0 deletions config/set/php/php80.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ services:
Rector\Php80\Rector\FuncCall\ClassOnObjectRector: null
Rector\Php80\Rector\Ternary\GetDebugTypeRector: null
Rector\Php80\Rector\FuncCall\TokenGetAllToObjectRector: null
Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector: null
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,17 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
$catchedThrowableVariable = $node->var;
$caughtThrowableVariable = $node->var;
if ($caughtThrowableVariable === null) {
return null;
}

$this->traverseNodesWithCallable($node->stmts, function (Node $node) use ($catchedThrowableVariable): ?int {
$this->traverseNodesWithCallable($node->stmts, function (Node $node) use ($caughtThrowableVariable): ?int {
if (! $node instanceof Throw_) {
return null;
}

return $this->refactorThrow($node, $catchedThrowableVariable);
return $this->refactorThrow($node, $caughtThrowableVariable);
});

return $node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ public function refactor(Node $node): ?Node
$type = $node->types[0];
$typeShortName = $this->getShortName($type);

if ($node->var === null) {
return null;
}

$oldVariableName = $this->getName($node->var);
if (! $oldVariableName) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace Rector\Php80\Rector\Catch_;

use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Catch_;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\RectorDefinition\CodeSample;
use Rector\Core\RectorDefinition\RectorDefinition;

/**
* @see https://wiki.php.net/rfc/non-capturing_catches
*
* @see \Rector\Php80\Tests\Rector\Catch_\RemoveUnusedVariableInCatchRector\RemoveUnusedVariableInCatchRectorTest
*/
final class RemoveUnusedVariableInCatchRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Remove unused variable in catch()', [
new CodeSample(
<<<'PHP'
final class SomeClass
{
public function run()
{
try {
} catch (Throwable $notUsedThrowable) {
}
}
}
PHP
,
<<<'PHP'
final class SomeClass
{
public function run()
{
try {
} catch (Throwable) {
}
}
}
PHP

),
]);
}

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

/**
* @param Catch_ $node
*/
public function refactor(Node $node): ?Node
{
$caughtVar = $node->var;
if ($caughtVar === null) {
return null;
}

if ($this->isVariableUsed((array) $node->stmts, $caughtVar)) {
return null;
}

$node->var = null;

return $node;
}

/**
* @param Node[] $nodes
*/
private function isVariableUsed(array $nodes, Variable $variable): bool
{
return (bool) $this->betterNodeFinder->findFirst($nodes, function (Node $node) use ($variable) {
if (! $node instanceof Variable) {
return false;
}

return $this->areNodesEqual($node, $variable);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Rector\Php80\Tests\Rector\Catch_\RemoveUnusedVariableInCatchRector\Fixture;

final class SomeClass
{
public function run()
{
try {
} catch (Throwable $notUsedThrowable) {
}
}
}

?>
-----
<?php

namespace Rector\Php80\Tests\Rector\Catch_\RemoveUnusedVariableInCatchRector\Fixture;

final class SomeClass
{
public function run()
{
try {
} catch (Throwable) {
}
}
}

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

declare(strict_types=1);

namespace Rector\Php80\Tests\Rector\Catch_\RemoveUnusedVariableInCatchRector;

use Iterator;
use Rector\Core\Testing\PHPUnit\AbstractRectorTestCase;
use Rector\Php80\Rector\Catch_\RemoveUnusedVariableInCatchRector;

final class RemoveUnusedVariableInCatchRectorTest 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 RemoveUnusedVariableInCatchRector::class;
}
}
3 changes: 3 additions & 0 deletions rules/phpunit/src/Rector/TryCatchToExpectExceptionRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ private function processTryCatch(TryCatch $tryCatch): ?array
$this->newExpressions = [];

$exceptionVariable = $tryCatch->catches[0]->var;
if ($exceptionVariable === null) {
return null;
}

// we look for:
// - instance of $exceptionVariableName
Expand Down