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,27 @@
<?php

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

/**
* This direct use new ExceptionWithCustomParam() inside the class is on purpose to reproduce the issue
*/
final class ExceptionWithCustomParam extends \RuntimeException
{
public function __construct(
public readonly bool $flag,
string $message,
int $code = 0,
?\Throwable $previous = null,
) {
parent::__construct($message, $code, $previous);
}

public function run(): void
{
try {
throw new \RuntimeException('root');
} catch (\Throwable $e) {
throw new ExceptionWithCustomParam(flag: true, message: $e->getMessage(), previous: $e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
}

if (! isset($new->getArgs()[1])) {
if ($this->hasCodeParameter($new->class)) {
if ($this->hasParameter($new, 'code') && ! $this->hasArgument($new, 'code')) {
// get previous code
$new->args[1] = new Arg(
new MethodCall($caughtThrowableVariable, 'getCode'),
Expand All @@ -173,7 +173,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->hasCodeParameter($new->class)) {
if ($this->hasParameter($new, 'code') && ! $this->hasArgument($new, 'code')) {
$new->args[1] = new Arg(
new MethodCall($caughtThrowableVariable, 'getCode'),
name: $shouldUseNamedArguments ? new Identifier('code') : null
Expand All @@ -183,11 +183,14 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
} elseif (! $hasChanged) {
return null;
}
} else {
} elseif ($this->hasParameter($new, 'previous') && ! $this->hasArgument($new, 'previous')) {
$new->args[$exceptionArgumentPosition] = new Arg(
$caughtThrowableVariable,
name: $shouldUseNamedArguments ? new Identifier('previous') : null
);
$hasChanged = true;
} elseif (! $hasChanged) {
return null;
}

// null the node, to fix broken format preserving printers, see https://github.com/rectorphp/rector/issues/5576
Expand All @@ -197,9 +200,13 @@ private function refactorThrow(Throw_ $throw, Variable $caughtThrowableVariable)
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}

private function hasCodeParameter(Name $exceptionName): bool
private function hasParameter(New_ $new, string $parameterName): bool
{
$className = $this->getName($exceptionName);
$className = $this->getName($new->class);
if ($className === null) {
return false;
}

if (! $this->reflectionProvider->hasClass($className)) {
return false;
}
Expand All @@ -217,7 +224,18 @@ private function hasCodeParameter(Name $exceptionName): bool
);

foreach ($extendedParametersAcceptor->getParameters() as $extendedParameterReflection) {
if ($extendedParameterReflection->getName() === 'code') {
if ($extendedParameterReflection->getName() === $parameterName) {
return true;
}
}

return false;
}

private function hasArgument(New_ $new, string $argumentName): bool
{
foreach ($new->getArgs() as $arg) {
if ($arg->name instanceof Identifier && $arg->name->toString() === $argumentName) {
return true;
}
}
Expand Down
Loading