Repository navigation
Support #[\Override] on class constants and enum cases - #7077
Merged
Merged
Conversation
PHP 8.6 allows #[\Override] on class constants and enum cases. - Report #[\Override] on a constant or enum case that overrides nothing, with a fix that removes the attribute - Report a missing #[\Override] when checkMissingOverrideConstantAttribute is enabled, with a fix that adds it; the default null follows checkMissingOverridePropertyAttribute and checkMissingOverrideMethodAttribute on PHP 8.6+ - Report #[\Override] on class constants and enum cases below PHP 8.6 - Report enum cases overriding a typed or final interface constant Closes phpstan/phpstan#15370 Amp-Thread-ID: https://ampcode.com/threads/T-01a1df02-11a2-7485-9bf7-a0b7cacc89ad
…analysed PHP version supports it With checkMissingOverridePropertyAttribute: null, the rule followed checkMissingOverrideMethodAttribute based on the single PHP version PHPStan runs with. A project requiring "php": "^8.4" in composer.json was told to add #[\Override], which PHP 8.4 rejects on properties. Use the analysed PHP version range from the scope instead. Amp-Thread-ID: https://ampcode.com/threads/T-01a1df02-11a2-7485-9bf7-a0b7cacc89ad
…very analysed PHP version supports it Amp-Thread-ID: https://ampcode.com/threads/T-01a1df02-11a2-7485-9bf7-a0b7cacc89ad
ondrejmirtes
force-pushed
the
override-class-constants
branch
from
November 15, 2026 12:08
6e7a082 to
81b2176
Compare
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.
Closes phpstan/phpstan#15370
PHP 8.6 allows
#[\Override]on class constants and enum cases (RFC). This follows how#[\Override]on properties works.classConstant.override,enum.caseOverride): reported on every PHP version, like methods and properties. The fix removes the attribute. Private parent constants don't count, constants from traits are checked in the using class, and the behaviour of interfaces extending interfaces and anonymous classes follows the php-src tests.classConstant.missingOverride,enum.caseMissingOverride): controlled by the newcheckMissingOverrideConstantAttributeparameter. Its defaultnullreports whencheckMissingOverridePropertyAttributeorcheckMissingOverrideMethodAttributeistrue, but only when every analysed PHP version supports the attribute (the scope's PHP version range, e.g. not with"php": "^8.5"in composer.json). The fix adds the attribute. Constants declared in traits are skipped.classConstant.overrideAttribute,enum.caseOverrideAttribute): reported based on the scope's PHP version.enum.caseNativeType). PHP rejects this even formixedorUnitEnum, because an enum case is an untyped constant. When the constant only has a PHPDoc type, the enum case object has to fit it (enum.caseType). Overriding a final constant is also reported (enum.caseOverridesFinal).For
const A = 1, B = 2;the attribute applies to both constants, so no fix is offered there.The same applies now to
checkMissingOverridePropertyAttribute: null: it used to followcheckMissingOverrideMethodAttributebased on the single PHP version PHPStan runs with, so a project requiring"php": "^8.4"analysed with PHP 8.5 was told to add#[\Override]to properties, which PHP 8.4 rejects. It now reports only when the whole analysed range is PHP 8.5+.