art4/rector-bc-library 问题修复 & 功能扩展

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

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

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 VersionStatusDetails
2.2Status63 of 63 rules are checked and replaced (if needed)
2.3Status63 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 classnot-final
Add type to a public propertyNo, only as phpdocNo, only as phpdoc
Add type to a protected propertyYesNo
Add type to a private propertyYesYes

Public Methods

Type of Change allowed?final classfinal methodnot-final
Add type hint to an argumentYesYesNo
Remove type hint of an argumentYesYesNo
Change argument typeYesYesNo
Add return typeYesYesNo
Remove return typeYesYesNo, only for void
Change return typeYesYesNo

Protected Methods

Type of Change allowed?final classfinal methodnot-final
Add type hint to an argumentYesYesNo
Remove type hint of an argumentYesYesNo
Change argument typeYesYesNo
Add return typeYesYesNo
Remove return typeYesYesNo, only for void
Change return typeYesYesNo

Private Methods

Type of Change allowed?final classfinal methodnot-final
Add type hint to an argumentYesYesYes
Remove type hint of an argumentYesYesYes
Change argument typeYesYesYes
Add return typeYesYesYes
Remove return typeYesYesYes
Change return typeYesYesYes

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:

  1. Open an issue with a minimal reproducible example and the Rector version you used.
  2. If possible, add a failing test or fixture that demonstrates the problem.
  3. Run tests and static analysis locally: composer test and composer run phpstan.
  4. 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 art4/rector-bc-library 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2025-12-13