Phpstorm安装php-cs-fixer及使用方法

Phpstorm安装php-cs-fixer及使用方法(windows)

第一步:composer引入php-cs-fixer
第二步:在phpstorm中设置php-cs-fixer
第三步:测试及使用


第一步:composer引入php-cs-fixer
在命令行窗口运行 composer global require friendsofphp/php-cs-fixer,执行安装即可。

第二步:在phpstorm中设置php-cs-fixer
在这里插入图片描述
参数如下填写

参数参考值备注
NAMEphp-cs-fixer任意填写
Description规范代码描述内容,任意填写
ProgramD:\phpStudy\PHPTutorial\WWW\laravel\vendor\bin\php-cs-fixer.batcomposer安装的php-cs-fixer的php-cs-fixer.bat的路径
Argumentsfix "$FileDir$\$FileName$" --config=.php_cs.dist --using-cache=noconfig后面的.php_cs.dist是php-cs-fixer的配置文件路径
Working Directory$ProjectFileDir$填写phpstorm的变量宏指令就可以

注意:
表格中的Arguments参数值中.php_cs.dist文件可以自己创建在项目的根目录下面,内容如下:

<?php
$finder = PhpCsFixer\Finder::create()
    ->exclude('tests/Fixtures')
    ->in(__DIR__)
    ->append([__DIR__.'/php-cs-fixer'])
;
$config = PhpCsFixer\Config::create()
    ->setRiskyAllowed(true)
    ->setRules([
        '@PSR2'=>true,
        '@PHP56Migration' => true,
        '@PHPUnit60Migration:risky' => true,
        '@PhpCsFixer' => true,
        '@PhpCsFixer:risky' => true,
        //'header_comment' => ['header' => $header],
        'list_syntax' => ['syntax' => 'long'],
        'single_quote'                               => true, //简单字符串应该使用单引号代替双引号;
        'no_unused_imports'                          => true, //删除没用到的use
        'no_singleline_whitespace_before_semicolons' => true, //禁止只有单行空格和分号的写法;
        'self_accessor'                              => true, //在当前类中使用 self 代替类名;
        'no_empty_statement'                         => true, //多余的分号
        'no_extra_consecutive_blank_lines'           => true, //多余空白行
        'no_blank_lines_after_class_opening'         => true, //类开始标签后不应该有空白行;
        'include'                                    => true, //include 和文件路径之间需要有一个空格,文件路径不需要用括号括起来;
        'no_trailing_comma_in_list_call'             => true, //删除 list 语句中多余的逗号;
        'no_leading_namespace_whitespace'            => true, //命名空间前面不应该有空格;
        'standardize_not_equals'                     => true, //使用 <> 代替 !=;
        'binary_operator_spaces'                     => ['default' => 'align_single_space'] //等号对齐、数字箭头符号对齐
    ])
    ->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;

第三步:测试及使用
点击phpstorm的Tools->External Tools->php-cs-fixer(是第二步中的Name变量值),就可以有下图中的结果.
在这里插入图片描述
如果您觉得看了有用,请点赞哦~~~

  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值