feat: add four new native diagnostics#288
Open
calebdw wants to merge 4 commits into
Open
Conversation
Flag invalid enum declarations that would cause PHP fatal errors: - Backed enum cases missing a value - Unit enum cases that have a value - Backing types other than int or string (e.g. bool, float) - Duplicate backed values across cases When an invalid backing type is detected, per-case value checks are skipped since they would be noise on top of the root cause. Closes #199 Closes #278 (enum-related portion)
Extend the missing-method diagnostic and code action to cover abstract methods declared in used traits. Previously only interface methods and abstract parent class methods were checked. Three changes: - collect_missing_methods now walks used_traits for abstract methods (with BackedEnum/UnitEnum filtered out for enums) - The early-return in implementation_errors that skipped classes with no interfaces and no parent now also considers used_traits - method_source_description reports the originating trait name Closes #278
When a parent method declares a `static` return type (including `?static`), child classes must also use `static` — using `self` narrows the contract and causes a PHP fatal error at runtime. This diagnostic catches that mistake at edit time. Checks all method sources: parent classes, interfaces, and traits. Uses `native_return_type` (the PHP type hint) rather than the effective docblock type, since PHP only enforces native hints. Closes #198
When a match expression's subject has a known scalar type, literal arm conditions of a different scalar type are flagged as unreachable since match uses strict comparison (===). For example, an int literal in a match against a string subject will never match. Uses the Walker trait to collect match expressions from the AST, then resolves the subject type via the forward walker. Handles union and nullable subject types conservatively: a literal is only flagged when it is incompatible with every branch of the union. Closes #200
|
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
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
This PR adds four new native diagnostics requested in issues #199, #278, #198, and #200. Each is a separate commit.
Enum declaration diagnostics (Closes #199)
Flags invalid enum declarations that would cause PHP fatal errors:
enum Foo: int) where a case lacks= value.enum Foo) where a case has= value.intorstring(e.g.enum Foo: bool).When an invalid backing type is detected, per-case value checks are skipped since they would be noise on top of the root cause.
Unimplemented trait abstract methods (Closes #278)
Concrete classes that use a trait with abstract methods without implementing them are now flagged as errors, matching PHP's fatal error at runtime. Extends the existing
collect_missing_methodsinfrastructure (used by both the diagnostic and the "Implement missing methods" code action) with a newcollect_abstract_from_used_traitspass. Also fixes an early-return that previously skipped classes with onlyused_traits(no interfaces or parent class).Incompatible
staticreturn type override (Closes #198)When a parent method declares a
staticreturn type (including?static), child classes must also usestatic— usingselfnarrows the contract and causes a PHP fatal error at runtime. This diagnostic catches that mistake at edit time. Usesnative_return_typerather than the effective docblock type, since PHP only enforces native type hints.Match arm type checking (Closes #200)
matchexpressions where a literal arm condition can never match the subject's type under strict comparison (===) now produce a warning. For example, anintliteral in a match against astringsubject is flagged as unreachable. Uses the Walker trait to collect match expressions from the AST, then resolves the subject type via the forward walker. Handles union and nullable subject types conservatively: a literal is only flagged when it is incompatible with every branch of the union.Test coverage
All pass. Clippy and fmt clean.