Skip to content

[CodeQuality] Add inherit_code option to ThrowWithPreviousExceptionRector - #9014

Open
blancks wants to merge 3 commits into
rectorphp:mainfrom
blancks:feat/configurable-code-inherit-in-throw-with-previous
Open

blancks wants to merge 3 commits into
rectorphp:mainfrom
blancks:feat/configurable-code-inherit-in-throw-with-previous

Conversation

@blancks

@blancks blancks commented Nov 23, 2026 •

Copy link
Copy Markdown

What this PR adds

Hi there! This adds an opt-in inherit_code option to ThrowWithPreviousExceptionRector, so rethrowing inside a catch can rely on previous: alone instead of copying the caught exception's code.

Why I did it

I first wrote this as a custom rule in a project I'm working on, both to address type inconsistencies (PDOException returning code strings while standard exceptions usually expects an int) and for what I think to be a misleading semantics.

If you have a coordinator on top of several underlying implementations, the same code can mean different things in different places. Re-throwing it under a new exception makes it look like the coordinator's own code when instead it only got a meaning for the specific root exception.

Since the rule already exists here and does most of the job, I figured it's nicer to make this a built-in option than to keep a custom rule around 😛

What is changed

ThrowWithPreviousExceptionRector now implements ConfigurableRectorInterface.

With inherit_code => true (default), the current behavior is untouched.

With inherit_code => false, the following applies:

  • new Foo('msg', $e->getCode(), $e) becomes new Foo('msg', previous: $e)
  • new Foo('msg', $e->getCode()) becomes new Foo('msg', previous: $e)
  • new Foo('msg', code: $e->getCode(), previous: $e) becomes new Foo('msg', previous: $e)
  • new Foo('msg') becomes new Foo('msg', previous: $e) when the exception's constructor has a previous parameter
  • It doesn't touch explicit codes that aren't inherited from $e, catches without a variable, and throws that already pass previous.

Usage

return RectorConfig::configure()
    ->withConfiguredRule(ThrowWithPreviousExceptionRector::class, [
        ThrowWithPreviousExceptionRector::INHERIT_CODE => false,
    ]);

Thanks for the awesome project, by the way! ❤️

I'm also happy to adjust anything that doesn't quite fit the project's standards or conventions.

@TomasVotruba

Copy link
Copy Markdown
Member

Looking at this, it would better fit a new rule in code-quality set. Most likely nobody will configure it, but project might benefit from leaner syntax.

@blancks

blancks commented Nov 23, 2026

Copy link
Copy Markdown
Author

@TomasVotruba I discarded that approach when preparing this PR because the two rules would have overlapping behavior around the code parameter. ThrowWithPreviousException would actively try to add it, while the new rule would attempt to remove it.

This means enabling the new rule would also require explicitly skipping the existing one, which IMO results in a worse dev experience for this particular case.

If you think a dedicated rule would be a better fit, I'm absolutely happy to refactor it! I just wanted to make sure that's the preferred direction before going ahead with it

@TomasVotruba

Copy link
Copy Markdown
Member

@blancks It should only trigger in case when all 3 args are filled:

new Foo('msg', $e->getCode(), $e)

new Foo('msg', code: $e->getCode(), previous: $e)

That way they're indempendent and idempotent 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants