Skip to content

fix(properties): report a readonly property redeclared as non-readonly, or the reverse, and one without a native type - #15448

Open
jirkace wants to merge 6 commits into
vimeo:masterfrom
jirkace:fix/7767-readonly-redeclaration
Open

jirkace wants to merge 6 commits into
vimeo:masterfrom
jirkace:fix/7767-readonly-redeclaration

Conversation

@jirkace

@jirkace jirkace commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

A property redeclared with or without readonly

Psalm misses the PHP fatal error of a child class that redeclares a parent's property as readonly, or a readonly one as non-readonly.

Before

<?php
class A {
    public int $prop = 1;
}

final class B extends A {
    public readonly int $prop; // no issue

    public function __construct() {
        $this->prop = 2;
    }
}

After

    public readonly int $prop; // OverriddenPropertyAccess: Property B::$prop cannot redeclare the non-readonly property A::$prop as readonly

A readonly property with only a docblock type

Psalm misses the PHP fatal error of a readonly property that has no native type.

Before

<?php
final class A {
    /** @var string */
    public readonly $foo; // no issue

    public function __construct() {
        $this->foo = "";
    }
}

After

    public readonly $foo; // MissingPropertyType: Readonly property A::$foo must have a type

Refs #7767

…y, or the reverse, and one without a native type vimeo#7767

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • New OverriddenPropertyAccess when a class redeclares a non-private property of a parent class declared in PHP code with a different native readonly modifier. PHP: "Cannot redeclare non-readonly property A::$prop as readonly B::$prop" and "Cannot redeclare readonly property A::$prop as non-readonly B::$prop". Promoted properties are compared the same way.
  • New MissingPropertyType ("Readonly property A::$foo must have a type") for a native readonly property that has a docblock type but no native one. PHP: "Readonly property F::$foo must have type". Without a docblock type the existing MissingPropertyType already covers it, so it is not reported twice.
  • Not reported, checked against PHP 8.4: redeclaring a private property of the parent either way, a docblock @readonly parent property, readonly on both sides.
  • Parent classes that are not user code (built-in classes, stubs) are not compared.
  • These are the three snippets of Full Support of PHP 8.1 readonly property #7767 that still reported nothing (https://psalm.dev/r/935f8cf961, https://psalm.dev/r/11c464ac18, https://psalm.dev/r/d41b8e5778). The issue has other parts, so the PR references it without closing it.
  • Perf corpora measured locally (errorLevel="1"): Laravel src/Illuminate 27 720 issues before and after, Symfony 11 773 before and after.

@psalm-github-bot

Copy link
Copy Markdown

I found these snippets:

https://psalm.dev/r/935f8cf961
<?php

/** @psalm-suppress MissingConstructor */
class A {
    public int $prop;
}

/** @psalm-suppress MissingConstructor */
class B extends A {
    // Illegal: readwrite -> readonly
    public readonly int $prop;
}
Psalm output (using commit f122213):

No issues!
https://psalm.dev/r/11c464ac18
<?php

/** @psalm-suppress MissingConstructor */
class A {
    public readonly int $prop;
}

/** @psalm-suppress MissingConstructor */
class B extends A {
    // Illegal: readonly -> readwrite
    public int $prop;
}
Psalm output (using commit f122213):

No issues!
https://psalm.dev/r/d41b8e5778
<?php

class Foobar
{
    /** @var string */
    public readonly $foo;
    
    public function __construct()
    {
        $this->foo = "";
    }
}
Psalm output (using commit f122213):

No issues!

@cursor cursor Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Stale Bugbot comment from a previous run.

@github-actions

github-actions Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 798.7 108.5 $\color{gray}{\textsf{+0.7\%}}$ 25 767 52 296 806 0
✅ symfony 3 642.2 293.4 $\color{gray}{\textsf{-0.6\%}}$ 52 315 100 768 1 775 0
✅ doctrine-orm 339.3 25.0 $\color{gray}{\textsf{+1.9\%}}$ 3 348 5 766 0 0
✅ psl 624.3 33.0 $\color{gray}{\textsf{+0.9\%}}$ 1 296 5 125 2 0
✅ phpunit 435.0 21.0 $\color{gray}{\textsf{-1.0\%}}$ 1 575 2 363 18 0
✅ php-parser 229.1 13.9 $\color{gray}{\textsf{-0.1\%}}$ 17 032 21 091 0 0
✅ tempest 916.3 37.9 $\color{gray}{\textsf{-1.3\%}}$ 5 494 11 032 271 0
✅ amphp-amp 48.7 1.4 30 57 0 0
✅ webmozarts-assert 282.8 4.0 $\color{gray}{\textsf{-0.7\%}}$ 576 2 740 0 0
✅ guzzle-promises 47.3 1.5 $\color{gray}{\textsf{-1.3\%}}$ 148 403 2 0
✅ monolog 140.2 5.4 $\color{gray}{\textsf{+1.9\%}}$ 937 2 483 31 0
✅ drupal 5 091.1 228.7 $\color{gray}{\textsf{-0.7\%}}$ 97 797 168 839 686 0
✅ wordpress 2 035.1 202.3 $\color{gray}{\textsf{-1.1\%}}$ 143 514 678 442 491 1
Total 15 630.1 976.1 $\color{gray}{\textsf{-0.5\%}}$ 349 829 1 051 405 4 082 1
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 side by side on the same runner; ±10 % is noise. Above +10 % warns, never fails. re-run: a second round was taken and the faster run of each side 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. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • Profiling: when both sides support it, the measured runs use --debug-performance=PATH for the hot-files block, which adds the same small CPU and memory cost to both sides.
  • 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 ed33de6 · base b4f6909 (merge-base) · workflow run

jirkace and others added 3 commits November 29, 2026 20:34
…ck type

The redeclaration itself is now reported as OverriddenPropertyAccess first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ve type once vimeo#7767

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 29, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up (8501df9), after the Bugbot finding "Duplicate missing type readonly class": confirmed. In a readonly class a property with only a docblock type was reported twice (Properties of readonly classes must have a type from the scanner and the new Readonly property ... must have a type). The new report is now skipped there. testReadonlyPropertyWithoutANativeTypeIsReportedOnce asserts one MissingPropertyType each for a property of a readonly class, a documented readonly property and an undocumented one.

Edited existing test (b90b057): PropertyTypeInvarianceTest::nonReadonlyRedeclarationDoesNotInheritTheType redeclares a readonly parent property as non-readonly, which is the fatal error this PR reports; OverriddenPropertyAccess was added to its ignored issues, its NonInvariantDocblockPropertyType assertion is unchanged.

@orklah

orklah commented Nov 30, 2026

Copy link
Copy Markdown
Collaborator

bugbot review

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 2b78ea0. Configure here.

@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 this, both checks match real PHP fatal errors and the tests reproduce cleanly. One false positive though: PHP only rejects a readonly/non-readonly redeclaration when the parent property is not abstract, and since PHP 8.4 a readonly property may implement an abstract get-only hooked property. This now reports an OverriddenPropertyAccess on valid code:

<?php
abstract class AbstractHooked {
    abstract public int $x { get; }
}

final class ReadonlyImpl extends AbstractHooked {
    public function __construct(public readonly int $x) {}
}
// OverriddenPropertyAccess: Property ReadonlyImpl::$x cannot redeclare the non-readonly property AbstractHooked::$x as readonly

Could you skip the comparison when the parent property is abstract (Psalm doesn't record that yet, $stmt->isAbstract() in the scanner would do), and add this case to readonlyPropertyRedeclaredWhereItIsAllowed with php_version 8.4?

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.

2 participants