vscode配置php php-cs,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.

133b4c1af7894853aa91b8ba44dc5596.png

系统级全局安装

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也能够正常启用。

0f449edf2acd14b0f34324a29e4b578e.png

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

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

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

PHP_CodeSniffer典型使用

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

使用规范

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

2687f6f0b3c788cb7e48422171e5e847.png

![phpcs.png]

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

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

>Line exceeds 85 characters; contains 98 characters

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

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

a512bba55fde7a802e099382ca1f65c5.png

总结

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

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

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值