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/named-args.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Rector\CodeQuality\Rector\CallLike\AddNameToBooleanArgumentRector;
use Rector\CodeQuality\Rector\CallLike\AddNameToNullArgumentRector;
use Rector\CodeQuality\Rector\FuncCall\SortCallLikeNamedArgsRector;
use Rector\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\MethodCall\RemoveNullNamedArgOnNullDefaultParamRector;

Expand All @@ -18,5 +19,6 @@
SortCallLikeNamedArgsRector::class,
SortAttributeNamedArgsRector::class,
ExplicitAttributeNamedArgsRector::class,
NarrowPreviousExceptionCodeArgRector::class,
]);
};

This file was deleted.

This file was deleted.

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

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

class SkipNamedArgument
{
public function run()
{
try {
$this->run();
}catch(\Throwable $throwable) {
throw new \LogicException('Some exception', previous: $throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

use LogicException;

class SkipNamedArgumentForMessageAndPrevious
{
public function run()
{
try {
$this->run();
}catch(\Throwable $throwable) {
throw new LogicException(message: 'Some exception', previous: $throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector\Fixture;

final class Named
{
public function run()
{
try {
$this->run();
} catch (\Throwable $throwable) {
throw new \RuntimeException(message: 'Failed', code: $throwable->getCode(), previous: $throwable);
}
}

public function mixed()
{
try {
$this->run();
} catch (\Throwable $throwable) {
throw new \RuntimeException('Failed', $throwable->getCode(), previous: $throwable);
}
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector\Fixture;

final class Named
{
public function run()
{
try {
$this->run();
} catch (\Throwable $throwable) {
throw new \RuntimeException(message: 'Failed', previous: $throwable);
}
}

public function mixed()
{
try {
$this->run();
} catch (\Throwable $throwable) {
throw new \RuntimeException('Failed', previous: $throwable);
}
}
}

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

namespace Rector\Tests\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector\Fixture;

final class Positional
{
public function run()
{
try {
$this->run();
} catch (\Throwable $throwable) {
throw new \RuntimeException('Failed', $throwable->getCode(), $throwable);
}
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector\Fixture;

final class Positional
{
public function run()
{
try {
$this->run();
} catch (\Throwable $throwable) {
throw new \RuntimeException('Failed', previous: $throwable);
}
}
}

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

namespace Rector\Tests\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector\Fixture;

final class CustomOrderException extends \RuntimeException
{
public function __construct(string $message, \Throwable $previous, int $code)
{
parent::__construct($message, $code, $previous);
}
}

final class SkipCustomParameterOrder
{
public function run()
{
try {
$this->run();
} catch (\Throwable $throwable) {
throw new CustomOrderException('Failed', $throwable->getCode(), $throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector\Fixture;

final class SkipDifferentCode
{
public function run(\Throwable $other)
{
try {
$this->run($other);
} catch (\Throwable $throwable) {
throw new \RuntimeException('Failed', 404, $throwable);
}
}

public function otherVariable(\Throwable $other)
{
try {
$this->run($other);
} catch (\Throwable $throwable) {
throw new \RuntimeException('Failed', $other->getCode(), $throwable);
}
}

public function missingPrevious(\Throwable $other)
{
try {
$this->run($other);
} catch (\Throwable $throwable) {
throw new \RuntimeException('Failed', $throwable->getCode());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Tests\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class NarrowPreviousExceptionCodeArgRectorTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

use Rector\CodeQuality\Rector\New_\NarrowPreviousExceptionCodeArgRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
->withRules([NarrowPreviousExceptionCodeArgRector::class]);
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,8 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
/** @var Arg $arg1 */
$arg1 = $new->args[1];
if ($arg1->name instanceof Identifier && $arg1->name->toString() === 'previous') {
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) {
// previous is already passed, code is omitted on purpose
if (! $hasChanged) {
return null;
}
} elseif ($this->hasParameter($new, 'previous') && ! $this->hasArgument($new, 'previous')) {
Expand Down
Loading
Loading