aretche/php-code-quality

可以在 Symfony 项目中通过 Composer 运行的代码质量脚本。(由 karriere/code-quality 分支而来)

v6.0.1 2019-12-12 16:55 UTC

README

Build Status Code Style

PHP 包的代码质量

此包提供可以通过 Composer 运行的代码质量脚本。

这些脚本也可以在 Jenkins 等持续集成(CI)服务器上运行。

使用的包

FriendsOfPHP/PHP-CS-Fixer

目前用于修复代码。
根据 Symfony 编码规范修复 src 目录中的所有文件。

此包不用于检查(linting),因为 PHP_Codesniffer 输出更易于阅读。

squizlabs/PHP_CodeSniffer

目前用于检查(linting)代码。
检查 src 目录中的所有文件。

此包也可以用于修复,但 PHP-CS-Fixer 做得更好。

phpmd/phpmd

用于错误检测。
src 目录中的所有文件上运行定义的规则集(config/phpmd.xml)。

phpstan/phpstan

用于静态分析。
src 目录中的所有文件执行静态分析。

phpunit/phpunit

用于测试代码。
必须在根目录中的 phpunit.xml 文件中进行配置。

lchrusciel/api-test-case

基于 PHPUnit,用于测试 API 方法。

phpspec/phpspec

用于测试(SpecBDD)代码。
必须在根目录中的 phpspec.yml 文件中进行配置。

我们使用 leanphp/phpspec-code-coverage 扩展来生成覆盖率报告。
此扩展需要根目录中的 phpspec-coverage.yml 文件和已启用 Xdebug。

安装

运行 composer require --dev aretche/php-code-quality 来安装此包。

安装后,将所需的脚本插入到您的 composer.json 中。

{
    "scripts": {
        "lint": "Karriere\\CodeQuality\\CodeStyleChecker::run",
        "fix": "Karriere\\CodeQuality\\CodeStyleFixer::run",
        "md": "Karriere\\CodeQuality\\MessDetector::run",
        "static": "Karriere\\CodeQuality\\StaticAnalyzer::run",
        "unit-test": "Karriere\\CodeQuality\\UnitTest::run",
        "unit-coverage": "Karriere\\CodeQuality\\UnitCoverage::run",
        "check-coverage": "Karriere\\CodeQuality\\CloverCoverageCheck::run",
        "spec-test": "Karriere\\CodeQuality\\SpecificationTest::run",
        "spec-coverage": "Karriere\\CodeQuality\\CodeCoverage::run"
    }
}

用法

您可以使用如下方式运行脚本:composer {script} -- {options}

如果您在 Windows 上使用 Git-Shell(或在 Windows Intellij Terminal 中的 Git-Shell),则可以如此调用脚本:composer.bat {script}。否则颜色将无法显示。

您可以通过添加 --notty 标志来禁用 TTY(对于 Jenkins 需要)。
在 Windows 平台上,它会自动禁用。

composer {script} -- --env=jenkins --notty

脚本

lint

Usage:
  lint [--] [options]

Options:
      --env    Specifiy the environment. Possible values:
               'local': prints output on command-line.
               'jenkins': generates a checkstyle report file.
      --fail   Exit with 1 if linting fails.
      --notty  Disable TTY.

static

Usage:
  static [--] [options]

Options:
      --env    Specifiy the environment. Possible values:
               'local': prints output on command-line.
               'jenkins': generates a json report file (phpstan.json).
      --notty  Disable TTY.

md

Usage:
  md [--] [options]

Options:
      --env    Specifiy the environment. Possible values:
               'local': prints output on command-line.
               'jenkins': generates a xml report file (phpmd.xml).
      --notty  Disable TTY.

fix

Usage:
  fix [--] [options]

Options:
      --dry-run  Show changes to be applyed. 
      --notty    Disable TTY.

unit-test

Usage:
  unit-test [--] [options]

Options:
      --fail     Exit with 1 if tests fail.
      --notty    Disable TTY.
   -v --verbose  Increase the verbosity of messages.

unit-coverage

Usage:
  unit-coverage [--] [options]

Options:
      --env    Specifiy the environment. Possible values:
               'local': generate html report in coverage directory.
               'jenkins': generates clover.xml coverage report in root directory.
      --notty  Disable TTY.

check-coverage

Usage:
  check-coverage [--] [options]

Options:
      --min-coverage    Specify minimal coverage level. Possible
                        values: 0, 25, 50, 100 (default)    
      --fail            Exit with 1 on insufficient coverage.
      --notty           Disable TTY.

spec-test

Usage:
  spec-test [--] [options]

Options:
      --fail     Exit with 1 if tests fail.
      --notty    Disable TTY.
   -v --verbose  Increase the verbosity of messages.

spec-coverage

Usage:
  spec-coverage [--] [options]

Options:
      --env    Specifiy the environment. Possible values:
               'local': prints output on command-line.
               'jenkins': generates a JUnit report file.
      --notty  Disable TTY.

使用自定义匹配器

此包集成了 karriere/phpspec-matchers。为了使用此包中定义的自定义匹配器,请将扩展配置包含在您的 phpspec.yml 中。

常见问题解答

为什么我必须提供两个 phpspec 配置文件?

代码覆盖率扩展会减慢 phpspec 测试,所以我们将其排除在正常配置文件之外。保持测试快速!

如何增加 test-脚本输出的详细程度?

运行 composer test -- -v