ergebnis/phpstan-rules 版本演进全解析:从 0.1.0 到 2.14.0 的 PHPStan 扩展规则集 开发工具【免费下载链接】php-file-iteratorFilterIterator implementation that filters files based on a list of suffixes, prefixes, and other exclusion criteria.项目地址https://gitcode.com/gh_mirrors/ph/php-file-iterator点击查看免费下载本篇以ergebnis/phpstan-rules的完整变更记录CHANGELOG.md为核心骨架结合其rules.neon参数定义与源码实现系统梳理这套 PHPStan 扩展规则集从 0.1.0 到 2.14.0 的全部演进脉络每一条规则何时引入、哪些参数可供配置、PHP 版本与 phpstan/phpstan 依赖如何变迁以及 0.14.0 那次从Localheinz\PHPStan迁移到Ergebnis\PHPStan的 breaking change 该如何平滑升级。读完你将掌握该扩展的完整规则清单、配置语法ergebnis参数段、错误标识符体系并能在自己的phpstan.neon中精准启停这些规则。作为实例仓库根目录的 phpstan.neon 与 tools/.phpstan/composer.json 展示了它在 php-file-iterator 项目中的真实落地方式。一、这份变更记录在讲什么ergebnis/phpstan-rules是一套面向 PHPStan 的规则扩展包type: phpstan-extension通过composer.json的extra.phpstan.includes自动加载rules.neon为本就强大的 PHPStan 追加一批编码风格与设计约束层面的规则。它要求phpstan/phpstan:^2.1.35支持 PHP 7.4 ~ 8.6 多个大版本详见 composer.lock 中该包依赖声明。本仓库phpunit/php-file-iterator将其作为require-dev依赖锁定在2.14.0见 tools/.phpstan/composer.json 与 composer.lock并搭配phpstan/phpstan-strict-rules与phpstan/extension-installer使用PHPStan 分析入口为 tools/phpstan。变更记录本身遵循两份业界规范Keep a Changelog按Unreleased 版本号分节每节内部再按Added新增、Changed变更、Fixed修复、Removed移除分类Semantic Versioning语义化版本0.x为预发布阶段1.0.0起承诺向后兼容2.0.0携带破坏性变更。每个版本条目都附有与前一个版本的 diff 链接方便审计具体改动。二、规则全景从 0.1.0 到 2.14.0 的演进时间线整套规则的演进可以归纳为五条主题线下面按主题重组变更记录并保留版本与引入顺序。1. 类与继承约束Classes / Methods版本规则/变更说明0.1.0Classes\AbstractOrFinalRule非匿名类既非abstract也非final时报错0.2.0Classes\FinalRule非匿名类必须final构造函数增加$excludeClassNames白名单参数0.5.0参数allowAbstractClasses、classesNotRequiredToBeAbstractOrFinal允许配置FinalRuleAbstractOrFinalRule行为并入FinalRule后被移除且构造函数默认值被删除配置必须显式给出0.7.0Classes\NoExtendsRule继承未允许的类时报错0.7.1 起内置一组默认允许被继承的类0.9.0 / 1.0.0FinalRule忽略 Doctrine 实体先忽略注解式实体ORM\Mapping\Entity1.0.0 起也忽略带Doctrine\ORM\Mapping\Entity属性的实体0.15.0Classes\PHPUnit\Framework\TestCaseWithSuffixRule继承PHPUnit\Framework\TestCase的具体类必须以Test结尾0.13.0Methods\PrivateInFinalClassRulefinal类中的protected方法若可降级为private则报错2.5.1 起忽略匿名类、trait 中继承来的protected方法以及被 PHPUnit 属性/注解要求为protected的方法0.12.0Methods\FinalInAbstractClassRuleabstract类中具体的public/protected方法必须final0.15.1 起允许抽象类中存在非final的public构造函数2.10.0Methods\InvokeParentHookMethodRule覆写父类 hook 方法时若未按预期顺序调用父类实现则报错行为通过hookMethods参数定义2. 参数与类型声明约束Closures / Functions / Methods0.3.0 批量引入Functions\NoNullableReturnTypeDeclarationRule、Methods\NoNullableReturnTypeDeclarationRule、Closures\NoParameterWithNullDefaultValueRule、Closures\NoNullableReturnTypeDeclarationRule、Functions\NoParameterWithNullDefaultValueRule、Methods\NoParameterWithNullDefaultValueRule、Closures\NoParameterWithNullableTypeDeclarationRule、Functions\NoParameterWithNullableTypeDeclarationRule、Methods\NoParameterWithNullableTypeDeclarationRule。0.5.0新增Methods\NoConstructorParameterWithDefaultValueRule禁止类与匿名类的构造函数参数带默认值。0.12.0新增Methods\NoParameterWithContainerTypeDeclarationRule禁止方法参数类型声明为已知的依赖注入容器或服务定位器默认Psr\Container\ContainerInterface可通过interfacesImplementedByContainers扩展。2.0.0新增methodsAllowedToUseContainerTypeDeclarations参数白名单内的方法名允许使用容器类型声明。2.7.0新增Closures\NoParameterPassedByReferenceRule、Functions\NoParameterPassedByReferenceRule、Methods\NoParameterPassedByReferenceRule禁止参数按引用传递以及Functions\NoReturnByReferenceRule、Methods\NoReturnByReferenceRule禁止按引用返回。2.5.2上述 6 条 nullable 相关规则补强——能识别null以错误大小写如Null或相对于根命名空间引用的情况。3. 表达式与语句约束Expressions / Statements / CallLikes0.11.0 批量引入Expressions\NoEmptyRuleempty()、Expressions\NoEvalRuleeval()、Expressions\NoErrorSuppressionRule错误抑制、Expressions\NoCompactRulecompact()、Statements\NoSwitchRuleswitch。同时要求nikic/php-parser:^4.2.3、phpstan/phpstan:~0.11.15。0.8.0Expressions\NoIssetRuleisset()0.8.1 修复了该规则未被真正启用的问题。2.8.0新增Expressions\NoAssignByReferenceRule禁止变量按引用赋值。2.9.0新增CallLikes\NoNamedArgumentRule禁止以命名参数named argument调用匿名函数、函数或方法。2.0.0Expressions\NoEmptyRule被移除breaking changeempty()不再由本扩展约束。2.10.4 / 2.10.5NoNamedArgumentRule持续修复——支持变量表达式上的静态调用、invokable 调用、属性上的 callable、变量类名构造函数对无法解析的调用退化为通用错误消息仅对已知调用给出精确描述。4. 文件级约束Files0.11.0Files\DeclareStrictTypesRulePHP 文件缺少declare(strict_types1)时报错0.14.3 起忽略首行为 shebang 的情况。2.13.0Files\NoPhpstanIgnoreRule发现phpstan-ignore、phpstan-ignore-line、phpstan-ignore-next-line标签时报错强制修掉错误或使用 baseline而不是就地忽略。5. 错误消息与标识符演进0.4.0移除错误消息中的双引号、统一方法/函数/参数名的措辞与位置使输出风格与 phpstan/phstan 自身错误消息一致。2.5.0为每条规则引入机器可读的错误标识符error identifier2.5.1 起规则通过 builder 正式返回带标识符的错误2.6.1 修复NoParameterWithNullableTypeDeclarationRule使用了不正确的标识符2.10.2 为InvokeParentHookMethodRule重命名了标识符。三、三个关键里程碑的深度解读1. 0.14.0命名空间迁移与参数段重构breaking change这是 0.x 阶段最重要的破坏性版本包含两项必须手工迁移的变更其一vendor 命名空间从Localheinz更名为Ergebnis。官方给出的三步升级命令composer remove localheinz/phpstan-rules composer require ergebnis/phpstan-rules再替换源码与配置中出现过的Localheinz\PHPStan前缀find . -type f -exec sed -i .bak s/Localheinz\\PHPStan/Ergebnis\\PHPStan/g {} \;最后清理备份文件find -type f -name *.bak -delete其二参数从全局parameters段移入ergebnis段。此前phpstan.neon的写法是parameters: allowAbstractClasses: true classesAllowedToBeExtended: [] classesNotRequiredToBeAbstractOrFinal: [] interfacesImplementedByContainers: - Psr\Container\ContainerInterface0.14.0 之后这些参数必须全部嵌套到ergebnis之下防止与 PHPStan 全局参数命名冲突parameters: - allowAbstractClasses: true - classesAllowedToBeExtended: [] - classesNotRequiredToBeAbstractOrFinal: [] - interfacesImplementedByContainers: - - Psr\Container\ContainerInterface ergebnis: allowAbstractClasses: true classesAllowedToBeExtended: [] classesNotRequiredToBeAbstractOrFinal: [] interfacesImplementedByContainers: - Psr\Container\ContainerInterface同版本还修复了composer.json中不当的replace配置0.14.1、恢复了 PHP 7.1 支持0.14.2、并借 0.10.0 的parametersSchema保证了与bleedingEdge.neon的兼容性。2. 1.0.0正式稳定1.0.0 起支持phpstan/phpstan:^1.0.0并放弃非稳定版本同时FinalRule不再对带Doctrine\ORM\Mapping\Entity属性的非 final 类报错。此后0.x阶段结束进入语义化版本承诺期。3. 2.0.0面向 PHPStan 2.x 的大版本2.0.0 是一次集中的破坏性变更移除 PHP 7.2 / 7.3 / 7.4 支持新增 PHP 8.2 支持新增methodsAllowedToUseContainerTypeDeclarations参数允许使用容器类型声明的方法名列表新增禁用任意规则的能力配合后文enabled: false使用支持 nullable union types移除Expressions\NoEmptyRule。此后的 2.x 演进主线是扩平台 加规则2.1.0 丢弃 PHP 8.0、支持 PHP 8.32.2.0 允许nikic/php-parser:^5.0.02.3.0/2.4.0 支持 PHP 8.42.5.0 支持 PHP 7.4 与 8.0 并移除对nikic/php-parser的依赖2.6.0 支持phpstan/phpstan:^2.0.02.11.0/2.12.0 支持 PHP 8.52.14.0 允许安装于 PHP 8.6。依赖上2.9.0 要求phpstan/phpstan:^2.1.82.13.0 提高到^2.1.35。四、当前规则清单与完整配置参数依据 rules.neon包内 rules.neon 是配置中枢conditionalTags依据各规则的enabled参数决定是否注册为 PHPStan 规则parameters给出全部默认值parametersSchema定义参数结构校验services完成依赖注入。当前 2.14.0 共注册 25 条规则全部默认启用顶层ergebnis.allRules: true规则类规则关键参数默认值CallLikes\NoNamedArgumentRule禁止命名参数调用enabledClasses\FinalRule类必须 finalenabled、allowAbstractClassestrue、classesNotRequiredToBeAbstractOrFinal[]Classes\NoExtendsRule禁止继承未许可类enabled、classesAllowedToBeExtended[]Classes\PHPUnit\Framework\TestCaseWithSuffixRule测试类须以 Test 结尾enabledClosures\NoNullableReturnTypeDeclarationRule闭包禁止可空返回类型enabledClosures\NoParameterPassedByReferenceRule闭包禁止按引用传参enabledClosures\NoParameterWithNullableTypeDeclarationRule闭包禁止可空参数类型enabledExpressions\NoAssignByReferenceRule禁止按引用赋值enabledExpressions\NoCompactRule禁止compact()enabledExpressions\NoErrorSuppressionRule禁止抑制错误enabledExpressions\NoEvalRule禁止eval()enabledExpressions\NoIssetRule禁止isset()enabledFiles\DeclareStrictTypesRule必须declare(strict_types1)enabledFiles\NoPhpstanIgnoreRule禁止phpstan-ignore*标签enabledFunctions\NoNullableReturnTypeDeclarationRule函数禁止可空返回类型enabledFunctions\NoParameterPassedByReferenceRule函数禁止按引用传参enabledFunctions\NoParameterWithNullableTypeDeclarationRule函数禁止可空参数类型enabledFunctions\NoParameterWithNullDefaultValueRule函数参数禁止null默认值enabledFunctions\NoReturnByReferenceRule禁止按引用返回enabledMethods\FinalInAbstractClassRule抽象类具体方法须 finalenabledMethods\InvokeParentHookMethodRulehook 方法须按序调用父类实现enabled、hookMethods[]Methods\NoConstructorParameterWithDefaultValueRule构造参数禁止默认值enabledMethods\NoNullableReturnTypeDeclarationRule方法禁止可空返回类型enabledMethods\NoParameterPassedByReferenceRule方法禁止按引用传参enabledMethods\NoParameterWithContainerTypeDeclarationRule方法禁止容器类型声明enabled、interfacesImplementedByContainers[Psr\Container\ContainerInterface]、methodsAllowedToUseContainerTypeDeclarations[]Methods\NoParameterWithNullableTypeDeclarationRule方法禁止可空参数类型enabledMethods\NoParameterWithNullDefaultValueRule方法参数禁止null默认值enabledMethods\NoReturnByReferenceRule方法禁止按引用返回enabledMethods\PrivateInFinalClassRulefinal 类中 protected 应降为 privateenabledStatements\NoSwitchRule禁止switchenabled其中Closures\NoParameterWithNullDefaultValueRule与Expressions\NoEmptyRule已不在当前清单中前者未出现在rules.neon的 conditionalTags 内后者在 2.0.0 被移除。invokeParentHookMethod.hookMethods的 schema 定义较为独特每个元素包含四个字段依据parametersSchemaclassNamestringhook 方法所属父类名methodNamestringhook 方法名hasContentno或yes父类实现是否有内容invocationany、first或last子类应在何时调用父类实现。配置示例按需启停规则启用全部规则即可省略ergebnis段默认allRules: true只启用部分规则则把allRules设为false并逐个声明parameters: ergebnis: allRules: false final: enabled: true allowAbstractClasses: false classesNotRequiredToBeAbstractOrFinal: - PHPUnit\Framework\TestCase privateInFinalClass: enabled: true noExtends: enabled: true classesAllowedToBeExtended: - Symfony\Component\EventDispatcher\EventSubscriberInterface五、错误标识符体系自 2.5.0 引入、2.5.1 起随错误正式返回的错误标识符error identifier统一形如ergebnis.ruleName。定义集中在 src/ErrorIdentifier.phpfinal()→ergebnis.final、noPhpstanIgnore()→ergebnis.noPhpstanIgnore、privateInFinalClass()→ergebnis.privateInFinalClass、noNamedArgument()→ergebnis.noNamedArgument等toString()方法负责拼接前缀见 src/ErrorIdentifier.php#L138-L144。标识符的价值在于它允许在 PHPStan 的reportUnmatchedIgnoredErrors场景下精确匹配/忽略特定规则、在 CI 中按标识符聚合统计错误也让phpstan-ignore ergebnis.noCompact这类带标识符的定向忽略成为可能——当然NoPhpstanIgnoreRule会禁止你这么做详见下一节。六、源码剖析与在本仓库中的实际落地NoPhpstanIgnoreRule 的实现方式src/Files/NoPhpstanIgnoreRule.php 是 2.13.0 新增规则的代表性实现它实现了PHPStan\Rules\RuleFileNodegetNodeType()返回FileNode::class见 L29-L32用PhpParser\NodeFinder遍历文件全部节点收集每个节点的注释并按行匹配正则phpstan-ignore(-line|-next-line)?见 L49一旦命中就报出提示应修复错误或改用 baseline而不是用phpstan-ignore就地忽略消息文本见 L55-L58并通过ErrorIdentifier::noPhpstanIgnore()携带标识符、nonIgnorable()标记为不可忽略。2.13.1 修复了它遍历节点不全的问题2.14.0 又修正了Functions\NoParameterPassedByReferenceRule的错误标识符。php-file-iterator 中的真实配置仓库根目录的 phpstan.neon 是这套扩展在真实项目中的配置范本parameters: level: 10 paths: - src - tests/unit strictRules: allRules: false booleansInConditions: true closureUsesThis: true disallowedBacktick: true disallowedEmpty: true disallowedImplicitArrayCreation: true disallowedLooseComparison: true disallowedShortTernary: true illegalConstructorMethodCall: true matchingInheritedMethodNames: true noVariableVariables: true numericOperandsInArithmeticOperators: true overwriteVariablesWithLoop: true requireParentConstructorCall: true strictFunctionCalls: true switchConditionsMatchingType: true uselessCast: true ergebnis: allRules: false final: enabled: true privateInFinalClass: enabled: true includes: - phar://phpstan.phar/conf/bleedingEdge.neon可见该项目的做法是分析等级拉到level: 10并叠加bleedingEdge.neon这正是 0.10.0 补上parametersSchema后得以兼容的场景同时并行使用phpstan-strict-rules与ergebnis/phpstan-rules两套扩展对ergebnis只显式启用final与privateInFinalClass两条最符合该库类设计收敛诉求的规则其余规则保持关闭allRules: false。依赖侧tools/.phpstan/composer.json 声明ergebnis/phpstan-rules: ^2.14.0、phpstan/phpstan: ^2.2.17、phpstan/extension-installer: ^1.4.3后者自动激活rules.neon的 include并由 composer.lock 锁定到 2.14.0。七、升级与维护建议版本策略以 SemVer 为准。2.x内部的小版本升级通常安全新增规则默认启用可能带来新报错建议升级后先跑一遍全量分析3.0.0如出现需重点核对规则移除与参数结构变更。0.14.0 之前的老用户参照第三节的迁移命令完成命名空间替换与参数移入ergebnis段两步操作。新增规则默认全开allRules: true意味着每次升级都可能引入新的约束如 2.13.0 的NoPhpstanIgnoreRule会立刻对既有phpstan-ignore标签报错。若担心波动可先设allRules: false再逐个enabled: true灰度。与 strict-rules 的取舍ergebnis/phpstan-rules偏设计约束phpstan/phpstan-strict-rules偏严格语义两者互补php-file-iterator 的 phpstan.neon 就是两者同开的参考配置。八、结语从 0.1.0 的单一AbstractOrFinalRule到 2.14.0 的 25 条规则、错误标识符体系、allRules总开关与 PHP 8.6 支持ergebnis/phpstan-rules的变更记录完整展现了类设计收敛 → 类型声明纪律 → 表达式/语句禁令 → 文件级规范这条规则谱系。对使用者而言最有价值的不是背诵每条规则而是掌握 rules.neon 这一配置中枢用allRules控制总体范围、用各规则的enabled与专项参数微调边界、用错误标识符对接 CI 与 IDE。本文的版本对照表与配置示例可直接复制到你的phpstan.neon中按需裁剪。赞分享开发工具【免费下载链接】php-file-iteratorFilterIterator implementation that filters files based on a list of suffixes, prefixes, and other exclusion criteria.项目地址https://gitcode.com/gh_mirrors/ph/php-file-iterator点击查看免费下载相关推荐从 0.1.0 到 2.14.0ergebnis/phpstan-rules 演进全解析与 PHPStan 规则扩展实战指南从 0.1.0 到 2.14.0ergebnis/phpstan rules 演进全解析与 PHPStan 规则扩展实战指南 本文以 ergebnis/php后端ergebnis/phpstan-rules 全版本解读从 0.1.0 到 2.14.0 的规则演进与 PHPStan 配置实战ergebnis/phpstan rules 全版本解读从 0.1.0 到 2.14.0 的规则演进与 PHPStan 配置实战 导读 本文基于 php co测试代码质量ergebnis/phpstan-rules 演进全解从 0.1.0 到 2.14.0 的 PHPStan 规则库变更史与迁移指南ergebnis/phpstan rules 演进全解从 0.1.0 到 2.14.0 的 PHPStan 规则库变更史与迁移指南 这篇技术指南以 ergeb开发工具测试创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考