pierophp/php-cs-fixer-extra

v0.0.7 2017-03-08 20:21 UTC

This package is auto-updated.

Last update: 2024-08-29 04:31:10 UTC


README

一些为 SensioLabs PHP CS Fixer 提供的额外修复器。 http://cs.sensiolabs.org/

安装

$ composer require --dev pierophp/php-cs-fixer-extra

使用方法

在项目根目录创建一个 ".php_cs" 文件,内容如下示例

<?php

$finder = Symfony\Component\Finder\Finder::create()
    ->files()
    ->in(__DIR__)
    ->exclude('vendor')
    ->exclude('resources/views')
    ->exclude('storage')
    ->exclude('public')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

return Symfony\CS\Config::create()
    ->addCustomFixer(new \PhpCsFixerExtra\Fixer\PhpdocFullNamespaceFixer())
    ->finder($finder)
    ->setUsingCache(true);

运行命令进行修复

$ ./vendor/bin/php-cs-fixer --config-file=./.php_cs fix my_file.php

在 Git 预提交中配置

如果你有 Git 2.9+ 版本,你可以在预提交中配置 PHP CS。

在项目根目录创建一个路径为 "hooks/pre-commit" 的文件,内容如下

#!/bin/bash

while read -r file;
do
  file=`echo ${file:1}`
  if [[ $file = *.php && -e $file && $file != *migrations* ]]; then
    ./vendor/bin/php-cs-fixer --config-file=./.php_cs fix $file
    git add $file
  fi
done < <(git diff --cached --name-status --diff-filter=ACM)	

之后,添加到 git

$ git config --add core.hooksPath hooks/