Repository navigation
Allow default values of readonly properties on PHP 8.6 - #7082
Merged
Merged
Conversation
PHP 8.6 allows readonly properties to declare a default value (https://wiki.php.net/rfc/readonly_property_defaults). The default value initializes the property, so assigning it again in a constructor is reported as an already assigned readonly property. Reinitialization in __clone() and __unserialize() stays allowed, same as in PHP. Amp-Thread-ID: https://ampcode.com/threads/T-01a1dfee-ec21-74a8-a643-d9aba88acbdd
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.
PHP 8.6 allows readonly properties to declare a default value (RFC, implementation).
property.readOnlyDefaultValueis no longer reported when the analysed PHP version is 8.6 or later.assign.readOnlyProperty("Readonly property X::$foo is already assigned."), same as PHP's "Cannot modify readonly property" error.__clone()(once), clone-with, and__unserialize()stays allowed, as in PHP.I went through the tests added in the php-src commit: PHPStan reports no errors where PHP accepts the code, and reports an error wherever PHP throws, except where it is intentionally stricter (uninitialized readonly properties, assignments outside of constructors). The only case not reported is two traits declaring the same property with different default values; PHPStan has no check for incompatible trait property composition at all, independent of readonly.
Closes phpstan/phpstan#15363