PhpStorm中配置 php-cs-fixer 以工具插件的方式 格式化代码


1、安装

   1.1 直接下载:https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases

      请注意版本号,不同的版本使用的配置文件不同。

      默认的配置请在:https://github.com/PHP-CS-Fixer/PHP-CS-Fixer 仓库中找到 .php-cs-fixer.dist.php 文件 (版本2+的文件是 .php_cs.dist),并复制到自己项目的根目录。

 

   1.2 通过composer安装

   新建composer.json文件,并写入如下代码。然后 终端切换到文件所在目录,执行 composer update 命令

{
       "require" :{
           "friendsofphp/php-cs-fixer": "^3.8"
       }
}

或者

mkdir -p tools/php-cs-fixer
composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer

 

2、配置

在PhpStorm中  File | Settings | Tools | External Tools 类新增工具插件

Name: php-cs-fixer
Program: /Users/wang/Desktop/tools/php-cs-fixer3/vendor/bin/php-cs-fixer
Arguments: fix $FileDir$/$FileName$ --config=.php-cs-fixer.dist.php
Working director: $ProjectFileDir$
  • Name 工具名称
  • Program 安装的php-cs-fixer路径
  • Arguments 要执行的命令 实参
  • Working drector 执行命令的工作目录

 

也可以将命令 修改为 fix $FileDir$ --config=.php-cs-fixer.dist.php     这样就可以直接格式化选中目录下的所有文件

可以根据自己的习惯和需求修改参数。

 

 

3、设置系统环境变量

将  /Users/wang/Desktop/tools/php-cs-fixer3 (文件所在目录) 设置为系统环境变量。

Window系统也是如此。

 

4、命令行

   4.1 如果未设置系统环境变量,格式化指定PHP文件的命令如下:

/Users/wang/Desktop/tools/php-cs-fixer3/vendor/bin/php-cs-fixer fix /文件路径/Nav.php

 

   4.2 设置了系统环境变量,命令精简为为如下:

php-cs-fixer fix /文件路径/Nav.php

 

实际应用中,使用命令行的时机很少。在PhpStorm中配置好工具插件,直接在项目中右键选择工具插件格式化。

5、附录

   5.1 目前我的项目中使用的配置文件 .php-cs-fixer.dist.php代码如下:

<?php

$header = <<<'EOF'
 +----------------------------------------------------------------------
 | wangqy
 +----------------------------------------------------------------------
 | Copyright (c) 2022 https://upwqy.com All rights reserved.
 +----------------------------------------------------------------------
 | Author: wangqy <529857614@qq.com>
 +----------------------------------------------------------------------
EOF;

$finder = PhpCsFixer\Finder::create()
    ->ignoreDotFiles(false)
    ->ignoreVCSIgnored(true)
    ->exclude('tests/Fixtures')
    ->in(__DIR__)
    ->append([
        __DIR__.'/dev-tools/doc.php',
        // __DIR__.'/php-cs-fixer', disabled, as we want to be able to run bootstrap file even on lower PHP version, to show nice message
    ])
;

$config = new PhpCsFixer\Config();
$config
    ->setRiskyAllowed(true)
    ->setRules([
        '@PHP71Migration' => true,
        //        '@PHP71Migration:risky' => true,
        '@PHPUnit75Migration:risky' => true,
        '@PhpCsFixer' => true,
        '@PhpCsFixer:risky' => true,
        'general_phpdoc_annotation_remove' => ['annotations' => ['expectedDeprecation']], // one should use PHPUnit built-in method instead
        'header_comment' => ['header' => $header],
        //        'ordered_class_elements' => ['sort_algorithm' => 'alpha'],
        'php_unit_test_class_requires_covers' => false,
        'php_unit_internal_class' => false,
        'strict_comparison' => false,
        'strict_param' => false,
        //        'phpdoc_add_missing_param_annotation' => ['only_untyped' => false],
        //        'phpdoc_no_empty_return' => false
        //        'modernize_strpos' => true, // needs PHP 8+ or polyfill
    ])
    ->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;

 

5.2 PhpStorm提供的文档用于配置php-cs-fixer检测

https://www.jetbrains.com/help/phpstorm/using-php-cs-fixer.html

 

发布时间 : 2023-03-01,阅读量:1121 , 分类: PHP
本文链接:https://upwqy.com/details/388.html
PHP 页面跳转的三种方式 设计模式:单例模式