VsCode插件巡礼-phpcs

不羁的心 提交于 2020-11-01 14:35:34

我们希望规范项目开发中PHP代码的编码规范,锁定了php-code-sniffer这款插件,在安装执行过程中遇到的问题及理解分享如下


认识PHP_CodeSniffer

https://github.com/squizlabs/PHP_CodeSniffer/是一个代码规范工具,用于规范团队开发中的类名,注释,参数的等方面写法,帮助团队成员形成统一的编码风格。


有多种安装方式,mac下采用以下命令安装,最为方便

brew install php-code-sniffer


安装路径 /usr/local/Cellar/php-code-sniffer

/usr/local/Cellar/php-code-sniffer/3.4.1/binlsphpcbf  phpcs


composer方式

composer global require squizlabs/php_codesniffer

安装路径

/Users/name/.composer/vendor/squizlabs/php_codesniffer/


lsphpcbf    phpcbf.bat  phpcs    phpcs.bat


两种安装方式,mac下推荐使用brew安装

其它安装方式参考github库[PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer/)


VsCode中启用PHP_CodeSniffer


VsCode中启用PHP_CodeSniffer需要安装phpcs插件。


phpcs与PHP sniffer关系


Before using this plugin, you must ensure that phpcs is installed on your system,The preferred method is using [composer](https://getcomposer.org/) for both system-wide and project-wide installations.


也就是说在VsCode中使用phpcs插件之前,需要首先在系统上安装有phpcs,安装方法是依赖于composer方式,并且可以从系统级和项目级两个纬度选择安装。


如果只是在vscode中安装插件,系统级中不安装phpcs。会出现以下错误


phpcs: Request workspace/configuration failed with message: Unable to locate phpcs. Please add phpcs to your global path or use composer dependency manager to install it in your project locally.




系统级全局安装

composer global require squizlabs/php_codesniffer


Changed current directory to /Users/wmm/.composer

Using version ^3.4 for squizlabs/php_codesniffer

./composer.json has been updated

Loading composer repositories with package information

Updating dependencies (including require-dev)

Package operations: 1 install, 0 updates, 0 removals

  - Installing squizlabs/php_codesniffer (3.4.0): Downloading (100%)         

Writing lock file

Generating autoload files


项目级别的安装

以下是VScode 对于插件 phpcs 的介绍说明,更多参考 https://marketplace.visualstudio.com/items?itemName=ikappas.phpcs 

Project-wide Installation


The `phpcs` linter can be installed in your project using the Composer Dependency Manager for PHP.


1.  Install [composer](https://getcomposer.org/doc/00-intro.md "https://getcomposer.org/doc/00-intro.md").


2.  Require `phpcs` package by typing the following at the root of your project in a terminal:


composer require --dev squizlabs/php_codesniffer

   

在没有配置完成时,又发现了另外一个插件 PHP Sniffer。在特性中写着两条


Runs phpcs to lint PHP code.

Runs phpcbf to format fixable PHP code validation errors.


显然可以推断 PHP Sniffer 是对phpcs的封装。验证后发现,这个插件可以不使用,PHP_CodeSniffer也能够正常启用。


![VsCode插件巡礼-phpcs-图南科技]


如果同时安装 PHP Sniffer 和 phpcs插件,但是没有安装PHP_CodeSniffer会出现以下错误

phpcs: Unable to locate phpcs. Invalid version string encountered!


PHP_CodeSniffer典型使用

phpcs -i
The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz and Zend



使用规范

1 方法注释 参数,返回值三项用空行分割


![phpcs.png]


2 参数必须有注释,也识别空格,多个参数纵向需要对齐。


3 每一行的代码长度不能超过一定的长度。


>Line exceeds 85 characters; contains 98 characters


4 私有方法使用下划线开始。


>Private method name " " must be prefixed with an underscore



总结

VsCode中对于插件phpcs安装方式的介绍,实际上更多是PHP_CodeSniffer工具的安装介绍,插件的作用就是帮助项目与PHP_CodeSniffer做关联。要体验到完善的phpcs使用体验,还需要PHP智能注释这个插件,自动为方法提供注释,再手动做细节调整。


Code Review是保证团队代码质量的重要措施之一,而代码规范是Code Review的重要一环,而代码规范又要借助于有效的工具,phpcs就是这样的工具,推动代码整洁,高效开发,培养工程师对于代码整洁度的洁癖和质量交付的严格约束。


----------------------------------------

文章已同步到公众号《图南科技》欢迎关注



本文分享自微信公众号 - 图南日晟(tunan_technology)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!