博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
windows环境PhpStorm中简单使用PHP_CodeSniffer规范php代码
阅读量:6889 次
发布时间:2019-06-27

本文共 3216 字,大约阅读时间需要 10 分钟。

为什么使用PHP_CodeSniffer

一个开发团队统一的编码风格,有助于他人对代码的理解和维护,对于大项目来说尤其重要。

PHP_CodeSniffer是PEAR中的一个用PHP5写的用来检查嗅探PHP代码是否有违反一组预先设置好的编码标准的一个包,它是确保你的代码简洁一致的必不可少的开发工具,甚至还可以帮助程序员减少一些语义错误。

什么是Pear

由于PHP_CodeSniffer的安装依赖PHP和Pear环境,那么我们有必要了解下什么是Pear。

来自:

PEAR是PHP扩展与应用库(the PHP Extension and Application Repository)的缩写。它是一个PHP扩展及应用的一个代码仓库,简单地说,PEAR之于PHP就像是CPAN(Comprehensive Perl Archive Network)之于Perl。
PEAR的基本目标是发展成为PHP扩展和库代码的知识库,而这个项目最有雄心的目标则是试图定义一种标准,这种标准将帮助开发者编写可移植、可重用的代码。

安装Pear

在已经安装了PHP环境的前提下,进入php目录,如果没有go-pear.php文件,就到下载go-pear.php文件,该地址在浏览器打开可以看到一段PHP的代码,直接保存文件另存为go-pear.php到php根目录下面。

使用管理员方式打开命令行,输入以下命令:

1 cd c:\php2 php go-pear.phar

 

这是出现:

1 Are you installing a system-wide PEAR or a local copy?2 (system|local) [system] :

 

直接回车默认system继续,出现如下:

1 Below is a suggested file layout for your new PEAR installation.  To 2 change individual locations, type the number in front of the 3 directory.  Type 'all' to change all of them or simply press Enter to 4 accept these locations. 5  6  1. Installation base ($prefix)                   : C:\php 7  2. Temporary directory for processing            : C:\php\tmp 8  3. Temporary directory for downloads             : C:\php\tmp 9  4. Binaries directory                            : C:\php10  5. PHP code directory ($php_dir)                 : C:\php\pear11  6. Documentation directory                       : C:\php\docs12  7. Data directory                                : C:\php\data13  8. User-modifiable configuration files directory : C:\php\cfg14  9. Public Web Files directory                    : C:\php\www15 10. System manual pages directory                 : C:\php\man16 11. Tests directory                               : C:\php\tests17 12. Name of configuration file                    : C:\WINDOWS\pear.ini18 13. Path to CLI php.exe                           : C:\php19 20 1-13, 'all' or Enter to continue:

 

直接回车,出现如下,表示安装成功,

/*省略*/The 'pear' command is now at your service at c:\php\pear.bat/*省略*/

在php根目录下面会看到如下几个文件:

双击pear.bat文件,注册pear到当前环境。

安装PHP_CodeSniffer

在安装完pear之后,就可以安装php_CodeSniffer了,继续在cmd中输入:

1 pear install PHP_CodeSniffer

等待安装完成,安装完成后php根目录下回出现以下两个文件:

按照下图依次打开文件夹,在看如下目录结构:

在php->pear->PHP->CodeSniffer->Standards中可以看到一些php的规范,Generic是通用规范。

现在我们就可以使用这些规范来检测我们的php代码了,先说说在命令行中如何使用。

我们可以使用phpcs -h来看看使用帮助:

phpcs -h

 看到的如下:

这里我只简单的说明如何检查单个文件或整个文件目录:

1 phpcs -n F:\Hg\web\application\controllers\  //检测文件目录2 phpcs -n F:\Hg\web\application\controllers\home_controller.php  //检测单个文件

 看到如下结果(单个文件):

这样,我们就可以根据这些错误信息去修改我们的代码,使其符合规范。

我们可以指定使用某一个规范进行检测,方法如下:

1 phpcs -n --standard=Zend F:\Hg\web\application\controllers\

 

不指定标准,会使用php通用规范Generic。

安装CodeIgniter标准

去下载包解压,复制src目录到php->pear->PHP->CodeSniffer->Standards目录下,并且改名为CodeIgniter

上图为解压后图

上图为放到php代码规范下后的图。

现在就可以使用CodeIgniter标准检测代码了:

1 phpcs -n --standard=CodeIgniter F:\Hg\web\application\controllers\

 

PHPSTORM配置PHP_CodeSniffer检测环境

 打开phpstorm的配置框,找到Languages & Frameworks -> php-> Code Sniffer,不同版本的phpstorm可能会有出入,直接搜索Code Sniffer也可以。

点击如下进行编辑:

设置PHP Code Sniffer path为phpcs.bat的路径。

 点击Validate,出现如下图表示设置成功:

打开配置搜索Inspections, 展开PHP,勾选PHP Code Sniffer validation, 选择Coding standard为CodeIgniter, 点击OK确定。

接下来,在编码PHP的时候就会出现规范提示

 

如上图,鼠标移动到有波浪提示的地方,就会出现phpcs的规范提示了。

配置到此结束,希望可以帮到需要的程序猿!

 

最规范的代码就是不出现任何的波浪提示。

 

 

参考:

转载地址:http://kyabl.baihongyu.com/

你可能感兴趣的文章
Functor and Monad in Swift
查看>>
linux调试工具glibc的演示分析-core dump double free【转】
查看>>
Top 22 Free Responsive HTML5 Admin & Dashboard Templates 2018
查看>>
适合初学者的python实际例子
查看>>
我的第一个python web开发框架(25)——定制ORM(一)
查看>>
Android padding 和margin
查看>>
IOS UIView 01-View开始深入 绘制像素到屏幕上
查看>>
在Android中使用Protocol Buffers(中篇)
查看>>
Apache之Rewrite和RewriteRule规则梳理以及http强转https的配置总结
查看>>
Python3+pyshark捕获数据包并保存为文件
查看>>
Unable to launch the IIS Express Web server
查看>>
怎样使用EOS.JS的API
查看>>
Spring Boot项目配置RabbitMQ集群
查看>>
口语详解|为什么“how to say”是错的?
查看>>
前端工程化系列[05] Yeoman脚手架使用入门
查看>>
MySQL必会的28条经典查询
查看>>
彻底删除Cygwin
查看>>
一次单核CPU占用过高问题的处理
查看>>
如何让自己的电脑发布ASP http://jingyan.baidu.com/article/19192ad853224ce53f570748.html
查看>>
SQL基础
查看>>