Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

False positive "Readonly property X is assigned outside of the constructor" #12537

Open
LastDragon-ru opened this issue Feb 7, 2025 · 2 comments · May be fixed by phpstan/phpstan-src#3817
Open
Labels
Milestone

Comments

@LastDragon-ru
Copy link

Bug report

Not sure why phpstan report error here :)

    private readonly WeakMap $storage;

    public function __construct() {
        $this->storage = new WeakMap(); 
    }

    public function set(stdClass $class, int $value): void {
        $this->storage[$class] = $value; // Readonly property Metadata::$storage is assigned outside of the constructor.
    }

Code snippet that reproduces the problem

https://phpstan.org/r/39b70b99-6927-44ef-b4df-3685d6c3dd6e

Expected output

No error.

Did PHPStan help you today? Did it make you happy in any way?

Yep 🤗

@ondrejmirtes
Copy link
Member

Funny - arrays cannot be modified this way, but ArrayAccess objects can :) https://3v4l.org/8lAND

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 7, 2025
@SerafimArts
Copy link

PHP objects are passed by pointer and readonly attribute applies the access modifier to the pointer itself, but the value of the object itself can be changed, since the object is not readonly (unless otherwise explicitly stated).

In contrast, arrays are passed by value (or more precisely, copy-on-write) and readonly is applied to the value itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants