定制 art4/rector-bc-library 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

art4/rector-bc-library

最新稳定版本:1.0.0

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! 🎉

统计信息

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

GitHub 信息

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

其他信息

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