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
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Catch_\ThrowWithPreviousExceptionRector\Fixture;

use CustomException;
use Throwable;

class SkipCustomExceptionNotAutoloaded
{
public function run()
{
try {

} catch (Throwable $exception) {
if (true) {
throw new CustomException;
}
handleException($exception);
}
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,16 @@ public function refactor(Node $node): ?Node
return null;
}

$isChanged = $this->refactorThrow($node, $caughtThrowableVariable);
return $isChanged;
$result = $this->refactorThrow($node, $caughtThrowableVariable);
if ($result === null) {
return null;
}

$isChanged = true;
return $result;
});

if (! (bool) $isChanged) {
if (! $isChanged) {
return null;
}

Expand Down Expand Up @@ -142,6 +147,12 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
$messageArgument = $new->args[0] ?? null;
$shouldUseNamedArguments = $messageArgument instanceof Arg && $messageArgument->name instanceof Identifier;

$hasCodeParameter = $this->hasParameter($new, 'code');
$hasCodeArgument = $this->hasArgument($new, 'code');
if (! isset($new->getArgs()[1]) && (! $hasCodeParameter || $hasCodeArgument)) {
return null;
}

$hasChanged = false;
if (! isset($new->args[0])) {
// get previous message
Expand All @@ -158,7 +169,7 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
}

if (! isset($new->getArgs()[1])) {
if ($this->hasParameter($new, 'code') && ! $this->hasArgument($new, 'code')) {
if ($hasCodeParameter && ! $hasCodeArgument) {
// get previous code
$new->args[1] = new Arg(
new MethodCall($caughtThrowableVariable, 'getCode'),
Expand All @@ -173,7 +184,7 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
/** @var Arg $arg1 */
$arg1 = $new->args[1];
if ($arg1->name instanceof Identifier && $arg1->name->toString() === 'previous') {
if ($this->hasParameter($new, 'code') && ! $this->hasArgument($new, 'code')) {
if ($hasCodeParameter && ! $hasCodeArgument) {
$new->args[1] = new Arg(
new MethodCall($caughtThrowableVariable, 'getCode'),
name: $shouldUseNamedArguments ? new Identifier('code') : null
Expand Down
Loading