克雷塔/php-cs-fixer-config

使用 PHP CS Fixer 的 PHP 代码检查工具,用于克雷塔包

v1.0.3 2017-04-01 12:44 UTC

This package is not auto-updated.

Last update: 2024-09-15 02:07:53 UTC


README

使用 PHP CS Fixer 的 PHP 代码检查工具,针对 Kreta

Build Status

它基于 refinery29/php-cs-fixer-configprooph/php-cs-fixer-config 的理念。

安装

$ composer require --dev kreta/php-cs-fixer-config

用法

配置

在项目根目录下创建配置文件 .php_cs

<?php

/*
 * This file is part of the Kreta package.
 *
 * (c) Beñat Espiña <benatespina@gmail.com>
 * (c) Gorka Laucirica <gorka.lauzirika@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

use Kreta\PhpCsFixerConfig\KretaConfig;

$config = new KretaConfig();
$config->getFinder()->in(__DIR__ . '/src');

$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;

$config->setCacheFile($cacheDir . '/.php_cs.cache');

return $config;

如果您的项目使用 PhpSpec BDD 测试框架,也在项目根目录下创建一个 .phpspec_cs 文件

<?php

/*
 * This file is part of the Kreta package.
 *
 * (c) Beñat Espiña <benatespina@gmail.com>
 * (c) Gorka Laucirica <gorka.lauzirika@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

declare(strict_types=1);

use Kreta\PhpCsFixerConfig\KretaConfig;

$config = new KretaConfig(true);
$config->getFinder()
    ->in(__DIR__ . '/tests/Spec')
    ->name('*Spec.php');

$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;

$config->setCacheFile($cacheDir . '/.php_cs.cache');

return $config;

Git

.php_cs.cache(这是由 php-cs-fixer 创建的缓存文件)添加到 .gitignore

/vendor
/.php_cs.cache

Composer

composer.json 文件中添加以下脚本

(...)

"scripts": {
    (...)

    "cs": [
        "php-cs-fixer fix --config=.php_cs -v",
        "php-cs-fixer fix --config=.phpspec_cs -v"
    ]
},

Travis

更新您的 .travis.yml 以缓存 php_cs.cache 文件

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

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

script:
  - vendor/bin/php-cs-fixer fix --config=.php_cs --verbose --diff --dry-run
  - vendor/bin/php-cs-fixer fix --config=.phpspec_cs --verbose --diff --dry-run

修复问题

手动

如果您需要本地修复问题并且之前添加了 composer 脚本,只需运行

$ composer run-script cs

否则您可以运行

$ vendor/bin/php-cs-fixer fix -v
$ vendor/bin/php-cs-fixer fix -v --config=.phpspec_cs 

致谢

克雷塔由以下人员创建

@benatespina - benatespina@gmail.com
@gorkalaucirica - gorka.lauzirika@gmail.com

许可选项

License