prooph/php-cs-fixer-config

prooph组件的PHP CS Fixer配置

v0.5.0 2021-08-31 19:05 UTC

This package is auto-updated.

Last update: 2024-09-13 05:37:59 UTC


README

prooph组件的PHP CS Fixer配置

Build Status Coverage Status Gitter

它基于refinery29/php-cs-fixer-config的思路。

安装

运行

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

添加到composer.json文件中;

"scripts": {
  "check": [
    "@cs",
  ],
  "cs": "php-cs-fixer fix -v --diff --dry-run",
  "cs-fix": "php-cs-fixer fix -v --diff",
}

使用方法

配置

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

<?php

$config = new Prooph\CS\Config\Prooph();
$config->getFinder()->in(__DIR__);
$config->getFinder()->append(['.php-cs-fixer.php']);

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

$config->setCacheFile($cacheDir . '/.php-cs-fixer.cache');

return $config;

头部注释

当你在项目根目录创建一个.docheader文件时,它将被用作头部注释。

建议使用以下模板,但你可以使用任何你想要的。

This file is part of `%package%`.
(c) 2016-%year% prooph software GmbH <[email protected]>
(c) 2016-%year% Sascha-Oliver Prolic <[email protected]>

For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.

Git

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

vendor/
.php-cs-fixer.cache

Travis

更新你的.travis.yml以缓存.php-cs-fixer.cache文件

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

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

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

修复问题

手动

如果你需要本地修复问题,只需运行

$ composer cs-fix

预提交钩子

你可以添加一个pre-commit钩子

$ touch .git/pre-commit && chmod +x .git/pre-commit

将以下内容粘贴到.git/pre-commit

#!/usr/bin/env bash

echo "pre commit hook start"

CURRENT_DIRECTORY=`pwd`
GIT_HOOKS_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

PROJECT_DIRECTORY="$GIT_HOOKS_DIR/../.."

cd $PROJECT_DIRECTORY;
PHP_CS_FIXER="vendor/bin/php-cs-fixer"

HAS_PHP_CS_FIXER=false

if [ -x "$PHP_CS_FIXER" ]; then
    HAS_PHP_CS_FIXER=true
fi

if $HAS_PHP_CS_FIXER; then
    git status --porcelain | grep -e '^[AM]\(.*\).php$' | cut -c 3- | while read line; do
        ${PHP_CS_FIXER} fix --config-file=.php-cs-fixer.php --verbose ${line};
        git add "$line";
    done
else
    echo ""
    echo "Please install php-cs-fixer, e.g.:"
    echo ""
    echo "  composer require friendsofphp/php-cs-fixer:2.0.0"
    echo ""
fi

cd $CURRENT_DIRECTORY;
echo "pre commit hook finish"

许可证

本软件包使用MIT许可证授权。