Skip to content

Let @psalm- purity tags win over @pure and @phpstan-pure - #14071

Merged
orklah merged 3 commits into
vimeo:masterfrom
shu-chen-dot-ca:fix-13607-psalm-purity-tag-precedence
Nov 20, 2026
Merged

orklah merged 3 commits into
vimeo:masterfrom
shu-chen-dot-ca:fix-13607-psalm-purity-tag-precedence

Conversation

@shu-chen-dot-ca

@shu-chen-dot-ca shu-chen-dot-ca commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

@pure and @phpstan-pure

@pure and @phpstan-pure override @psalm-capabilities and @psalm-impure on the same function or method, whatever the tag order.

Before

<?php
interface Message {
    /**
     * @pure
     * @psalm-capabilities read-props
     */
    public function body(): string;
}

final class MyMessage implements Message {
    public function __construct(private string $body) {}

    public function body(): string { // ImmutableDependency: Message::body is pure, but MyMessage::body additionally requires read-props
        return $this->body;
    }
}

After

    public function body(): string { // no issue

The PhpStorm Pure and Immutable attributes

#[JetBrains\PhpStorm\Pure] overrides @psalm-capabilities the same way, and #[JetBrains\PhpStorm\Immutable] narrows a class's @psalm-capabilities to read-props.

Before

<?php
interface Message {
    /** @psalm-capabilities read-props */
    #[\JetBrains\PhpStorm\Pure]
    public function body(): string;
}

final class MyMessage implements Message {
    public function __construct(private string $body) {}

    public function body(): string { // ImmutableDependency: Message::body is pure, but MyMessage::body additionally requires read-props
        return $this->body;
    }
}

/** @psalm-capabilities read-props|write-this-props */
#[\JetBrains\PhpStorm\Immutable]
final class Counter {
    private int $n = 0;

    public function next(): int {
        return ++$this->n; // InaccessibleProperty: Counter::$n is marked readonly
    }
}

After

    public function body(): string { // no issue
        return ++$this->n; // no issue
Before After
before after

Fixes #13607

@github-actions

github-actions Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 765 99.4 $\color{gray}{\textsf{-1.1\%}}$ 25 918 53 210 578
✅ symfony 3 622 200.1 $\color{gray}{\textsf{+0.2\%}}$ 50 660 98 733 1 443
✅ doctrine-orm 393 21.4 $\color{gray}{\textsf{-0.5\%}}$ 3 479 6 160 0
✅ psl 632 25.9 $\color{gray}{\textsf{-0.5\%}}$ 1 305 5 461 1
✅ phpunit 471 19.9 $\color{gray}{\textsf{-0.2\%}}$ 1 742 3 452 17
✅ php-parser 236 13.7 $\color{gray}{\textsf{+0.7\%}}$ 17 133 21 035 0
✅ tempest 899 35.2 $\color{gray}{\textsf{+0.1\%}}$ 5 021 10 514 221
✅ amphp-amp 61 1.4 $\color{gray}{\textsf{-3.6\%}}$ 29 49 0
✅ webmozarts-assert 285 3.2 $\color{gray}{\textsf{+0.6\%}}$ 577 2 722 0
✅ guzzle-promises 55 1.4 $\color{gray}{\textsf{-0.7\%}}$ 158 435 2
✅ monolog 153 3.6 $\color{gray}{\textsf{-4.7\%}}$ 903 2 403 10
✅ drupal 4 821 232.0 $\color{gray}{\textsf{-2.9\%}}$ 97 526 169 331 508
Total 13 395 657.3 $\color{gray}{\textsf{-1.2\%}}$ 204 451 373 505 2 780
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +20 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Thresholds apply per project, not to the total.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.

head e5fcb73 · base 67cfa84 (merge-base) · workflow run

@alies-dev alies-dev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 The docblock fix is correct for @pure and @phpstan-pure, but the same bug remains for the #[JetBrains\PhpStorm\Pure] attribute, so the fix is incomplete.

I checked against current master (2d8a48d), with this branch merged in (clean merge). The example from #13607 fails on master and is clean with the branch. The same holds for @phpstan-pure + @psalm-mutation-free, and @pure + @psalm-impure is now impure. A lone @pure still forbids reading props (your pureAloneStillForbidsReadingProps guard).

Findings

  1. src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php (the attribute loop, Psalm\Pure / JetBrains\PhpStorm\Pure): the generic #[JetBrains\PhpStorm\Pure] attribute sets capabilities = NONE and still overrides an explicit @psalm-capabilities. This is the same library scenario as the issue (pure for other tools, read-props for Psalm 7), with the PhpStorm attribute instead of the tag. On the branch:
    interface M {
        /** @psalm-capabilities read-props */
        #[\JetBrains\PhpStorm\Pure]
        public function body(): string;
    }
    final class MM implements M {
        public function __construct(private string $body) {}
        public function body(): string { return $this->body; }
    }
    // ImmutableDependency: M::body is pure, but MM::body additionally requires read-props
    The docs section you edit lists #[Psalm\Pure] and #[JetBrains\PhpStorm\Pure] next to @pure, so users will expect the new precedence rule to apply to the attribute too. #[Psalm\Pure] is Psalm's own and can keep behaving like @psalm-pure. The JetBrains one should not override a @psalm- capability tag. Please apply the same precedence there and add a test.

Recommendations (non-blocking)

  • FunctionLikeDocblockParser: the new elseif repeats the two assignments of the first branch. A local $generic_pure = isset(...phpstan-pure) || isset(...pure) consulted as the last link of the chain would keep one copy and make the precedence order readable in one place.
  • The docs sentence says "A @psalm- tag from the table wins over @pure". @psalm-pure is in that table too, and @psalm-pure + @psalm-capabilities on one method still resolves to @psalm-pure. Saying "a @psalm-capabilities or @psalm-impure tag" would be more accurate (the mutation-free tags are parse-only and should stay out of the docs).
  • UPGRADING.md: a docblock with both @pure and a @psalm- capability tag changes meaning, so it deserves an entry.

@shu-chen-dot-ca

Copy link
Copy Markdown
Contributor Author

Thanks for the review, all done except one point:

  • #[JetBrains\PhpStorm\Pure] now only applies when the docblock has no purity tag, so your example is clean and @psalm-impure wins too. #[Psalm\Pure] still wins as before. The class-level #[JetBrains\PhpStorm\Immutable] had the same problem: it narrowed a class's @psalm-capabilities read-props|write-this-props down to read-props. It now gives way to the class's purity tag in the same way. Tests: psalmCapabilitiesWinOverPhpStormPure, psalmImpureWinsOverPhpStormPure and psalmCapabilitiesWinOverPhpStormImmutable fail without this commit. The guards phpStormPureAloneStillForbidsReadingProps, psalmPureAttributeStillWinsOverPsalmCapabilities and phpStormImmutableAloneIsStillReadProps show the attributes alone, and #[Psalm\Pure], behave as before.
  • Docs: the sentence now names @psalm-capabilities and @psalm-impure only (and the PhpStorm attributes), without the mutation-free tags.
  • UPGRADING.md: added a [BC] entry.
  • $generic_pure: I left the parser as it is. @psalm-pure has to stay the first link, since it wins over @psalm-capabilities. A single NONE branch would then need the other four tags negated in its condition, which I found harder to read than the two assignments at the end of the chain.

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. The docblock change and the PhpStorm attribute handling look right, and each new
case fails on master and passes here.

One issue with the new attribute guard: !$storage->has_mutations_annotation is also true after an earlier
#[Psalm\ExternalMutationFree] (or #[Psalm\Pure]/#[Psalm\Immutable]) in the same loop, not only after a
docblock tag, so the result now depends on attribute order:

#[\Psalm\ExternalMutationFree]
#[\JetBrains\PhpStorm\Immutable]
final class A {
    private int $n = 0;
    public function next(): int {
        return ++$this->n; // no issue on this branch
    }
}

#[\JetBrains\PhpStorm\Immutable]
#[\Psalm\ExternalMutationFree]
final class B {
    private int $n = 0;
    public function next(): int {
        return ++$this->n; // InaccessibleProperty + ImpurePropertyAssignment
    }
}

The same happens on a method with #[Psalm\ExternalMutationFree] and #[JetBrains\PhpStorm\Pure]. On master
both orders behave the same. Could you read the docblock state once before the attribute loop
($docblock_has_mutations_annotation = $storage->has_mutations_annotation;) and test that in both
FunctionLikeNodeScanner and ClassLikeNodeScanner, with a test case for one of these combinations?

@shu-chen-dot-ca

Copy link
Copy Markdown
Contributor Author

Good catch, thanks. Both scanners now read $docblock_has_mutations_annotation once before the attribute loop and test that, so a #[Psalm\...] attribute earlier in the list no longer makes the PhpStorm one skip. Your four cases (both classes and both methods) report the same errors as master in either order now. New tests: psalmAttributeBeforePhpStormImmutableStillNarrows and psalmAttributeBeforePhpStormPureStillNarrows fail on the previous commit, and psalmAttributeAfterPhpStormImmutableStillNarrows covers the other order.

@shu-chen-dot-ca
shu-chen-dot-ca force-pushed the fix-13607-psalm-purity-tag-precedence branch from 5edc8aa to b2f486b Compare November 20, 2026 00:58
@orklah
orklah force-pushed the fix-13607-psalm-purity-tag-precedence branch from b2f486b to e5fcb73 Compare November 20, 2026 07:04
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 20, 2026

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the fix and the follow-up changes.

@orklah
orklah merged commit 8156a36 into vimeo:master Nov 20, 2026
70 of 71 checks passed
@shu-chen-dot-ca
shu-chen-dot-ca deleted the fix-13607-psalm-purity-tag-precedence branch November 20, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

@pure / @phpstan-pure override an explicit @psalm-capabilities or @psalm-mutation-free on the same method

3 participants