Add new Rector rules and improve compatibility#6
Merged
Conversation
Transform patterns like:
$value = getValue();
if ($value === null) {
throw new Exception();
}
Into:
$value = getValue() ?? throw new Exception();
Also supports the elvis operator pattern for falsy checks.
Co-Authored-By: Claude <noreply@anthropic.com>
- Use `ClassMethod::class` and `Function_::class` instead of `NodeGroup::STMTS_AWARE` which only exists in Rector 2.x - Handle `Stmt\Throw_` (traditional throw statement) in addition to `Expr\Throw_` (PHP 8.0+ throw expression) for AST compatibility - Use `FirstClassCallableRector` in config.php which works in both versions Co-Authored-By: Claude <noreply@anthropic.com>
Member
Author
|
Formatting will be handled by mll-lab/php-cs-fixer-config#14. |
In Rector 2.x, FirstClassCallableRector is deprecated and replaced by ArrayToFirstClassCallableRector. Use class_exists() to conditionally select the correct class based on the installed Rector version. Co-Authored-By: Claude <noreply@anthropic.com>
- Add ElvisToCoalesceRector: converts ?: to ?? when expression type can only be falsy via null (e.g., object|null) - Extract shared isOnlyNullFalsy() logic into NullFalsyTypeTrait - IfThrowToCoalesceThrowRector now prefers ?? over ?: when type analysis shows null is the only possible falsy value Co-Authored-By: Claude <noreply@anthropic.com>
Member
Author
|
Added Also improved @simbig could you re-review? |
simbig
approved these changes
Jan 20, 2026
Change getNativeType() to getType() so docblock types are considered, not just native PHP type declarations. Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IfThrowToCoalesceThrowRectorto transform null-check-then-throw patterns to use the null coalesce operator with throw expressionElvisToCoalesceRectorto convert elvis operator (?:) to null coalesce (??) when the expression can only be falsy via nullFirstClassCallableRectororArrayToFirstClassCallableRectorbased on Rector versionIfThrowToCoalesceThrowRectorExample
Before:
After:
Test plan
🤖 Generated with Claude Code