Repository navigation
Support @phpstan-assert tags above property hooks, check assert tags of overrides - #7291
Open
ondrejmirtes wants to merge 5 commits into
Open
ondrejmirtes wants to merge 5 commits into
ondrejmirtes wants to merge 5 commits into
Conversation
`@phpstan-assert-if-true A $x` together with `@phpstan-assert-if-true B $y` means that a true result says both hold, so a false result says only that at least one of them does not. Each tag used to be negated on its own, narrowing $x to not-A and $y to not-B in the false branch. Several tags are now negated together: about the same expression they combine into one negated tag, about different expressions they narrow nothing. The same goes for several -if-false tags in the true branch. Amp-Thread-ID: https://ampcode.com/threads/T-01a23672-d1c1-7389-8ee4-edae78fcf9ec
…of overrides
Assert tags written above a property with a get hook (or above the hook
itself) narrow their subjects when the property is read, with $this
standing for the object the property is fetched from:
/** @phpstan-assert-if-true !null $this->sponsorName */
public bool $sponsored { get => $this->sponsorName !== null; }
New checks, always on for properties and behind the bleeding edge toggle
checkAssertTagsCompatibility for methods and functions:
- conditional asserts require a bool return type (property type)
- assert tags above a property without a get hook are reported
- an overriding method or property has to narrow every expression its
parent's tags narrow at least as much, after the call and in each
branch it can return. Conditional asserts narrow both branches, so
they can't change in either direction. Parameters are matched by
position.
Amp-Thread-ID: https://ampcode.com/threads/T-01a23672-d1c1-7389-8ee4-edae78fcf9ec
Tags above the property itself are reported and no longer narrow, tags above a set hook are reported because it returns void. A get hook without assert tags inherits them from the get hook it overrides, like a method. Amp-Thread-ID: https://ampcode.com/threads/T-01a23672-d1c1-7389-8ee4-edae78fcf9ec
This was referenced Dec 2, 2026
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.
Property hook asserts
@phpstan-assert,@phpstan-assert-if-trueand@phpstan-assert-if-falsewritten above a get hook now narrow their subjects when the property is read. Hooks are the method-like part of a property, so the tags go there and not above the property.$thisstands for the object the property is fetched from:PropertyFetchHandlerapplies the get hook's asserts the same way method call asserts are applied. A get hook without tags inherits them from the get hook it overrides, including abstract{ get; }hooks in interfaces, with template types resolved like inherited method PHPDocs. This also works for generic classes and for?->. The turbo-ext mirrors (PropertyFetchHandler,DefaultNarrowingHelper,PhpClassReflectionExtension) are updated too.Negating several conditional asserts
A separate commit fixes behaviour that already existed.
-if-true A $xtogether with-if-true B $yused to narrow$xto not-A and$yto not-B in the false branch, but a false result only says that at least one of them does not hold. Several tags are now negated together:The same goes for several
-if-falsetags in the true branch.New checks
The checks are always on for property hooks. For methods and functions they are behind the bleeding-edge toggle
checkAssertTagsCompatibility.trueandfalseare allowed. A method without any declared return type is skipped. Identifier:assert.nonBool.assert.property.assert.setHook.method.childAssertTypeandproperty.childAssertType.TypeIsand Flow's default type guards.=T) and unconditional asserts are covariant.false(ortrue) is checked only in the branch it can take.Closes phpstan/phpstan#10463
Closes phpstan/phpstan#13259