Skip to content
Closed
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,35 @@
<?php

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

class NamedArgument
{
public function run()
{
try {
$this->run();
}catch(\Throwable $throwable) {
throw new \LogicException('Some exception', previous: $throwable);
}
}
}

?>
-----
<?php

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

class NamedArgument
{
public function run()
{
try {
$this->run();
}catch(\Throwable $throwable) {
throw new \LogicException('Some exception', $throwable->getCode(), previous: $throwable);
}
}
}

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

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

use LogicException;

class NamedArgumentForMessageAndPrevious
{
public function run()
{
try {
$this->run();
}catch(\Throwable $throwable) {
throw new LogicException(message: 'Some exception', previous: $throwable);
}
}
}

?>
-----
<?php

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

use LogicException;

class NamedArgumentForMessageAndPrevious
{
public function run()
{
try {
$this->run();
}catch(\Throwable $throwable) {
throw new LogicException(message: 'Some exception', code: $throwable->getCode(), previous: $throwable);
}
}
}

?>

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,14 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
/** @var Arg $arg1 */
$arg1 = $new->args[1];
if ($arg1->name instanceof Identifier && $arg1->name->toString() === 'previous') {
// previous is already passed, code is omitted on purpose
if (! $hasChanged) {
if ($hasCodeParameter && ! $hasCodeArgument) {
$new->args[1] = new Arg(
new MethodCall($caughtThrowableVariable, 'getCode'),
name: $shouldUseNamedArguments ? new Identifier('code') : null
);
$new->args[$exceptionArgumentPosition] = $arg1;
$hasChanged = true;
} elseif (! $hasChanged) {
return null;
}
} elseif ($this->hasParameter($new, 'previous') && ! $this->hasArgument($new, 'previous')) {
Expand Down
Loading