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.设置规则和扫描的目录
<?php
use PhpCsFixerConfig;
use PhpCsFixerFinder;
$rules = [
'@PHP80Migration' => true,
'ordered_imports' => [
'sort_algorithm' => 'alpha',
],
'class_attributes_separation' => [
'elements' => [
'const' => 'one',
'method' => 'one',
'property' => 'one',
],
],
'@PSR12' => true,
'@PHPUnit100Migration:risky' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'list_syntax' => ['syntax' => 'long'],
'encoding' => true,
'line_ending' => true,
'array_syntax' => ['syntax' => 'short'],
'single_quote' => true,
'self_accessor' => true,
'binary_operator_spaces' => ['default' => 'single_space'],
'include' => true,
'standardize_not_equals' => true,
'no_closing_tag' => true,
'no_unused_imports' => true,
'no_singleline_whitespace_before_semicolons' => true,
'no_empty_statement' => true,
'no_unreachable_default_argument_value' => true,
'no_blank_lines_after_class_opening' => true,
'no_leading_namespace_whitespace' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'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,
'increment_style' => false,
'align_multiline_comment' => ['comment_type' => 'phpdocs_only'],
'blank_line_after_namespace' => true,
'blank_line_after_opening_tag' => false,
'blank_line_before_statement' => ['statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try']],
'cast_spaces' => ['space' => 'none'],
'combine_consecutive_issets' => true,
'combine_consecutive_unsets' => true,
'declare_equal_normalize' => ['space' => 'single'],
'elseif' => true,
'full_opening_tag' => true,
'function_declaration' => ['closure_function_spacing' => 'one'],
'function_typehint_space' => true,
'general_phpdoc_annotation_remove' => ['annotations' => []],
'heredoc_to_nowdoc' => true,
'linebreak_after_opening_tag' => true,
'lowercase_cast' => true,
'constant_case' => true,
'lowercase_keywords' => true,
'new_with_braces' => true,
'no_blank_lines_after_phpdoc' => true,
'no_empty_comment' => true,
'no_empty_phpdoc' => true,
'no_leading_import_slash' => true,
'no_mixed_echo_print' => ['use' => 'echo'],
'no_multiline_whitespace_around_double_arrow' => true,
'no_null_property_initialization' => true,
'echo_tag_syntax' => true,
'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_operator_with_successor_space' => false,
'phpdoc_align' => ['tags' => ['param', 'return', 'throws', 'type', 'var']],
'phpdoc_annotation_without_dot' => true,
'phpdoc_indent' => true,
'phpdoc_no_empty_return' => true,
'phpdoc_no_package' => false,
'phpdoc_order' => false,
'phpdoc_separation' => true,
'phpdoc_single_line_var_spacing' => true,
'phpdoc_summary' => false,
'phpdoc_var_without_name' => true,
'single_blank_line_at_eof' => true,
'single_line_after_imports' => true,
'space_after_semicolon' => ['remove_in_empty_for_expressions' => true],
'switch_case_semicolon_to_colon' => true,
'switch_case_space' => true,
'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 规则说明