开发工具代码质量静态分析【免费下载链接】phpstanPHP Static Analysis Tool - discover bugs in your code without running it!项目地址https://gitcode.com/gh_mirrors/ph/phpstan点击查看免费下载导读new.deprecatedEnum是 PHPStan 静态分析框架中由 phpstan-deprecation-rules 为骨架结合仓库中的标识符注册表与同族文档讲解该错误的触发条件、底层规则实现、与new.enum的关联以及正确的迁移修复方式帮助你理解并清理代码库中的废弃枚举使用。1. 错误标识符是什么PHPStan 自 1.x 起为每一条规则错误分配了稳定的错误标识符identifier。标识符由使用位置前缀 语义后缀组成例如new.deprecatedEnum中的new前缀表示该错误产生于new ClassName()实例化表达式。在 website/errors/CLAUDE.md 的标识符前缀参考表中new前缀对应的 PHP 语言特性被明确描述为前缀PHP 特性newnew ClassName()实例化因此new.deprecatedEnum的语义可以精确拆解为在new表达式中使用了一个已被废弃deprecated的枚举类型。它的shortDescription简短描述为Instantiation of a deprecated enum.实例化一个废弃的枚举。该标识符在文档 Frontmatter 中被标记为ignorable: true默认情况下允许通过ignoreErrors配置忽略此错误除非在规则链中显式调用-nonIgnorable()unlikely: true这是一个不太可能出现的错误原因见下文第 3 节。2. 触发示例完整的复现代码原文档给出的最小复现代码如下PHP 版本要求 8.1因为枚举是 PHP 8.1 引入的语言特性?php declare(strict_types 1); // lint 8.1 /** deprecated Use NewStatus instead */ enum OldStatus { case Active; } $x new OldStatus();逐行拆解这段代码/** deprecated Use NewStatus instead */通过 PHPDoc 的deprecated注解标记OldStatus枚举已废弃并在注解中指明推荐替代品为NewStatusenum OldStatus { case Active; }声明一个纯枚举pure enum包含一个枚举成员caseActive$x new OldStatus();对废弃枚举执行new实例化——这正是触发new.deprecatedEnum的代码模式。需要特别说明的是enum的关键字在前缀参考中同样属于new前缀的管辖范围而deprecatedEnum后缀则表明被实例化的对象类型是废弃枚举而非普通的废弃类new.deprecatedClass、废弃接口new.deprecatedInterface或废弃 traitnew.deprecatedTrait。3. 为什么会被报告规则实现与触发链路3.1 报告方phpstan-deprecation-rules 扩展原文档明确指出This error is reported by the phpstan-deprecation-rules extension.也就是说new.deprecatedEnum并不由 PHPStan 核心phpstan-src直接产生而是来自官方维护的废弃检测扩展phpstan/phpstan-deprecation-rules。该扩展专门负责检测代码中对已废弃 API 的使用包括废弃的类、方法、属性、常量、trait、接口以及枚举。在仓库的标识符注册表 website/src/errorsIdentifiers.json 中可以查到new.deprecatedEnum与规则类的映射关系errorsIdentifiers.jsonnew.deprecatedEnum: { PHPStan\\Rules\\Deprecations\\RestrictedDeprecatedClassNameUsageExtension: { phpstan/phpstan-deprecation-rules: [ .../RestrictedDeprecatedClassNameUsageExtension.php#L62 ] } }从注册表可以确认两点实现事实负责产出该标识符的规则类是PHPStan\Rules\Deprecations\RestrictedDeprecatedClassNameUsageExtension该规则位于phpstan/phpstan-deprecation-rules包的src/Rules/Deprecations/目录下是一个受限废弃类名使用Restricted Deprecated Class Name Usage扩展同时负责new.deprecatedClass、new.deprecatedInterface、new.deprecatedTrait、new.deprecatedEnum等一组废弃类型被实例化的错误见 errorsIdentifiers.json 中同目录规则类的连续注册。3.2 语言层面的根本原因报告该错误的底层语义是废弃的枚举类型被计划在未来的某个版本中移除。一旦移除new OldStatus()这样的代码将彻底失效因此在迁移窗口期内静态分析器会提醒开发者停止使用它。原文档对此的表述为A deprecated enum is used in anewexpression. Deprecated enums are planned for removal in a future version.3.3 关键细节unlikely标识符与new.enum的伴生关系这是理解new.deprecatedEnum最重要的一点。原文档专门用一段 Note 说明Note: triggering this identifier requires instantiating an enum, which PHP does not allow. PHPStan therefore always also reports anew.enumerror, and in practice the deprecation identifier is not reported alongside it.翻译并展开如下触发该标识符的前提是对枚举执行new而 PHP 语言本身禁止实例化枚举因此 PHPStan 在分析new OldStatus()时必然同时报告new.enum错误Enums cannot be instantiated with the new keyword即枚举不能用new关键字实例化由于new.enum先于废弃检测被报告、且该代码在运行时必然抛出致命错误在实际输出中new.deprecatedEnum通常不会与new.enum同时出现——这就是 Frontmatter 中unlikely: true标记的由来。对照同目录的 website/errors/new.enum.md 可以看到其短描述与成因Enums cannot be instantiated with the new keyword.枚举不能用new关键字实例化。Enums in PHP cannot be instantiated using thenewkeyword. Enum cases are predefined singleton instances and must be accessed directly via their case names (e.g.,Suit::Hearts). Attempting to usenewon an enum will result in a fatal error at runtime.即枚举成员是语言预定义的单例实例只能通过EnumName::CaseName的形式直接访问任何new Enum()都会在运行时产生致命错误fatal error。4. 如何修复从实例化改为直接引用枚举成员由于枚举不可实例化new.deprecatedEnum的正确修复方式与new.deprecatedClass替换为另一个可实例化的类完全不同。原文档给出的修复方案是不要实例化枚举而是直接使用枚举成员case-$x new OldStatus(); $x NewStatus::Active;如果迁移目标是保留原枚举类型语义也可以把修复改为引用OldStatus自身的成员但OldStatus已废弃推荐还是切换到NewStatus-$x new OldStatus(); $x OldStatus::Active;修复背后的原理结合 new.enum.md 的说明枚举的每个case都是一个预先存在的、唯一的单例对象访问方式固定为EnumName::CaseName例如Suit::Hearts、NewStatus::Active直接引用枚举成员既不会触发运行时致命错误也不会再命中废弃类型检测。在实际的废弃迁移中更完整的修复还包含以下配套动作同步更新类型声明任何使用OldStatus作为参数类型、返回类型或属性类型的地方都应一并迁移为NewStatus避免新旧类型混用传递废弃状态如果调用方自身也处于废弃迁移链中例如某个内部过渡函数仍然需要暂时构造旧的枚举引用可以参照new.deprecatedClass文档的处理方式——为调用方代码加上deprecated注解。此时 PHPStan 的废弃传播机制不会在已废弃的代码内部使用废弃 API时报错因为这类内部使用属于迁移过程的一部分参见 website/errors/new.deprecatedClass.md 中的示例在createLogger()函数上添加/** deprecated */后函数内部new OldLogger()不再被报告。5. 如何启用与配置该检测new.deprecatedEnum由phpstan/phpstan-deprecation-rules扩展提供因此需要先安装该扩展才能获得废弃检测能力。5.1 安装扩展在项目根目录执行 Composer 安装命令composer require --dev phpstan/phpstan-deprecation-rules该扩展会注册PHPStan\Rules\Deprecations\命名空间下的一系列废弃使用规则包括负责new.deprecatedEnum的RestrictedDeprecatedClassNameUsageExtension。5.2 在 PHPStan 配置中启用规则在项目的phpstan.neon或phpstan.neon.dist配置文件中引入扩展的规则集includes: - vendor/phpstan/phpstan-deprecation-rules/rules.neon启用后PHPStan 分析时会自动对代码中所有使用了deprecated标记的类、枚举、接口、trait、方法、属性、常量等场景产生对应的*.deprecated*系列标识符错误。5.3 忽略策略由于该标识符默认ignorable: true在迁移尚未完成的过渡期可以通过ignoreErrors按标识符精确忽略特定位置parameters: ignoreErrors: - identifier: new.deprecatedEnum path: legacy/DeprecatedUsage.php说明按 website/errors/CLAUDE.md 的约定标识符详情页本身不鼓励以忽略错误作为首选修复方式忽略仅适用于明确的过渡期豁免场景正常情况下应优先按第 4 节完成代码迁移。6. 延伸deprecatedEnum标识符家族new.deprecatedEnum只是 PHPStan 废弃枚举检测体系中的一个成员。在 website/errors 目录下以deprecatedEnum为后缀的标识符文档覆盖了废弃枚举在各类使用位置上的检测标识符触发位置new.deprecatedEnumnew实例化表达式instanceof.deprecatedEnum$x instanceof EnumName表达式catch.deprecatedEnumcatch (EnumName $e)捕获块method.deprecatedEnum原生方法类型声明staticMethod.deprecatedEnumClassName::method()静态方法调用property.deprecatedEnum原生属性类型声明如private Foo $barstaticProperty.deprecatedEnumClassName::$property静态属性访问classConstant.deprecatedEnumClassName::CONSTANT常量访问attribute.deprecatedEnumPHP 8.0 属性#[AttributeName]parameter.deprecatedEnum函数/方法参数的原生类型声明return.deprecatedEnum原生返回类型声明varTag.deprecatedEnumvarPHPDoc 标签propertyTag.deprecatedEnumpropertyPHPDoc 标签methodTag.deprecatedEnummethodPHPDoc 标签assert.deprecatedEnumphpstan-assertPHPDoc 标签mixin.deprecatedEnummixinPHPDoc 标签traitUse.deprecatedEnum类体中的use TraitNameclass.extendsDeprecatedEnum/enum.implementsDeprecatedEnum等类继承/枚举实现声明这些标识符的命名遵循 website/errors/CLAUDE.md 中记录的前缀规则且大多数由ClassNameUsageLocation推导而来。理解new前缀在其中的定位有助于你在 PHPStan 输出中快速定位问题代码的语法位置。7. 小结new.deprecatedEnum由phpstan-deprecation-rules扩展报告对应规则类为PHPStan\Rules\Deprecations\RestrictedDeprecatedClassNameUsageExtension见 errorsIdentifiers.json触发模式是对标注了deprecated的枚举执行new而枚举在 PHP 中本就被禁止实例化因此该标识符标记为unlikely实际分析中会伴随new.enum一并出现new.enum.md正确的修复不是换一个可实例化的类而是改用枚举成员直接引用EnumName::CaseName并同步迁移相关的类型声明如需在过渡期豁免可依据ignorable: true通过ignoreErrors按标识符精确忽略但首选方案仍是完成废弃 API 的迁移。通过本文你应当能够在 PHPStan 报告中识别new.deprecatedEnum及同族标识符的含义、定位到对应源码规则phpstan-deprecation-rules的src/Rules/Deprecations/目录、并给出符合 PHP 8.1 枚举语义的正确修复代码。赞分享开发工具代码质量静态分析【免费下载链接】phpstanPHP Static Analysis Tool - discover bugs in your code without running it!项目地址https://gitcode.com/gh_mirrors/ph/phpstan点击查看免费下载相关推荐PHPStan 错误标识符解析assert.deprecatedEnum —— phpstan-assert 引用已废弃枚举的检测与修复PHPStan 错误标识符解析 assert.deprecatedEnum —— phpstan assert 引用已废弃枚举的检测与修复 导读 asser开发工具代码质量静态分析PHPStan 错误标识符 generics.deprecatedEnumBound 全解析template 约束引用已废弃枚举的检测与修复PHPStan 错误标识符 generics.deprecatedEnumBound 全解析 template 约束引用已废弃枚举的检测与修复 本指南围绕开发工具代码质量静态分析PHPStan 错误标识符深度解析catch.deprecatedEnum——catch 子句引用已废弃枚举的检测与修复PHPStan 错误标识符深度解析catch.deprecatedEnum——catch 子句引用已废弃枚举的检测与修复 本篇技术指南围绕 PHPStan 官开发工具代码质量静态分析创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
