安装friendsofphp/php-cs-fixer
随便指定一个位置 新建 tools/php-cs-fixer文件夹,如E:\tools\php-cs-fixercomposer require --working-dir=tools/php-cs-fixer friendsofphp/php-cs-fixer
配置格式化配置文件,放在tools/php-cs-fixer文件夹下,文件名为.php-cs-fixer.dist.php
$header = <<<'EOF'This file is part of PHP CS Fixer.(c) Fabien Potencier <fabien@symfony.com>Dariusz Rumiński <dariusz.ruminski@gmail.com>This source file is subject to the MIT license that is bundledwith this source code in the file LICENSE.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(['@PHP74Migration' => true,'@PSR2' => true,'@PHPUnit60Migration:risky' => true,'@PhpCsFixer' => true,'list_syntax' => ['syntax' => 'long'],'align_multiline_comment' => ['comment_type' => 'phpdocs_only'],'array_indentation'=>true,'single_quote' => true, //简单字符串应该使用单引号代替双引号;'no_unused_imports' => true, //删除没用到的use'no_singleline_whitespace_before_semicolons' => true, //禁止只有单行空格和分号的写法;'self_accessor' => true, //在当前类中使用 self 代替类名;'no_empty_statement' => true, //多余的分号// 'no_extra_consecutive_blank_lines' => ['extra'], //多余空白行'no_blank_lines_after_class_opening' => true, //类开始标签后不应该有空白行;'include' => true, //include 和文件路径之间需要有一个空格,文件路径不需要用括号括起来;'no_trailing_comma_in_list_call' => true, //删除 list 语句中多余的逗号;'no_leading_namespace_whitespace' => true, //命名空间前面不应该有空格;'array_syntax' => array('syntax' => 'short'), //数组 【】 php版本大于5.4;'no_blank_lines_after_phpdoc' => true, //PHP 文档块开始开始元素下面不应该有空白行;'object_operator_without_whitespace' => true, //(->) 两端不应有空格;'phpdoc_indent' => true, //phpdoc 应该保持缩进;'phpdoc_no_access' => true, //@access 不应该出现在 phpdoc 中;'phpdoc_no_package' => true,'phpdoc_to_comment' => true, //文档块应该都是结构化的元素;'phpdoc_trim' => true,'phpdoc_var_without_name' => true, //@var 和 @type 注释中不应该包含变量名;'no_leading_import_slash' => true, //删除 use 前的空行;'no_trailing_comma_in_singleline_array' => true, //PHP 单行数组最后一个元素后面不应该有空格;'single_blank_line_before_namespace' => true,//命名空间声明前应该有一个空白行;'binary_operator_spaces' => array('operators'=>['=' => 'align','+=' => 'align_single_space','===' => 'align_single_space_minimal','|' => 'no_space','=>' =>'align_single_space_minimal_by_scope']), //等号 => 对齐 symfony是不对齐的'cast_spaces' => true, //变量和修饰符之间应该有一个空格;'standardize_not_equals' => true, //使用 <> 代替 !=;'concat_space' => array('spacing' => 'one'), //点连接符左右两边有一个的空格;symfony是没空格'ternary_operator_spaces' => true, //三元运算符之间的空格标准化'trim_array_spaces' => true, //数组需要格式化成和函数/方法参数类似,上下没有空白行;'unary_operator_spaces' => true, //一元运算符和运算数需要相邻;'no_whitespace_in_blank_line' => true, //删除空白行中多余的空格;'multiline_whitespace_before_semicolons' => false, //分号前的空格// 'native_function_invocation'=>true,'combine_consecutive_issets' => true,'elseif'=>true,'combine_consecutive_unsets' => true, //多个unset,合并成一个// one should use PHPUnit methods to set up expected exception instead of annotations//'header_comment' => array('header' => $header), //添加,替换或者删除 header 注释。'heredoc_to_nowdoc' => true, //删除配置中多余的空行和/或者空行。'no_extra_blank_lines' => true, //在函数参数中,不能有默认值在非缺省值之前的参数。有风险'no_useless_else' => true, //删除无用的eles'no_useless_return' => true, //删除函数末尾无用的return'no_empty_phpdoc' => true, // 删除空注释'no_spaces_inside_parenthesis' => true, //删除括号后内两端的空格'no_trailing_whitespace' => true, //删除非空白行末尾的空白'no_whitespace_before_comma_in_array' => true, //删除数组声明中,每个逗号前的空格'ordered_class_elements' => false, //class elements排序'ordered_imports' => false, // use 排序'phpdoc_add_missing_param_annotation' => true, //添加缺少的 Phpdoc @param参数// 'phpdoc_trim_consecutive_blank_line_separation' => true, //删除在摘要之后和PHPDoc中的描述之后,多余的空行。'phpdoc_order' => true,'psr_autoloading' => true,// 'strict_comparison' => true, //严格比较,会修改代码有风险//'strict_param' => true,'ternary_to_null_coalescing' => true, //尽可能使用null合并运算符??。需要PHP> = 7.0。'whitespace_after_comma_in_array' => true, // 在数组声明中,每个逗号后必须有一个空格])->setFinder($finder);// special handling of fabbot.io service if it's using too old PHP CS Fixer versionif (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;
打开phpstorm 设置->tools->external tools,新建工具
名称,描述自己自定义Tool SettinsProgram:E:\tools\php-cs-fixer\vendor\bin\php-cs-fixer.batArguments: fix $FileDir$/$FileName$ --config=E:\tools\php-cs-fixer\.php-cs-fixer.dist.phpWorking directory: $ProjectFileDir$高级选项里,我们把 Open console for tool output取消勾选掉,省的每回都弹出一个终端出来
设置->Keymap,找到External Tools找到你刚才新建的外部工具,双击添加键盘快捷键