Fix phpstan/phpstan#13853: Smarter check about ununitialized readonly properties#5323
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#13853: Smarter check about ununitialized readonly properties#5323phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…sset guard is present - Track PropertyInitializationExpr with NeverType in the falsy branch of isset($this->prop) in TypeSpecifier - Skip readOnlyAssignNotInConstructor error in ReadOnlyPropertyAssignRule when PropertyInitializationExpr indicates property is known uninitialized - Adjust IssetCheck and ClassPropertiesNode to treat NeverType PropertyInitializationExpr as "not initialized" - New regression test in tests/PHPStan/Rules/Properties/data/bug-13853.php
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
When a readonly property is assigned outside the constructor but is guarded by an
isset()check (e.g.,if (!isset($this->prop)) { $this->prop = ...; }), PHPStan no longer reportsproperty.readOnlyAssignNotInConstructor. Theisset()guard ensures the property can only be assigned once, preventing the runtime error that the rule is designed to catch.Changes
src/Analyser/TypeSpecifier.php: In the falsy branch ofisset($this->prop), trackPropertyInitializationExprwithNeverTypeto indicate the property is known to be uninitialized.src/Rules/Properties/ReadOnlyPropertyAssignRule.php: SkipreadOnlyAssignNotInConstructorerror whenPropertyInitializationExprfor the property hasNeverType(meaning we're in the false branch of anissetcheck).src/Rules/IssetCheck.php: ExcludeNeverTypePropertyInitializationExprfrom the "property is initialized" check so nestedissetchecks within!issetguards don't produce false positives.src/Node/ClassPropertiesNode.php: AddhasPropertyInitialization()helper that treatsNeverTypePropertyInitializationExpras "not initialized", fixing false "already assigned" errors in constructors usingissetguards.Root cause
The
ReadOnlyPropertyAssignRuleunconditionally reported any readonly property assignment outside the constructor as an error. It did not consider that anisset()guard makes the assignment safe by ensuring the property is only assigned when uninitialized. The fix leverages the existingPropertyInitializationExprtracking mechanism, usingNeverTypeas a sentinel value to distinguish "known uninitialized via isset" from "initialized" (which usesMixedType).Test
Added
tests/PHPStan/Rules/Properties/data/bug-13853.phpwith three test cases:BaseReportLocator: readonly property assigned afterif (isset(...)) { return; }guard - no error expectedAnotherExample: readonly property assigned insideif (!isset(...))guard - no error expectedNoIssetGuard: readonly property assigned without anyissetguard - error still expectedFixes phpstan/phpstan#13853