Skip to content

Support #[\Override] on class constants and enum cases - #7077

Merged
ondrejmirtes merged 3 commits into
2.3.xfrom
override-class-constants
Nov 15, 2026
Merged

ondrejmirtes merged 3 commits into
2.3.xfrom
override-class-constants

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Closes phpstan/phpstan#15370

PHP 8.6 allows #[\Override] on class constants and enum cases (RFC). This follows how #[\Override] on properties works.

  • Override that overrides nothing (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.
  • Missing Override (classConstant.missingOverride, enum.caseMissingOverride): controlled by the new checkMissingOverrideConstantAttribute parameter. Its default null reports when checkMissingOverridePropertyAttribute or checkMissingOverrideMethodAttribute is true, 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.
  • Override below PHP 8.6 (classConstant.overrideAttribute, enum.caseOverrideAttribute): reported based on the scope's PHP version.
  • Enum case overriding an interface constant: when the constant has any native type, the enum case is reported (enum.caseNativeType). PHP rejects this even for mixed or UnitEnum, 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 follow checkMissingOverrideMethodAttribute based 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+.

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
@ondrejmirtes
ondrejmirtes force-pushed the override-class-constants branch from 6e7a082 to 81b2176 Compare November 15, 2026 12:08
@ondrejmirtes
ondrejmirtes merged commit 16b48af into 2.3.x Nov 15, 2026
897 of 907 checks passed
@ondrejmirtes
ondrejmirtes deleted the override-class-constants branch November 15, 2026 14:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

#[\Override] can be applied to class constants, including enum cases in PHP 8.6

1 participant