cambis / silverstripe-rector
Silverstripe 的 Rector 规则。
v0.7.0-alpha2
2024-09-16 21:51 UTC
Requires
- php: ^7.4 || ^8.0
- rector/rector: ^1.2
- silverstripe/framework: ^4.0 || ^5.0
- webmozart/assert: ^1.11
This package is auto-updated.
Last update: 2024-09-16 22:13:05 UTC
README
此项目包含 Rector 规则,用于 Silverstripe CMS。
查看可用的 Silverstripe 规则。
先决条件 🦺
php ^7.4 || ^8.0 silverstripe/framework ^4.0 || ^5.0
安装 👷♀️
通过 composer 安装。
composer require --dev cambis/silverstripe-rector
确保您的 composer.json
中已设置 PSR-4 自动加载。
{ "autoload": { "classmap": [ "app/src/Page.php", "app/src/PageController.php" ], "psr-4": { "MyProjectNamespace\\": "app/src" } }, "autoload-dev": { "psr-4": { "MyProjectNamespace\\Tests\\": "app/tests" } } }
验证一切符合 PSR-4 规范。
composer dumpautoload -o
重新构建您的应用程序。
vendor/bin/sake dev/build "flush=1"
配置 🚧
如果您没有现有的 rector.php
文件,运行以下命令,Rector 将为您创建一个。
vendor/bin/rector
然后使用 SilverstripeLevelSetList
和 SilverstripeSetList
集合,并选择一个常量。
<?php declare(strict_types=1); use Cambis\SilverstripeRector\Set\ValueObject\SilverstripeLevelSetList; use Cambis\SilverstripeRector\Set\ValueObject\SilverstripeSetList; use Rector\Config\RectorConfig; return RectorConfig::configure() ->withPaths([ __DIR__ . '/app/_config.php', __DIR__ . '/app/src', __DIR__ . '/app/tests', ]) ->withSets([ SilverstripeLevelSetList::UP_TO_SILVERSTRIPE_52, SilverstripeSetList::CODE_QUALITY, ]);
用法 🏃
使用 Rector 分析您的代码并审查建议的更改。
vendor/bin/rector process --dry-run
审查后应用建议的更改。
vendor/bin/rector process
有关更多信息,请参阅 官方文档。
故障排除 😢
运行 rector 时可能会遇到一些问题。如果您遇到问题,以下示例可能会对您有所帮助。
Rector 自动加载问题
如果您遇到“尝试分析时找不到类 ...”等问题,请参阅 官方文档。
Silverstripe 自动加载问题
如果您收到“系统错误:“Interface App\Contract\FooInterface was not found””之类的错误,请首先重新构建应用程序,然后再运行 Rector。
vendor/bin/sake dev/build "flush=1"
如果问题仍然存在,请检查您是否在代码中错误地导入了受影响的类。以下示例说明了这种情况。
<?php namespace App\Contract; interface FooInterface { } namespace App\Model; use App\Contract\Foointerface; // <--- The casing for this use statement is wrong and will likely cause an error. use SilverStripe\ORM\DataObject; class Foo extends DataObject implements Foointerface { }
修复导入的大小写并重新构建应用程序后再运行 Rector。
如果问题仍然持续,您可以在引导过程中包含受影响的文件来尝试解决它。
首先复制现有的引导文件
cp vendor/cambis/silverstripe-rector/bootstrap.php ./rector-bootstrap.php
然后修改文件如下
<?php declare(strict_types=1); +use App\Contract\FooInterface use SilverStripe\Core\DatabaselessKernel; use SilverStripe\ORM\Connect\NullDatabase; use SilverStripe\ORM\DB; +// Include any 'missing' files here using the following format: +if (!class_exists(FooInterface::class)) { + require_once __DIR__ . '/app/src/Contract/FooInterface.php'; +} -// Add Page/PageController stubs which may be required +// Add Page/PageController if (!class_exists(Page::class)) { - require __DIR__ . '/stubs/Page.php'; + require_once __DIR__ . '/app/src/Page.php'; } if (!class_exists(PageController::class)) { - require __DIR__ . '/stubs/PageController.php'; + require_once __DIR__ . '/app/src/PageController.php'; } // Feel free to leave the rest of file unchanged
最后,在您的配置中包含自定义的引导文件
<?php declare(strict_types=1); use Cambis\SilverstripeRector\Set\ValueObject\SilverstripeLevelSetList; use Rector\Config\RectorConfig; return RectorConfig::configure() ->withBootstrapFiles([ // Include the custom bootstrap file here __DIR__ . '/rector-bootstrap.php', ]) ->withPaths([ __DIR__ . '/app/_config.php', __DIR__ . '/app/src', __DIR__ . '/app/tests', ]) ->withSets([ SilverstripeLevelSetList::UP_TO_SILVERSTRIPE_52, ]);
请记住,在再次运行 Rector 之前,首先重新构建应用程序。