windows 环境下 PHPstorm 安装 PHP-CS-Fixer 及使用说明

windows 环境下 PHPstorm 安装 PHP-CS-Fixer 及使用说明

PHP-CS-Fixer 官方 github 网站:https://github.com/FriendsOfPHP/PHP-CS-Fixer

官方文档安装已经有了详细的说明,这里我们采用 composer 的方式安装。

  1. composer 中引入 PHP-CS-Fixer
$ composer global require friendsofphp/php-cs-fixer

这是全局安装, composer 会自动跳转到根目录地址自动下载,中间下载速度可能会比较慢,如果断了链接它会在有效范围内自动重连,耐心等待一段时间。

  1. PHPstorm 中设置 PHP-CS-Fixer
    依次打开设置:File>Settings>Tools>External Tools
    windows 环境下 PHPstorm 安装 PHP-CS-Fixer 及使用说明_第1张图片
    点击 + 号新增。
    windows 环境下 PHPstorm 安装 PHP-CS-Fixer 及使用说明_第2张图片
    参数说明:
参数 参考值 备注
Name php-cs-fixer 名称,任意填写
Description php-cs-fixer 规范代码 描述内容,任意填写
Program C:\Users\win10\AppData\Roaming\Composer\vendor\bin\php-cs-fixer.bat composer 安装的 php-cs-fixer 的 php-cs-fixer.bat 的路径
Arguments fix $FileDir$\$FileName$ --config=.php_cs.dist --using-cache=no config 参数后面的 .php_cs.dist 是 php-cs-fixer 的配置文件路径(即这个是文件的存放地址,根据你的实际环境来。)
Working Directory $Projectpath$ 工作目录,填写 phpstorm 的变量宏指令就可以

这里需要注意到是:$变量名$ 这种类型的格式属于 PHPstorm 宏变量指令(Macros)。

  1. 表格中的 Arguments 参数值中的 .php_cs.dist 是官方提供的格式化标准,其实就是 php 文件,可以从官方 github 仓库中下载。文件参数可以在自己创建项目的根目录下面建立,比如指令:fix $FileDir$\$FileName$ --config=E:\PhpStudy20180211\PHPTutorial\WWW\DolphinPHPV1.4.5\.php_cs.dist

目前官方 .php_cs.dist文件 ,这些格式化控制可以根据你自己的需要去定制 ,内容如下:

<?php

/*
 * This file is part of PHP CS Fixer.
 * (c) Fabien Potencier <[email protected]>
 *     Dariusz Rumiński <[email protected]>
 * This source file is subject to the MIT license that is bundled
 * with this source code in the file LICENSE.
 */

$header = <<<'EOF'
This file is part of PHP CS Fixer.
(c) Fabien Potencier <[email protected]>
    Dariusz Rumiński <[email protected]>
This source file is subject to the MIT license that is bundled
with this source code in the file LICENSE.
EOF;

$finder = PhpCsFixer\Finder::create()
    ->exclude('tests/Fixtures')
    ->in(__DIR__)
    ->append([__DIR__.'/php-cs-fixer'])
;

$config = PhpCsFixer\Config::create()
    ->setRiskyAllowed(true)
    ->setRules([
        '@PHP56Migration' => true,
        '@PHPUnit60Migration:risky' => true,
        '@PhpCsFixer' => true,
        '@PhpCsFixer:risky' => true,
        'header_comment' => ['header' => $header],
        'list_syntax' => ['syntax' => 'long'],
    ])
    ->setFinder($finder)
;

// special handling of fabbot.io service if it's using too old PHP CS Fixer version
if (false !== getenv('FABBOT_IO')) {
    try {
        PhpCsFixer\FixerFactory::create()
            ->registerBuiltInFixers()
            ->registerCustomFixers($config->getCustomFixers())
            ->useRuleSet(new PhpCsFixer\RuleSet($config->getRules()))
        ;
    } catch (PhpCsFixer\ConfigurationException\InvalidConfigurationException $e) {
        $config->setRules([]);
    } catch (UnexpectedValueException $e) {
        $config->setRules([]);
    } catch (InvalidArgumentException $e) {
        $config->setRules([]);
    }
}

return $config;

每个文件格式化之后在头部会有一段头部注释,可以修改或者取消,取消则注释掉以下命令:

// 'header_comment' => ['header' => $header],

这样就完成了所有配置,通过 PHPstorm 程序格式化 php 文件,通过 Tools>External Tools 去选择。
windows 环境下 PHPstorm 安装 PHP-CS-Fixer 及使用说明_第3张图片
可以看到,使用如下:
windows 环境下 PHPstorm 安装 PHP-CS-Fixer 及使用说明_第4张图片


如果刚开始使用该工具,可能有疑问?PHPstorm 自带的格式化就挺好,为什么还要折腾。实际上,这个工具更多的是为了规范团队内部风格代码的统一,也是让我们更好的遵守 PSR 规范。


每次执行一次文件的代码检查,类似键入命令,只是配置到 IDE 上,帮我们自动完成了这个步骤。如果不需要配置 IDE 。可以如下直接使用:

多个规则可以用 “,” 隔开:

$ php php-cs-fixer.phar fix /path/to/dir # 对目录下面的 php 文件格式化
$ php php-cs-fixer.phar fix /path/to/file # 对单个 php 文件格式化
$ php php-cs-fixer.phar fix /path/to/project --rules=@PSR2 # 采用 PSR2 规范进行格式化

更多使用请参照官方文档

你可能感兴趣的:(PHP-CS-Fixer)