1.全局安装php-cs-fixer

composer global require friendsofphp/php-cs-fixer

2.设置外部工具

名称:php-cs-fixer(自己喜欢的即可)
文件类型:PHP
程序:D:AppDataRoamingComposervendorbinphp-cs-fixer.bat
参数:fix $FileDir$/$FileName$ -vvv --diff --config=.php-cs-fixer.php --using-cache=no
工作目录:$ProjectFileDir$
自动保存编辑的文件以触发观察程序:去掉默认的勾选
显示控制台:改为始终

3.设置规则和扫描的目录

# F:WWWxxx.php-cs-fixer.php
<?php

use PhpCsFixerConfig;
use PhpCsFixerFinder;

$rules = [
    '@PHP80Migration' => true, // PHP 8.0 的语法迁移

    'ordered_imports' => [
        'sort_algorithm' => 'alpha', // 按字母顺序排序导入
    ],
    'class_attributes_separation' => [
        'elements' => [
            'const' => 'one', // 常量之间空一行
            'method' => 'one', // 方法之间空一行
            'property' => 'one', // 属性之间空一行
        ],
    ],
    '@PSR12' => true, // 使用 PSR-12 编码标准
    '@PHPUnit100Migration:risky' => true, // PHPUnit 10.0 迁移(风险)
    '@PhpCsFixer' => true, // PhpCsFixer 自带规则
    '@PhpCsFixer:risky' => true, // PhpCsFixer 自带规则(风险)
    'list_syntax' => ['syntax' => 'long'], // 使用长列表语法
    'encoding' => true, // PHP代码必须只使用没有BOM的UTF-8
    'line_ending' => true, // 所有的PHP文件编码必须一致
    'array_syntax' => ['syntax' => 'short'], // 用[]关键字来定义数组
    'single_quote' => true, // 简单字符串应该使用单引号代替双引号
    'self_accessor' => true, // 在当前类中使用 self 代替类名
    'binary_operator_spaces' => ['default' => 'single_space'], // 运算符应包含单个空格
    'include' => true, // Include/Require 时不使用括号
    'standardize_not_equals' => true, // 使用 <> 代替 !=
    'no_closing_tag' => true, // 关闭标签必须在 PHP 文件中去掉
    'no_unused_imports' => true, // 删除没用到的 use
    'no_singleline_whitespace_before_semicolons' => true, // 禁止在关闭分号前使用单行空格
    'no_empty_statement' => true, // 不应该存在空的结构体
    'no_unreachable_default_argument_value' => true, // 不可到达的默认参数值
    'no_blank_lines_after_class_opening' => true, // class 开标签后面不应该有空行
    'no_leading_namespace_whitespace' => true, // 命名空间前面不应该有空格
    'no_useless_else' => true, // 不需要没有用的 else 分支
    'no_useless_return' => true, // 删除无用的 return
    'object_operator_without_whitespace' => true, // 对象操作符前后不留空格
    'ordered_class_elements' => true, // 类元素排序
    'phpdoc_add_missing_param_annotation' => true, // 添加缺失的参数注释
    'strict_comparison' => false, // 不强制严格比较
    'strict_param' => false, // 不强制严格参数
    'concat_space' => ['spacing' => 'one'], // 连接字符需要一个空格
    'simplified_null_return' => true, // 简化返回 null
    'increment_style' => false, // 不强制前置递增
    'align_multiline_comment' => ['comment_type' => 'phpdocs_only'], // 多行注释对齐
    'blank_line_after_namespace' => true, // 命名空间之后空一行
    'blank_line_after_opening_tag' => false, // <?php 后面不加空行
    'blank_line_before_statement' => ['statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try']], // 在语句前加空行
    'cast_spaces' => ['space' => 'none'], // 类型转换不加空格
    'combine_consecutive_issets' => true, // 合并连续的 isset
    'combine_consecutive_unsets' => true, // 合并连续的 unset
    'declare_equal_normalize' => ['space' => 'single'], // declare 中等号加一个空格
    'elseif' => true, // 用 elseif 代替 else if
    'full_opening_tag' => true, // PHP代码必须用 <?php 或 <?=
    'function_declaration' => ['closure_function_spacing' => 'one'], // 闭包函数后加一个空格
    'function_typehint_space' => true, // 闭包函数类型提示后加空格
    'general_phpdoc_annotation_remove' => ['annotations' => []], // 移除指定的注解
    'heredoc_to_nowdoc' => true, // 无变量时转为 nowdoc
    'linebreak_after_opening_tag' => true, // <?php 标签后不允许代码
    'lowercase_cast' => true, // 数据类型转换小写
    'constant_case' => true, // true, false, null 小写
    'lowercase_keywords' => true, // PHP关键字小写
    'new_with_braces' => true, // new 实例后跟括号
    'no_blank_lines_after_phpdoc' => true, // phpdoc 后不应该有空行
    'no_empty_comment' => true, // 不应该有空注释
    'no_empty_phpdoc' => true, // 不应该有空的 phpdoc
    'no_leading_import_slash' => true, // use 语句取消前置斜杠
    'no_mixed_echo_print' => ['use' => 'echo'], // 不混用 echo 和 print
    'no_multiline_whitespace_around_double_arrow' => true, // => 不应被多行空格包围
    'no_null_property_initialization' => true, // 属性不能显式初始化 null
    'echo_tag_syntax' => true, // 用 <?php echo 代替 <?=
    'no_spaces_after_function_name' => true, // 函数和左括号之间不允许空格
    'no_spaces_inside_parenthesis' => true, // 括号内不允许空格
    'no_trailing_comma_in_singleline' => true, // 单行数组最后不应有逗号
    'no_trailing_whitespace' => true, // 删除行末尾的尾随空格
    'no_trailing_whitespace_in_comment' => true, // 注释中无尾随空格
    'no_unneeded_control_parentheses' => ['statements' => ['break', 'clone', 'continue', 'echo_print', 'return', 'switch_case', 'yield']], // 删除不需要的控制语句括号
    'no_unneeded_curly_braces' => true, // 删除不需要的花括号
    'no_unneeded_final_method' => true, // 终态类不能有终态方法
    'no_whitespace_before_comma_in_array' => true, // 数组声明中逗号前无空格
    'not_operator_with_space' => false, // 不强制逻辑 NOT 运算符空格
    'not_operator_with_successor_space' => false, // 不强制逻辑 NOT 运算符尾随空格
    'phpdoc_align' => ['tags' => ['param', 'return', 'throws', 'type', 'var']], // PHPDoc 标签左对齐
    'phpdoc_annotation_without_dot' => true, // PHPDoc 描述不以句号结尾
    'phpdoc_indent' => true, // Docblock 与文档缩进一致
    'phpdoc_no_empty_return' => true, // 省略 @return void/null
    'phpdoc_no_package' => false, // 不省略 @package 注释
    'phpdoc_order' => false, // 不对 PHPDoc 注释排序
    'phpdoc_separation' => true, // PHPDoc 注释组合在一起
    'phpdoc_single_line_var_spacing' => true, // 单行 @var PHPDoc 间距
    'phpdoc_summary' => false, // PHPDoc 摘要不强制句号结尾
    'phpdoc_var_without_name' => true, // @var 和 @type 不含变量名
    'single_blank_line_at_eof' => true, // 文件结尾单个空行
    'single_line_after_imports' => true, // use 语句后空一行
    'space_after_semicolon' => ['remove_in_empty_for_expressions' => true], // 分号后空格
    'switch_case_semicolon_to_colon' => true, // case 之后用冒号
    'switch_case_space' => true, // case 冒号和值之间无空格
    'ternary_operator_spaces' => true, // 三元操作符标准空格
    'trim_array_spaces' => true, // 数组格式化无前导或尾随空格
    'unary_operator_spaces' => true, // 一元运算符紧邻操作数
    'whitespace_after_comma_in_array' => true, // 数组中逗号后空格
    'nullable_type_declaration_for_default_null_value' => true, // 可空类型声明
    'multiline_whitespace_before_semicolons' => ['strategy' => 'no_multi_line'], // 禁止分号前多行空格
];

$finder = Finder::create()
    ->in([
        __DIR__ . '/tests',
    ])
    ->name('*.php')
    ->notName('*.blade.php')
    ->ignoreDotFiles(true)
    ->ignoreVCS(true);

return (new Config())
    ->setFinder($finder)
    ->setRules($rules)
    ->setRiskyAllowed(true)
    ->setUsingCache(true);

4.选定需要执行的目录运行php-cs-fixer

5.设置快捷键

6.查看效果

# https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v3.0.0/UPGRADE-v3.md#renamed-ru 规则改名
# https://medium.com/@valeryan/how-to-configure-phpstorm-to-use-php-cs-fixer-1844991e521f phpstorm设置cs
# https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v3.0.0/doc/ruleSets/index.rst 规则说明
所有用户都可以去薅羊毛,192元充值200元话费!先到先得!导航栏话费充值,正规可靠,快充慢充自由选择。
欧阳逸资源站 » phpstorm使用php-cs-fixer

发表评论