PHPStan extension to support #[Readonly] constructor properties

Overview

icanhazstring/phpstan-readonly-property

Support #[Readonly] promoted constructor properties for PHPStan. This library is used to have a full transition from PHP8.0 to 8.1 until readonly keyword will be introduced.

Installation

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

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

Usage

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

<?php

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

$user = new User('fu');
$user->name = 'bar'; // Will fail
You might also like...
Sandbox project for the PHPStan workshop

Sandbox project for a PHPStan workshop Installation Requirements Docker Engine Docker Compose Git Bash Getting started Clone this repository (git clon

A PHPStan package that supports working with Extbase

PHPStan for Extbase This package provides a couple of stubs and services to make your life easier when working with PHPStan and Extbase. Examples clas

Custom PHPStan rules

phpstan-rules Provides additional rules for phpstan/phpstan. Installation Run $ composer require --dev alister/phpstan-rules Usage All of the rules pr

Doctrine extensions for PHPStan

Doctrine extensions for PHPStan PHPStan Doctrine This extension provides following features: DQL validation for parse errors, unknown entity classes a

Various PHPStan rules we found useful in ShipMonk.

ShipMonk PHPStan rules Various rules we found useful in ShipMonk. You may found some of them opinionated, so we recommend picking only those fitting y

Repository containing all the PHPStan rules from the book
Repository containing all the PHPStan rules from the book "Recipes for Decoupling"

PHPStan rules from the book "Recipes for Decoupling" by Matthias Noback In the book "Recipes for Decoupling" we discuss how to decouple from web and C

WordPress extensions for PHPStan ⛏️

WordPress extensions for PHPStan Static analysis for the WordPress ecosystem. PHPStan WordPress Installation Add this package to your project. compose

Perform static analysis of Drupal PHP code with phpstan-drupal.

Perform static analysis of Drupal PHP code with PHPStan and PHPStan-Drupal on Drupal using PHP 8. For example: docker run --rm \ -v $(pwd)/example01

Magento 2 Extension to cleanup admin menu and Store > Configuration area by arranging third party extension items.
Magento 2 Extension to cleanup admin menu and Store Configuration area by arranging third party extension items.

Clean Admin Menu - Magento 2 Extension It will merge all 3rd party extension's menu items in backend's primary menu to a common menu item named "Exten

Comments
  • Missing way to require Attribute while keeping this in dev dependencies

    Missing way to require Attribute while keeping this in dev dependencies

    The issue I've found is that this phpstan extension should be required as a dev dependency but the Attribute should be exposed as non-dev code because one must import it before using.

    use Icanhazstring\PhpstanReadonlyPropertyExtension\Attribute\Readonly;
    
    opened by simPod 5
  • Not compatible with PHP 8.1

    Not compatible with PHP 8.1

    readonly is a token in php 8.1 so having attributes with same name cause parse error there.

    WDYT about renaming the attribute to IsReadonly and tagging v0.2?

    opened by simPod 3
  • Internal error: Property

    Internal error: Property "data" does not exist in file x.php

    I'm getting

    Internal error: Property "data" does not exist in file x.php

    error on similar code:

    class A {
        public function foo($val) {
            $x = new stdClass();
            $x->data = $val;
       }
    }
    

    basically when I'm assigning a value directly to any property. The extension seems not to distinguish between class own members and members of another instance.

    opened by simPod 3
Releases(0.2.0)
  • 0.2.0(Nov 16, 2021)

    What's Changed

    • Rename Readonly to IsReadonly in order to allow BC by @simPod in https://github.com/icanhazstring/phpstan-readonly-property/pull/7

    Full Changelog: https://github.com/icanhazstring/phpstan-readonly-property/compare/0.1.5...0.2.0

    Source code(tar.gz)
    Source code(zip)
  • 0.1.5(Nov 2, 2021)

    What's Changed

    • Support phpstan v1 by @simPod in https://github.com/icanhazstring/phpstan-readonly-property/pull/5

    Full Changelog: https://github.com/icanhazstring/phpstan-readonly-property/compare/0.1.4...0.1.5

    Source code(tar.gz)
    Source code(zip)
  • 0.1.4(Jul 31, 2021)

  • 0.1.3(Jul 28, 2021)

    Added

    • Added the possibility to not only write Readonly properties from the constructor
    • Added a check that prevents a declaring class from having two write access lines to a Readonly property
    Source code(tar.gz)
    Source code(zip)
  • 0.1.2(Jul 19, 2021)

  • 0.1.1(Jul 17, 2021)

Owner
Andreas Frömer
Proud daddy, Gamer and Lead Software Developer
Andreas Frömer
The main scope of this extension is to help phpstan to detect the type of object after the Assert\Assertion validation.

PHPStan beberlei/assert extension PHPStan beberlei/assert Description The main scope of this extension is to help phpstan to detect the type of object

PHPStan 33 Jan 2, 2023
PHPStan extension for webmozart/assert

PHPStan webmozart/assert extension PHPStan webmozart/assert Description The main scope of this extension is to help phpstan to detect the type of obje

PHPStan 139 Dec 22, 2022
An extension for PHPStan for adding analysis for PHP Language Extensions.

PHPStan PHP Language Extensions (currently in BETA) This is an extension for PHPStan for adding analysis for PHP Language Extensions. Language feature

Dave Liddament 9 Nov 30, 2022
PHPStan extension for sealed classes and interfaces.

Sealed classes with PHPStan This extension adds support for sealed classes and interfaces to PHPStan. Installation To use this extension, require it v

Jiří Pudil 14 Nov 28, 2022
Extension for PHPStan to allow analysis of Drupal code.

phpstan-drupal Extension for PHPStan to allow analysis of Drupal code. Sponsors Would you like to sponsor? Usage When you are using phpstan/extension-

Matt Glaman 154 Jan 2, 2023
A small package to add computed properties to any PHP class. 🐘

Computed Properties This package provides a trait and attribute that can provide computed property support. Installation This package can be installed

Ryan Chandler 22 Mar 28, 2022
Validated properties in PHP8.1 and above using attribute rules

PHP Validated Properties Add Rule attributes to your model properties to make sure they are valid. Why this package? When validating external data com

null 23 Oct 18, 2022
Joole Reflector - used to work with the properties of objects, their changes and merges

Joole Reflector allows you to change protected, as well as private properties of an object.

Ravil Sitdikov 1 May 7, 2022
[READ-ONLY] Properties define model metadata.

Charcoal Property Properties define object's metadata. They provide the building blocks of the Model's definition. Properties are defined globally for

The Charcoal PHP Framework 0 Jun 21, 2022
Analyzes PHPStan baseline files and creates aggregated error trend-reports

Analyzes phpstan baseline files Analyzes PHPStan baseline files and creates aggregated error trend-reports.

Markus Staab 22 Dec 23, 2022