Skip to content

Support @phpstan-assert tags above property hooks, check assert tags of overrides - #7291

Open
ondrejmirtes wants to merge 5 commits into
2.3.xfrom
property-hook-asserts
Open

ondrejmirtes wants to merge 5 commits into
2.3.xfrom
property-hook-asserts

Conversation

@ondrejmirtes

@ondrejmirtes ondrejmirtes commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Property hook asserts

@phpstan-assert, @phpstan-assert-if-true and @phpstan-assert-if-false written 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. $this stands for the object the property is fetched from:

public bool $sponsored {
	/**
	 * @phpstan-assert-if-true !null $this->sponsorName
	 * @phpstan-assert-if-true !null $this->sponsorUrl
	 */
	get => $this->sponsorName !== null && $this->sponsorUrl !== null;
}

if ($foo->sponsored) {
	// $foo->sponsorName and $foo->sponsorUrl are string
}

PropertyFetchHandler applies 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 $x together with -if-true B $y used to narrow $x to not-A and $y to 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:

  • Tags about the same expression combine into one negated tag.
  • Tags about different expressions narrow nothing in the other branch.

The same goes for several -if-false tags 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.

  • Bool return type: conditional asserts require a bool return type (or property type). true and false are allowed. A method without any declared return type is skipped. Identifier: assert.nonBool.
  • Not above the property: assert tags above a property itself (hooked, plain or promoted) are reported, with a tip to move them above the get hook when there is one. Identifier: assert.property.
  • Not above a set hook: a set hook returns void and runs on assignment, so there is no result to narrow by. Identifier: assert.setHook.
  • Overrides: an overriding method or property has to narrow every expression its parent's tags narrow at least as much. This is checked after the call and in each branch the child can return. Identifiers: method.childAssertType and property.childAssertType.
    • Conditional asserts narrow both branches (the other one with the negated type), so they are effectively invariant. This matches Python's TypeIs and Flow's default type guards.
    • Equality (=T) and unconditional asserts are covariant.
    • A child that never returns false (or true) is checked only in the branch it can take.
    • A child that has tags of its own and drops one of the parent's is reported, because tags are inherited only when the child has none. A child without tags keeps inheriting.
    • Parameters are matched by position, so renaming them is fine.

Closes phpstan/phpstan#10463
Closes phpstan/phpstan#13259

`@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
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.

Allow @phpstan-assert-if-true on properties Assertions on properties

1 participant