phpstorm 配置代码格式化工具

2020-11-26 10:22:36   php分享记录

 

  1. 安装friendsofphp/php-cs-fixer

    1. 随便指定一个位置 新建 tools/php-cs-fixer文件夹,如E:\tools\php-cs-fixer
    2. composer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer
  2. 配置格式化配置文件,放在tools/php-cs-fixer文件夹下,文件名为.php-cs-fixer.dist.php

    1. $header = <<<'EOF'
    2. This file is part of PHP CS Fixer.
    3. (c) Fabien Potencier <fabien@symfony.com>
    4. Dariusz Rumiński <dariusz.ruminski@gmail.com>
    5. This source file is subject to the MIT license that is bundled
    6. with this source code in the file LICENSE.
    7. EOF;
    8. $finder = PhpCsFixer\Finder::create()
    9. ->ignoreDotFiles(false)
    10. ->ignoreVCSIgnored(true)
    11. ->exclude('tests/Fixtures')
    12. ->in(__DIR__)
    13. ->append([
    14. __DIR__.'/dev-tools/doc.php',
    15. // __DIR__.'/php-cs-fixer', disabled, as we want to be able to run bootstrap file even on lower PHP version, to show nice message
    16. ]);
    17. $config = new PhpCsFixer\Config();
    18. $config
    19. ->setRiskyAllowed(true)
    20. ->setRules(
    21. [
    22. '@PHP74Migration' => true,
    23. '@PSR2' => true,
    24. '@PHPUnit60Migration:risky' => true,
    25. '@PhpCsFixer' => true,
    26. 'list_syntax' => ['syntax' => 'long'],
    27. 'align_multiline_comment' => [
    28. 'comment_type' => 'phpdocs_only'
    29. ],
    30. 'array_indentation'=>true,
    31. 'single_quote' => true, //简单字符串应该使用单引号代替双引号;
    32. 'no_unused_imports' => true, //删除没用到的use
    33. 'no_singleline_whitespace_before_semicolons' => true, //禁止只有单行空格和分号的写法;
    34. 'self_accessor' => true, //在当前类中使用 self 代替类名;
    35. 'no_empty_statement' => true, //多余的分号
    36. // 'no_extra_consecutive_blank_lines' => ['extra'], //多余空白行
    37. 'no_blank_lines_after_class_opening' => true, //类开始标签后不应该有空白行;
    38. 'include' => true, //include 和文件路径之间需要有一个空格,文件路径不需要用括号括起来;
    39. 'no_trailing_comma_in_list_call' => true, //删除 list 语句中多余的逗号;
    40. 'no_leading_namespace_whitespace' => true, //命名空间前面不应该有空格;
    41. 'array_syntax' => array('syntax' => 'short'), //数组 【】 php版本大于5.4;
    42. 'no_blank_lines_after_phpdoc' => true, //PHP 文档块开始开始元素下面不应该有空白行;
    43. 'object_operator_without_whitespace' => true, //(->) 两端不应有空格;
    44. 'phpdoc_indent' => true, //phpdoc 应该保持缩进;
    45. 'phpdoc_no_access' => true, //@access 不应该出现在 phpdoc 中;
    46. 'phpdoc_no_package' => true,
    47. 'phpdoc_to_comment' => true, //文档块应该都是结构化的元素;
    48. 'phpdoc_trim' => true,
    49. 'phpdoc_var_without_name' => true, //@var@type 注释中不应该包含变量名;
    50. 'no_leading_import_slash' => true, //删除 use 前的空行;
    51. 'no_trailing_comma_in_singleline_array' => true, //PHP 单行数组最后一个元素后面不应该有空格;
    52. 'single_blank_line_before_namespace' => true,//命名空间声明前应该有一个空白行;
    53. 'binary_operator_spaces' => array('operators'=>['=' => 'align','+=' => 'align_single_space','===' => 'align_single_space_minimal'
    54. ,'|' => 'no_space','=>' =>'align_single_space_minimal_by_scope']), //等号 => 对齐 symfony是不对齐的
    55. 'cast_spaces' => true, //变量和修饰符之间应该有一个空格;
    56. 'standardize_not_equals' => true, //使用 <> 代替 !=;
    57. 'concat_space' => array('spacing' => 'one'), //点连接符左右两边有一个的空格;symfony是没空格
    58. 'ternary_operator_spaces' => true, //三元运算符之间的空格标准化
    59. 'trim_array_spaces' => true, //数组需要格式化成和函数/方法参数类似,上下没有空白行;
    60. 'unary_operator_spaces' => true, //一元运算符和运算数需要相邻;
    61. 'no_whitespace_in_blank_line' => true, //删除空白行中多余的空格;
    62. 'multiline_whitespace_before_semicolons' => false, //分号前的空格
    63. // 'native_function_invocation'=>true,
    64. 'combine_consecutive_issets' => true,
    65. 'elseif'=>true,
    66. 'combine_consecutive_unsets' => true, //多个unset,合并成一个
    67. // one should use PHPUnit methods to set up expected exception instead of annotations
    68. //'header_comment' => array('header' => $header), //添加,替换或者删除 header 注释。
    69. 'heredoc_to_nowdoc' => true, //删除配置中多余的空行和/或者空行。
    70. 'no_extra_blank_lines' => true, //在函数参数中,不能有默认值在非缺省值之前的参数。有风险
    71. 'no_useless_else' => true, //删除无用的eles
    72. 'no_useless_return' => true, //删除函数末尾无用的return
    73. 'no_empty_phpdoc' => true, // 删除空注释
    74. 'no_spaces_inside_parenthesis' => true, //删除括号后内两端的空格
    75. 'no_trailing_whitespace' => true, //删除非空白行末尾的空白
    76. 'no_whitespace_before_comma_in_array' => true, //删除数组声明中,每个逗号前的空格
    77. 'ordered_class_elements' => false, //class elements排序
    78. 'ordered_imports' => false, // use 排序
    79. 'phpdoc_add_missing_param_annotation' => true, //添加缺少的 Phpdoc @param参数
    80. // 'phpdoc_trim_consecutive_blank_line_separation' => true, //删除在摘要之后和PHPDoc中的描述之后,多余的空行。
    81. 'phpdoc_order' => true,
    82. 'psr_autoloading' => true,
    83. // 'strict_comparison' => true, //严格比较,会修改代码有风险
    84. //'strict_param' => true,
    85. 'ternary_to_null_coalescing' => true, //尽可能使用null合并运算符??。需要PHP> = 7.0。
    86. 'whitespace_after_comma_in_array' => true, // 在数组声明中,每个逗号后必须有一个空格
    87. ]
    88. )
    89. ->setFinder($finder);
    90. // special handling of fabbot.io service if it's using too old PHP CS Fixer version
    91. if (false !== getenv('FABBOT_IO')) {
    92. try {
    93. PhpCsFixer\FixerFactory::create()
    94. ->registerBuiltInFixers()
    95. ->registerCustomFixers($config->getCustomFixers())
    96. ->useRuleSet(new PhpCsFixer\RuleSet($config->getRules()));
    97. } catch (PhpCsFixer\ConfigurationException\InvalidConfigurationException $e) {
    98. $config->setRules([]);
    99. } catch (UnexpectedValueException $e) {
    100. $config->setRules([]);
    101. } catch (InvalidArgumentException $e) {
    102. $config->setRules([]);
    103. }
    104. }
    105. return $config;
  3. 打开phpstorm 设置->tools->external tools,新建工具

    1. 名称,描述自己自定义
    2. Tool Settins
    3. ProgramE:\tools\php-cs-fixer\vendor\bin\php-cs-fixer.bat
    4. Arguments: fix $FileDir$/$FileName$ --config=E:\tools\php-cs-fixer\.php-cs-fixer.dist.php
    5. Working directory: $ProjectFileDir$
    6. 高级选项里,我们把 Open console for tool output取消勾选掉,省的每回都弹出一个终端出来
  4. 设置快键键
    1. 设置->Keymap,找到External Tools
    2. 找到你刚才新建的外部工具,双击添加键盘快捷键
  5. 操作你刚设置的快捷键,你会发现,代码已经自动格式化了