您的位置:首页 > 编程语言 > PHP开发

vscode 中使用php-cs-fixer和PHP Formatter 插件规范化PHP代码

2017-03-23 11:17 701 查看
什么是PHP-CS-Fixer?
它是php-fig组织定义的PHP代码规范,良好的代码规范可以提高代码可读性,团队沟通维护成本
使用它可以按照指定的规范格式化您的PHP代码,此工具不仅可以检测有不符合规范的代码,而且还可以修复它们



1.使用php-cs-fixer.phar格式化php文件
1.下载php-cs-fixer.phar到任意目录,尽量放到php目录下)
2.在VScode里手动调用php-cs-fixer.phar进行格式化



2.在Vscode里PHP Formatter 插件
1.下载php-cs-fixer.phar到任意目录,尽量放到php目录下)
2.安装PHP Formatter 插件并进行自定义设置(php路径、php-cs-fixer.phar路径,Rules等)
3.自定义PHP Formatter 插件的快捷键



配置信息如下:

//打印日志信息,用于调试
"phpformatter.logging":true,
//不使用composer方式
"phpformatter.composer":false,
//添加自定义参数,默认的参数level已经在新版本中移出所以会导致运行出错
//RULES=[@PSR1,@PSR2,@Symfony]
//source:https://github.com/FriendsOfPHP/PHP-CS-Fixer#usage
"phpformatter.arguments":["--rules=@Symfony"],
// Should point to php-cs-fixer.phar file, if you have installed this manually (without Composer). Should include .phar extension.
// php-cs-fixer.phar路径,使用composer方式时可以不填
"phpformatter.pharPath":"d:/wamp/bin/php/php5.5.12/php-cs-fixer.phar",
// If the pharPath is set, and you are not using Composer, and you haven't added PHP to your PATH, this should point to the php.exe file.
// php路径,使用composer方式时可以不填
"phpformatter.phpPath":"d:/wamp/bin/php/php5.5.12/php.exe"


VScode自定义快捷键配置





参考:
PHP-CS-Fixer:格式化你的PHP代码
vscode-php-formatter issues
使用 PHP-CS-Fixer 自动规范化你的 PHP 代码
VSCode安装php格式化工具php formatter
github PHP-CS-Fixer
vscode plugin PHP Formatter
PHP PSR-2 代码风格规范(中文版) -SegmentFault
PSR-2: Coding Style Guide
php-cs-fixer (PHP Coding Standards Fixer)

From WizNote
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: