wernerkrauss / silverstripe-rector
Silverstripe CMS 的 Rector 规则
v0.1
2024-02-19 17:12 UTC
Requires
- php: ^7.4 | ^8
- rector/rector: ^1
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-08-31 00:29:59 UTC
README
silverstripe-rector
用于自动升级 Silverstripe CMS 中废弃代码的开发工具
正在进行中,正在收集自动化的想法
关于 rector
rector
是一个用于自动代码升级和重构的工具。更多信息请参阅 rector 主页。
安装
此模块可以通过 composer 安装。由于 rector 使用 phpstan,建议也安装 syntro/silverstripe-phpstan
。
composer require syntro/silverstripe-phpstan --dev
composer require wernerkrauss/silverstripe-rector
vendor/bin/rector init
在项目根目录中创建一个基本的 phpstan.neon 文件
includes: - vendor/syntro/silverstripe-phpstan/phpstan.neon
这将添加所有要求并在项目根目录中创建一个名为 rector.php
的文件。您需要调整它,例如添加要升级的代码目录和要使用的规则。
一个基本的 rector 配置可能如下所示
<?php declare(strict_types=1); use Netwerkstatt\SilverstripeRector\Rector\Injector\UseCreateRector; use Netwerkstatt\SilverstripeRector\Rector\Misc\AddConfigPropertiesRector use Netwerkstatt\SilverstripeRector\Set\SilverstripeSetList; use Netwerkstatt\SilverstripeRector\Set\SilverstripeLevelSetList; use Rector\CodeQuality\Rector\Class_\InlineConstructorDefaultToPropertyRector; use Rector\Config\RectorConfig; use Rector\Set\ValueObject\LevelSetList; use Rector\Set\ValueObject\SetList; return static function (RectorConfig $rectorConfig): void { $rectorConfig->paths([ __DIR__ . '/app/_config.php', __DIR__ . '/app/src', ]); $rectorConfig->autoloadPaths([ //composer autoload is already loaded ]); //add needed files from modules, that don't support composer autoload yet $rectorConfig->bootstrapFiles([ __DIR__ . '/vendor/path/to/code/Foobar.php' ]); // // register a single rule $rectorConfig->rule(InlineConstructorDefaultToPropertyRector::class); // // define sets of rules $rectorConfig->sets([ //rector lists LevelSetList::UP_TO_PHP_74, SetList::CODE_QUALITY, SetList::CODING_STYLE, //silverstripe rector SilverstripeSetList::CODE_STYLE, SilverstripeLevelSetList::UP_TO_SS_4_13 ]); //add @config properites to configurations for phpstan //configure your own configs $rectorConfig->ruleWithConfiguration( AddConfigPropertiesRector::class, [ MyClass::class => [ 'my_config', 'another_config' ] ] ); };
Silverstripe-rector 随带两种类型的 SetLists:用于单个 rectors 集合的 SilverstripeSetList
(例如,从 5.0 升级到 5.1 或用于一般 Silverstripe 代码样式)和用于组合到给定 Silverstripe CMS 版本的 SilverstripeLevelSetList
,例如运行所有升级到 Silverstripe 5.1 的操作。
运行 rector
配置完成后,您可以在命令行中使用以下命令运行 rector
vendor/bin/rector --dry-run
选项 --dry-run
会打印代码更改;如果您对更改满意,可以移除该选项,rector 将实际更改文件。
有用的选项
--debug
用于调试详细程度。哪些文件和规则被处理了?--xdebug
开关允许运行 xdebug。
有关更多选项,请参阅 vendor/bin/rector --help
。
待办事项
SS3 到 SS4 升级(在运行官方升级工具之前)
- 将
Foo_Controller
重命名为FooController
- 如何使其动态化?通过扫描当前项目的配置脚本?
- 配置 PSR4 类到文件
- 也许给
src
目录添加命名空间 - 各种废弃
- 是否有可能自动化之前在 PHP 中配置现在在 YML 中配置的东西?
- 简单的修复方法是切换到新的 PHP 配置层并添加一个注释来手动修复此问题
- 修复模板中废弃的旧
Image
函数- 这需要一个用于 Silverstripe 模板的另一个文件解析器
- 将
class
到trait
,请参阅 ParentClassToTraitsRector
SS4 升级
- 如果缺少,则添加
$table_name
- 使用短类名代替 - 各种废弃
- 需要在集合列表中手动配置
- 修复 Image 和 File 关系中缺失的
$owns
- 如果不需要,则可配置排除列表
- 可配置哪些关系应自动拥有(例如,其他版本化的 DataObjects)
通用
其他
- 创建 SetLists 以便于配置
代码质量
- 如果它是一个 Silverstripe / Injectable 类,则将
new Foo()
转换为Foo::create()
- 将
@config
参数添加到$db
、$has_one
等中。 - 使用请求处理器而不是超全局变量 $_GET 和 $_POST。