This package is abandoned and no longer maintained. No replacement package was suggested.

Support #[IsReadonly] promoted constructor properties for PHPStan

0.2.0 2021-11-16 10:49 UTC

This package is auto-updated.

Last update: 2023-05-11 14:00:07 UTC


Support #[IsReadonly] class properties for PHPStan. This library is used to have a transition from PHP 8.0 to 8.1 until readonly keyword will be introduced.


composer require --dev icanhazstring/phpstan-readonly-property

Then use PHPStan Extension Installer using

composer require --dev phpstan/extension-installer

or manually add vendor/icanhazstring/phpstan-readonly-property/rules.neon into your phpstan.neon configuration.

# phpstan.neon

    - vendor/icanhazstring/phpstan-readonly-property/rules.neon


Add #[IsReadonly] to the property you want to have readonly only.


final class User
    public function __constrct(
        #[IsReadonly] public string $name
    ) {}

$user = new User('fu');
$user->name = 'bar'; // Will fail

Known limitations

There are some limitations to the static analysis from using 8.1 readonly flag.

Use of multiple setters can't be checked

If you are initializing your #[IsReadonly] property using a setter, PHPStan can NOT detect multiple calls of that setter to a readonly property.

final class Fu
    public string $value;

    public function setValue(string $value)
        $this->value = $value;

$fu = new Fu();
$fu->setValue('baz'); // Will work with this extension, but NOT with 8.1 `readonly`