在phpStrom中安装php代码格式化插件Php-cs-fixer
由于phpStrom原来的插件不再开源,现在转为使用Php-cs-fixer格式化代码。以下为在phpStrom中安装Php-cs-fixer的具体步骤。
- 安装
安装很简单,下载php-cs-fixer.phar文件就行了。
官方地址是:
http://get.sensiolabs.org/php-cs-fixer.phar
github地址:
https://github.com/FriendsOfPHP/PHP-CS-Fixer
或者
可以使用百度云:http://pan.baidu.com/s/1kVAZku7
(推荐使用gitHub,并按照其README中的使用方法使用,因为版本更新后参数选项可能会变) - 添加
进入phpStrom Settings -> Tools -> External Tools
点击添加
参数解读
Program:你的php编译程序的路径
Parameters: D:\JetBrains\php-cs-fixer\php-cs-fixer.phar fix --config-file .php_cs $FileDir$\$FileName$
其中:D:\JetBrains\php-cs-fixer\php-cs-fixer.phar是你步骤1中你下载的php-cs-fixer的路径
--config-file .php_cs使用项目目录下的.php_cs模板也可以使用下面的参数自己指定psr2或者其他标准格式化代码
(D:\JetBrains\php-cs-fixer\php-cs-fixer.phar fix —level=psr2 —verbose —config=sf23 $FileDir$\$FileName$) -
Working Directory: 工作目录 $ProjectFileDir$
- 配置phpStrom快捷键
进入phpStrom-> Settings->Appearance & Behavior -> Keymap
使用查找 php-cs-fixer
添加格式化时使用的快捷键,我这里使用的是Alt+Shift+Z,你可以设定你自己的快捷键。
安装完成,现在当你使用phpStrom便捷文件想格式化代码时,你就可以使用你设定的快捷键格式化代码
效果图:
参考文档: http://tzfrs.de/2015/01/automatically-format-code-to-match-psr-standards-with-phpstorm/
注意:
不过使用这篇文档的配置时会报错:%1不是有效的Win32应用程序
貌似这个作者没有遇到这种情况。