susina/coding-standard

php-cs-fixer 的共享配置,基于 PSR1 和 PSR2

v2.3 2021-07-27 05:46 UTC

This package is auto-updated.

Last update: 2024-08-27 12:58:06 UTC


README

Test Installation

Susina 编码标准库是一组针对 Susina 项目仓库的 php-cs-fixer 规则。它基于 PSR1PSR12,具有以下区别

  • 始终在文件开头使用 declare(strict_types=1) 并将其放置在 <?php 语句之后,一个空格。例如:<?php declare(strict_types=1);
  • 始终使用 短数组语法
  • 始终在字符串连接操作符周围放置一个空格:'string1' . 'string2'

安装

运行

$ composer require --dev susina/coding-standard

配置

在项目的根目录下创建一个 .php-cs-fixer.php 文件

<?php declare(strict_types=1);

$config = new Susina\CodingStandard\Config();
$config->getFinder()
    ->in(__DIR__ . '/src')
    ->in(__DIR__ . '/tests')
    ->excludes(['fixtures'])
;

return $config;

有关更多信息,请参阅 php-cs-fixer 文档

Composer 脚本

将以下行添加到您的 composer.json 文件中

 "scripts" : {
     "cs" : "php-cs-fixer fix -v --diff --dry-run",
     "cs-fix" : "php-cs-fixer fix -v --diff"
 }

现在您可以通过运行以下命令来检查您的代码风格

composer cs

并且您可以通过以下命令来修复它

composer cs-fix

Git

我们建议将 .php-cs-fixer.cache 添加到 .gitignore

vendor/
.php-cs-fixer.cache

Travis

您可以将 Travis 配置为缓存 .php-cs-fixer.cache 文件。更新您的 .travis.yml

cache:
  directories:
    - $HOME/.php-cs-fixer

然后在 script 部分中运行 php-cs-fixer

script:
  - composer cs

Github Actions

您可以将 Github actions 配置为缓存 .php-cs-fixer.cache 文件。在您的 workflow 中,在 Cache 配置步骤中,只需在 path 键下添加 ~/.php-cs-fixer.cache

- name: Cache multiple paths
  uses: actions/cache@v2
  with:
     path: |
        ~/.php-cs-fixer.cache
     key: your-awesome-cache-key

许可证

此软件包受 Apache2 许可证 的许可。有关完整的版权信息,请参阅此存储库中的 LICENSE 文件。