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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ fn (int $key, Item $item, string $unused) => $item->ok(); // unused parameter $u
### requirePreviousExceptionPass
- Detects forgotten exception pass-as-previous when re-throwing
- Checks if caught exception can be passed as argument to the call (including constructor call) in `throw` node inside the catch block
- Accepts the caught exception also as an argument of a nested call, e.g. `throw new HttpException(401, 'Locked', MyException::fromPrevious($e))`
- You may encounter false-positives in some edge-cases, where you do not want to pass exception as previous, feel free to ignore those

```php
Expand Down
46 changes: 25 additions & 21 deletions src/Rule/RequirePreviousExceptionPassRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,27 +106,7 @@ private function processExceptionCreation(
Scope $scope,
): array
{
$passed = false;

foreach ($node->getArgs() as $argument) {
if (!$argument->value instanceof Variable) {
continue; // support only simple variable pass
}

$argumentVariableName = $argument->value->name;

if (!is_string($argumentVariableName)) {
continue;
}

if ($caughtExceptionVariableName === null) {
continue;
}

if ($caughtExceptionVariableName === $argumentVariableName) {
$passed = true;
}
}
$passed = $caughtExceptionVariableName !== null && $this->isVariablePassed($node, $caughtExceptionVariableName);

if (!$this->reportEvenIfExceptionIsNotAcceptableByRethrownOne) {
$accepts = false;
Expand All @@ -152,6 +132,30 @@ private function processExceptionCreation(
return [];
}

private function isVariablePassed(
CallLike $node,
string $variableName,
): bool
{
if ($node->isFirstClassCallable()) {
return false;
}

foreach ($node->getArgs() as $argument) {
$value = $argument->value;

if ($value instanceof Variable && $value->name === $variableName) {
return true;
}

if ($value instanceof CallLike && $this->isVariablePassed($value, $variableName)) {
return true;
}
}

return false;
}

/**
* @return list<ParameterReflection>
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,23 @@ public static function createWithoutPrevious(): self
} catch (LogicException|RuntimeException $e) { // Logic can never occur here, so Runtime must be passed
throw MyException::createForSpecificPrevious(); // error: Exception $e not passed as previous to \RequirePreviousExceptionPassRule\With\MyException::createForSpecificPrevious()
}

try {
} catch (RuntimeException $e) {
throw new LogicException('Message', 0, MyException::createForAnyPrevious($e));
}

try {
} catch (RuntimeException $e) {
throw new LogicException('Message', 0, MyException::createForAnyPrevious(new RuntimeException('Inner', 0, $e)));
}

try {
} catch (RuntimeException $e) {
throw new LogicException('Message', 0, MyException::createWithoutPrevious()); // error: Exception $e not passed as previous to new \LogicException('Message', 0, \RequirePreviousExceptionPassRule\With\MyException::createWithoutPrevious())
}

try {
} catch (RuntimeException $e) {
throw new LogicException('Message', 0, MyException::createForAnyPrevious(...)); // error: Exception $e not passed as previous to new \LogicException('Message', 0, \RequirePreviousExceptionPassRule\With\MyException::createForAnyPrevious(...))
}
Loading