art4/rector-bc-library
Composer 安装命令:
composer require art4/rector-bc-library
包简介
Reusable Rector rule set for BC-related transformations
README 文档
README
Backward-compatible Rector rules for library maintainers.
| Rector Version | Status | Details |
|---|---|---|
| 2.2 | 63 of 63 rules are checked and replaced (if needed) | |
| 2.3 | 63 of 63 rules are checked and replaced (if needed) |
Installation
Install as a development dependency:
composer require --dev art4/rector-bc-library
This package is intended to be used alongside Rector. If you don't already have Rector in the project, add it as a dev dependency too:
composer require --dev rector/rector
Usage
The easiest way to use this package is to import the prepared set into your project's rector.php:
<?php
use Rector\Config\RectorConfig;
return RectorConfig::configure()
->withPreparedSets(
// NOTE: Deactivate the default prepared typeDeclaration set with `false`
typeDeclaraion: false
)
// Instead import this package's set as replacement
->withSets([
\Art4\RectorBcLibrary\Set::BC_TYPE_DECLARATION,
])
;
Gradual (level-based) adoption
If you'd like to increase type coverage gradually one level at a time (recommended), you can use the provided helper to apply a subset of rules for a given level. Do not use the ->withTypeCoverageLevel() method. For example:
<?php
use Rector\Config\RectorConfig;
return RectorConfig::configure()
// NOTE: Do not use the withTypeCoverageLevel() method
//->withTypeCoverageLevel(25)
>withRules(\Art4\RectorBcLibrary\Set::withTypeCoverageLevel(25))
;
Start with a low level and raise it iteratively as you gain confidence and run your tests.
Motivation
When Rector adds or narrows type declarations automatically, it usually improves type safety — but it can also introduce breaking changes for downstream projects that extend or override your classes or methods.
This package wraps selected Rector rules and applies them more cautiously to reduce the risk of accidental API breakage for library consumers.
Who should use this
- Library maintainers whose packages are consumed or extended by other projects ✅
- Projects where public API compatibility matters
If you are working on an application (not a library) or you control both upstream and downstream code, the standard Rector rules are often sufficient.
How it works
- Each wrapper inspects the code (for example: final/private status, vendor-lock signals, and other heuristics) before applying a change.
- If the wrapper detects a potential risk to downstream compatibility, it skips the change; otherwise it delegates to the original Rector rule.
- Use the prepared set by importing
\Art4\RectorBcLibrary\Set::BC_TYPE_DECLARATION(see Usage).
Allowed changes in classes
Based on the Symfony Backward Compatibility Promise the Rectors only allows this changes in classes:
Properties
| Type of Change allowed? | final class | not-final |
|---|---|---|
| Add type to a public property | No, only as phpdoc | No, only as phpdoc |
| Add type to a protected property | Yes | No |
| Add type to a private property | Yes | Yes |
Public Methods
| Type of Change allowed? | final class | final method | not-final |
|---|---|---|---|
| Add type hint to an argument | Yes | Yes | No |
| Remove type hint of an argument | Yes | Yes | No |
| Change argument type | Yes | Yes | No |
| Add return type | Yes | Yes | No |
| Remove return type | Yes | Yes | No, only for void |
| Change return type | Yes | Yes | No |
Protected Methods
| Type of Change allowed? | final class | final method | not-final |
|---|---|---|---|
| Add type hint to an argument | Yes | Yes | No |
| Remove type hint of an argument | Yes | Yes | No |
| Change argument type | Yes | Yes | No |
| Add return type | Yes | Yes | No |
| Remove return type | Yes | Yes | No, only for void |
| Change return type | Yes | Yes | No |
Private Methods
| Type of Change allowed? | final class | final method | not-final |
|---|---|---|---|
| Add type hint to an argument | Yes | Yes | Yes |
| Remove type hint of an argument | Yes | Yes | Yes |
| Change argument type | Yes | Yes | Yes |
| Add return type | Yes | Yes | Yes |
| Remove return type | Yes | Yes | Yes |
| Change return type | Yes | Yes | Yes |
Benefits ✅
- Reduce the chance of unexpected breaking changes when modernizing code
- Keep most of Rector's automated improvements while protecting public APIs
- Low-friction: works with your current Rector setup
License
This project is licensed under the GNU General Public License v3 or later (GPL-3.0-or-later). See the LICENSE file for details.
Be sure your project's license is compatible with the GNU GPL v3+ before using this package.
Contributing
Thanks for considering contributing! The fastest way to help is:
- Open an issue with a minimal reproducible example and the Rector version you used.
- If possible, add a failing test or fixture that demonstrates the problem.
- Run tests and static analysis locally:
composer testandcomposer run phpstan. - Open a pull request with a clear description of the fix and why it's safe for library maintainers.
Contributions that include a test and a short explanation are especially appreciated — thanks! 🎉
art4/rector-bc-library 适用场景与选型建议
art4/rector-bc-library 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.43k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「library」 「ruleset」 「bc」 「rector」 「backward compatibility」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 art4/rector-bc-library 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 art4/rector-bc-library 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 art4/rector-bc-library 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Coding styles rules and git hook for NAIS Based Applications
Jeux de règles pour PHP Mess Detector.
Rector rules for Silverstripe CMS.
Validate individual data elements or group validation rules into sets to validate a form.
Appocular PHP coding standard, PHP Code Sniffer rules.
IQRF PHPCodeSniffer ruleset.
统计信息
- 总下载量: 3.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 28
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: GPL-3.0-or-later
- 更新时间: 2025-12-13