承接 jiripudil/phpstan-sealed-classes 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

jiripudil/phpstan-sealed-classes

最新稳定版本:1.3.0

Composer 安装命令:

composer require --dev jiripudil/phpstan-sealed-classes

包简介

Sealed classes support for PHPStan.

README 文档

README

Important

This package is obsolete. You can now use the built-in @phpstan-sealed annotation introduced in PHPStan 2.1.18.

Sealed classes with PHPStan

Build Status latest version license monthly downloads downloads total

This extension adds support for sealed classes and interfaces to PHPStan.

Installation

To use this extension, require it via Composer

composer require --dev jiripudil/phpstan-sealed-classes

If you are using phpstan/extension-installer, this extension's configuration will be automatically enabled.

Otherwise, you need to include it explicitly in your phpstan.neon:

includes:
    - vendor/jiripudil/phpstan-sealed-classes/extension.neon

Usage

Sealed classes and interfaces allow developers to restrict class hierarchies: a sealed class can only be subclassed by classes that are explicitly permitted to do so. The same applies to sealed interfaces and their implementations. In a way, sealed classes are similar to enumerations, with an important distinction: while enums are singletons, a subclass of a sealed class can have multiple instances, each with its own state.

You can seal a class or an interface by attributing it as #[Sealed]. The attribute accepts a list of permitted descendants or implementations:

<?php

use JiriPudil\SealedClasses\Sealed;

#[Sealed(permits: [AllowedImplementation::class, AnotherImplementation::class])]
interface SealedInterface {}

class AllowedImplementation implements SealedInterface {}
class AnotherImplementation implements SealedInterface {}
class DisallowedImplementation implements SealedInterface {}

While the first two classes will be allowed, PHPStan will report an error for the third:

------ ----------------------------------------------------------------------------------
 Line   sealed-interface.php
------ ----------------------------------------------------------------------------------
 10     Type DisallowedImplementation is not allowed to be a subtype of SealedInterface.
------ ----------------------------------------------------------------------------------

Note that the restrictions do not apply to indirect subclasses. If a direct subclass of a sealed class is not sealed itself, it can be further extended without raising any errors. This code is perfectly fine:

<?php

use JiriPudil\SealedClasses\Sealed;

#[Sealed(permits: [AllowedDescendant::class])]
abstract class SealedClass {}

class AllowedDescendant extends SealedClass {}
class IndirectDescendant extends AllowedDescendant {}

统计信息

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

GitHub 信息

  • Stars: 34
  • Watchers: 4
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-19