使用vscode插件junstyle.php-cs-fixer格式化php文件

# 下载文件放到即可
/usr/local/bin/php-cs-fixer.phar

# 测试运行 php-cs-fixer.phar
PHP CS Fixer 3.8.0 (cbad111) BerSzcz against war! by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.0.17
...
  • 安装junstyle.php-cs-fixer插件到vscode

  • 打开一个项目文件夹,创建index.php进行测试,需要测试通过才能使用快捷键格式化

# 执行
php-cs-fixer.phar fix ./index.php

# 不报错即可 php-cs-fixer.phar fix ./index.php
Loaded config default.

Fixed all files in 0.032 seconds, 12.000 MB memory used
  • 配置自己的配置文件

# 在项目文件夹创建文件
touch ./.php-cs-fixer.php

# 写入
<?php

$finder = PhpCsFixer\Finder::create()
    ->in(__DIR__)
    ->exclude('somedir')
;

$config = new PhpCsFixer\Config();
return $config->setRules([
        '@Symfony' => true,
        'full_opening_tag' => false,
    ])
    ->setFinder($finder)
;

# 再执行, 会提示使用了配置文件
 php-cs-fixer.phar fix ./index.php

Loaded config default from "/home/jcleng/desktop/work/www/swoole_demo/./.php-cs-fixer.php".
Using cache file ".php-cs-fixer.cache".
Paths from configuration file have been overridden by paths provided as command arguments.

Fixed all files in 0.030 seconds, 14.000 MB memory used
  • .php-cs-fixer.php 的全文配置文件实例

<?php

declare(strict_types=1);

$header = <<<'EOF'

EOF;

return (new PhpCsFixer\Config())
    ->setRiskyAllowed(true)
    ->setRules([
        '@PSR2' => true,
        '@Symfony' => true,
        '@DoctrineAnnotation' => true,
        '@PhpCsFixer' => true,
        '@PhpCsFixer:risky' => true,
        'comment_to_phpdoc' => true,
        'return_type_declaration' => true,
        'header_comment' => [
            'comment_type' => 'PHPDoc',
            'header' => $header,
            'separate' => 'none',
            'location' => 'after_declare_strict',
        ],
        'array_syntax' => [
            'syntax' => 'short',
        ],
        'list_syntax' => [
            'syntax' => 'short',
        ],
        'concat_space' => [
            'spacing' => 'one',
        ],
        'blank_line_before_statement' => [
            'statements' => [
                'declare',
            ],
        ],
        'general_phpdoc_annotation_remove' => [
            'annotations' => [
                'author',
            ],
        ],
        'ordered_imports' => [
            'imports_order' => [
                'class', 'function', 'const',
            ],
            'sort_algorithm' => 'alpha',
        ],
        'single_line_comment_style' => [
            'comment_types' => [
            ],
        ],
        'yoda_style' => [
            'always_move_variable' => false,
            'equal' => false,
            'identical' => false,
        ],
        'phpdoc_align' => [
            'align' => 'left',
        ],
        'phpdoc_indent' => true,
        'multiline_whitespace_before_semicolons' => [
            'strategy' => 'no_multi_line',
        ],
        'constant_case' => [
            'case' => 'lower',
        ],
        'class_attributes_separation' => true,
        'combine_consecutive_unsets' => true,
        'declare_strict_types' => true,
        'linebreak_after_opening_tag' => true,
        'lowercase_static_reference' => true,
        'no_useless_else' => true,
        'no_unused_imports' => true,
        'not_operator_with_successor_space' => true,
        'not_operator_with_space' => false,
        'ordered_class_elements' => true,
        'php_unit_strict' => false,
        'phpdoc_separation' => false,
        'single_quote' => true,
        'standardize_not_equals' => true,
        'multiline_comment_opening_closing' => true,
        'phpdoc_add_missing_param_annotation' => [
            'only_untyped' => false,
        ],
        'phpdoc_single_line_var_spacing' => true,
        'phpdoc_trim' => true,
        'align_multiline_comment' => [
            'comment_type' => 'all_multiline',
        ],
        'no_blank_lines_after_phpdoc' => true,
        'no_trailing_whitespace_in_comment' => true,
        // 数组
        'array_indentation' => true,
        'trim_array_spaces' => true,
        'no_trailing_comma_in_singleline_array' => true,
        'no_whitespace_before_comma_in_array' => true,
        'whitespace_after_comma_in_array' => true,
        'whitespace_after_comma_in_array' => true,
    ])
    ->setFinder(
        PhpCsFixer\Finder::create()
            ->exclude('bin')
            ->exclude('public')
            ->exclude('runtime')
            ->exclude('vendor')
            ->in(__DIR__)
    )
    ->setUsingCache(false);