locomotivemtl/charcoal-coding-standard

Charcoal 编码规范

安装: 18

依赖关系: 0

建议者: 0

安全性: 0

星星: 0

关注者: 15

分支: 0

开放问题: 1

类型:phpcodesniffer-standard

dev-master / 0.1.x-dev 2018-06-28 14:08 UTC

This package is auto-updated.

Last update: 2024-08-26 05:06:27 UTC


README

License Latest Stable Version Build Status

这是 PHP_CodeSniffer 规则集,用于检查存储库是否遵循 Charcoal 框架使用的标准。

标准

Charcoal 编码规范扩展并扩展了 PSR-1PSR-2 PSR-12

有关完整执行参考,请参阅 Charcoal/ruleset.xml

安装

1. 项目安装

您可以将 Charcoal 编码规范作为您项目的 Composer 依赖项进行安装。

  1. 通过 Composer 安装包

    $ composer require --dev locomotivemtl/charcoal-coding-standard
  2. 使用 PHP_CodeSniffer 注册新标准

    $ ./vendor/bin/phpcs --config-set installed_paths ./vendor/locomotivemtl/charcoal-coding-standard

    第二个命令将标准注册到 phpcs。确保不要覆盖现有的 installed_paths 选项的值。

  3. 在您的存储库的根目录中创建一个包含以下内容的 phpcs.xml 文件

    <?xml version="1.0"?>
    <ruleset name="Charcoal Package">
        <rule ref="Charcoal" />
    </ruleset>

    您可以从该文件中包含或排除 sniffs。有关自定义的参考,请参阅 PHP_CodeSniffer 注释规则集

    注意:如果您没有使用 PHP_CodeSniffer 注册 Charcoal 编码规范,您可以使用其路径而不是其名称引用标准

    <rule ref="./vendor/locomotivemtl/charcoal-coding-standard/Charcoal/ruleset.xml" />

    示例: phpcs.xml.example

  4. 将脚本添加到您的 composer.json

    "scripts": {
        "cs-check": "phpcs --colors -p ./src ./tests",
        "cs-fix": "phpcbf --colors ./src ./tests"
    }

2. 全局安装

您还可以全局安装 Charcoal 编码规范

  1. 通过 Composer 安装包

    $ composer global require locomotivemtl/charcoal-coding-standard
  2. 使用 PHP_CodeSniffer 注册新标准

    $ phpcs --config-set installed_paths ~/.composer/vendor/locomotivemtl/charcoal-coding-standard

    第二个命令将标准注册到 phpcs。确保不要覆盖现有的 installed_paths 选项的值。

用法

Sniffing

根据您如何安装 Charcoal 编码规范,您可以执行以下操作

使用以下任一方法检测此标准违反情况

$ phpcs --standard=Charcoal /path/to/some/file/to/sniff.php
$ ./vendor/bin/phpcs --standard=Charcoal /path/to/some/file/to/sniff.php
$ composer cs-check

使用以下方法自动修复此标准的违反情况

$ phpcbf --standard=Charcoal /path/to/some/file/to/sniff.php
$ ./vendor/bin/phpcbf --standard=Charcoal /path/to/some/file/to/sniff.php
$ composer cs-fix

有关更多信息,请参阅 CLI 文档

注意:如果您没有使用 PHP_CodeSniffer 注册 Charcoal 编码规范,您可以使用其路径而不是其名称引用标准

$ ./vendor/bin/phpcs --standard=./vendor/locomotivemtl/charcoal-coding-standard /path/to/code

Linting

Charcoal 编码规范提供了一个简单但方便的 bash 脚本包装器,围绕 php -l 递归地同时对 PHP 文件进行语法检查,使用多个进程同时执行。

Usage:
  phplint [options] [<path>]...

Arguments:
  path            One or more paths to files or directories to search for PHP files.
                  Defaults to ./src and ./tests.

Options:
  -h, --help      Display this help message and exit.
  -V, --version   Display version information and exit.

示例

$ phplint src/Http/Controllers/ src/Providers/RouteServiceProvider.php

注意:脚本不是 composer.json 的一部分;当安装包时,它不会添加到指定的项目中。您必须从供应商包中调用脚本,或者您必须手动将其符号链接到您的 bin-dir

  1. 您可以从您的项目中运行脚本

    $ ./vendor/locomotivemtl/charcoal-coding-standard/bin/phplint ./src ./tests
  2. 您还可以将脚本符号链接到您的项目的 bin-dir

    $ cd $(composer config bin-dir)
    $ ln -s ../locomotivemtl/charcoal-coding-standard/bin/phplint ./phplint

有关检查 PHP 文件的更高级和可配置的工具,请参阅 overtrue/phplint

致谢

许可

Charcoal 在 MIT 许可下授权。有关详细信息,请参阅 LICENSE