icanhazstring/phpstan-readonly-property 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

icanhazstring/phpstan-readonly-property

最新稳定版本:0.2.0

Composer 安装命令:

composer require icanhazstring/phpstan-readonly-property

包简介

Support #[IsReadonly] promoted constructor properties for PHPStan

README 文档

README

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.

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 #[IsReadonly] to the property you want to have readonly only.

<?php

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
{
    #[IsReadonly]
    public string $value;

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

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

统计信息

  • 总下载量: 14.7k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-07-14