温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

VsCode插件phpcs安装执行过程中遇到的问题及解决方法

发布时间:2021-10-11 11:06:47 来源:亿速云 阅读:507 作者:柒染 栏目:大数据

VsCode插件phpcs安装执行过程中遇到的问题及解决方法,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

我们希望规范项目开发中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.

VsCode插件phpcs安装执行过程中遇到的问题及解决方法

系统级全局安装

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安装执行过程中遇到的问题及解决方法

![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 方法注释 参数,返回值三项用空行分割

VsCode插件phpcs安装执行过程中遇到的问题及解决方法

![phpcs.png]

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

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

>Line exceeds 85 characters; contains 98 characters

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

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

VsCode插件phpcs安装执行过程中遇到的问题及解决方法

总结

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

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

看完上述内容,你们掌握VsCode插件phpcs安装执行过程中遇到的问题及解决方法的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI