nexusphp/cs-config
Composer 安装命令:
composer require nexusphp/cs-config
包简介
A factory for custom rulesets for PHP CS Fixer.
README 文档
README
This library provides a factory for custom rulesets for friendsofphp/php-cs-fixer.
Installation
You can add this library as a local, per-project dependency to your project using Composer:
composer require nexusphp/cs-config
If you only need this library during development, for instance to run your project's test suite, then you should add it as a development-time dependency:
composer require --dev nexusphp/cs-config
Configuration
- Create a
.php-cs-fixer.dist.phpat the root of your project:
<?php use Nexus\CsConfig\Factory; use Nexus\CsConfig\Ruleset\Nexus82; return Factory::create(new Nexus82())->forProjects();
- Include the cache file in your
.gitignore. By default, the cache file will be saved in the project root.
vendor/ +# php-cs-fixer +.php-cs-fixer.php +.php-cs-fixer.cache
Advanced Configuration
Adding Preformatted License Header
You can create a preformatted license header to all PHP files by using the public forLibrary() method
instead of forProjects(). This method accepts two required arguments (the library name and author) and
two optional arguments (the email address and starting year of license).
- Scenario 1: Providing all arguments
<?php use Nexus\CsConfig\Factory; use Nexus\CsConfig\Ruleset\Nexus82; -return Factory::create(new Nexus82())->forProjects(); +return Factory::create(new Nexus82())->forLibrary('My Library', 'John Doe', 'john@doe.com', 2020);
This setting will configure a license header similar to below:
<?php /** * This file is part of My Library. * * (c) 2020 John Doe <john@doe.com> * * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */ namespace Nexus\CsConfig;
- Scenario 2: Providing only the required arguments
If you opted not to provide any of the optional arguments (i.e., email address, starting license year), these will not be shown on the license header allowing flexibility on the copyright portion.
<?php use Nexus\CsConfig\Factory; use Nexus\CsConfig\Ruleset\Nexus82; -return Factory::create(new Nexus82())->forProjects(); +return Factory::create(new Nexus82())->forLibrary('My Library', 'John Doe');
This will give the following license header:
<?php /** * This file is part of My Library. * * (c) John Doe * * For the full copyright and license information, please view * the LICENSE file that was distributed with this source code. */ namespace Nexus\CsConfig;
Overriding Rules in a Ruleset
If you feel that a specific rule in the ruleset is not appropriate for you, you can override it instead of creating a new ruleset:
<?php use Nexus\CsConfig\Factory; use Nexus\CsConfig\Ruleset\Nexus82; -return Factory::create(new Nexus82())->forProjects(); +return Factory::create(new Nexus82(), [ + 'binary_operator_spaces' => false, +])->forProjects();
Specifying Options to PhpCsFixer\Config
The Factory class returns an instance of PhpCsFixer\Config and fully supports all of
its properties setup. You can pass an array to the third parameter of Factory::create()
containing your desired options.
Options
| Key | Allowed Types | Default |
|---|---|---|
| cacheFile | string |
.php-cs-fixer.cache |
| customFixers | FixerInterface[], iterable, \Traversable |
[] |
| finder | iterable, string[], \Traversable |
default PhpCsFixer\Finder instance |
| format | string |
txt |
| hideProgress | bool |
false |
| indent | string |
' ' // 4 spaces |
| lineEnding | string |
"\n" |
| isRiskyAllowed | bool |
false |
| usingCache | bool |
true |
| customRules | array |
[] |
| ruleCustomisers | RuleCustomisationPolicyInterface or null |
null |
<?php use Nexus\CsConfig\Factory; use Nexus\CsConfig\Ruleset\Nexus82; -return Factory::create(new Nexus82())->forProjects(); +return Factory::create(new Nexus82(), [], [ + 'usingCache' => false, + 'hideProgress => true, +])->forProjects();
Customization of Ruleset
What is the purpose of a configuration factory if not able to create a custom ruleset for an organization-wide usage, right? Well, you are not constrained to use the default rulesets and putting a long array of overrides. That's pretty nasty.
The way to achieve this is dependent on you but the main idea is creating a new ruleset that
extends Nexus\CsConfig\Ruleset\AbstractRuleset. Yup, it's that easy. Then you just need to
provide details for its required four (4) protected properties.
<?php namespace MyCompany\CodingStandards\Ruleset; use Nexus\CsConfig\Ruleset\AbstractRuleset; final class MyCompany extends AbstractRuleset { public function __construct() { $this->name = 'My Company'; $this->rules = [ '@PSR2' => true, ... ]; $this->requiredPHPVersion = 80200; $this->autoActivateIsRiskyAllowed = true; } }
Then, in creating your .php-cs-fixer.dist.php, use your own ruleset.
<?php use Nexus\CsConfig\Factory; use MyCompany\CodingStandards\Ruleset\MyCompany; return Factory::create(new MyCompany())->forProjects();
Credits
This project is inspired by and an enhancement of ergebnis/php-cs-fixer-config.
Contributing
Contributions are very much welcome. If you see an improvement or bugfix, open a PR now!
nexusphp/cs-config 适用场景与选型建议
nexusphp/cs-config 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.79M 次下载、GitHub Stars 达 3, 最近一次更新时间为 2020 年 12 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 nexusphp/cs-config 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nexusphp/cs-config 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.79M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 24
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-01