Coding Standards (Drupal代码检测)
更新:HHH   时间:2023-1-7


本文内容主要是Drupal开发中代码检测,简单罗列了Coder和PHPCS的安装和使用的步骤。如需详情请参看Drupal官方文档。

本文参考文档:

Coder https://www.drupal.org/project/coder/

Installing Coder Sniffer https://www.drupal.org/node/1419988

Command Line Usage https://www.drupal.org/node/1587138


Coder可以检查你的Drupal代码,与编码标准(coding standards)和其他最佳写法对比。它可以从php_codesniffer使用phpcbf命令,修复违反编码标准的代码。这试用于所有版本的Drupal,所以你也可以使用Coder8.x-2.x去检查Drupal7的代码。

Coder不是一个模块,它是用来作为命令行工具和集成到开发IDE中使用的。

Usage

phpcs --standard=Drupal example.module

FILE: /home/klausi/workspace/coder/example.module
--------------------------------------------------------------------------------
FOUND 5 ERRORS AFFECTING 5 LINES
--------------------------------------------------------------------------------
  1 | ERROR | [ ] Missing file doc comment
  3 | ERROR | [ ] Missing function doc comment
  4 | ERROR | [ ] Opening brace should be on the same line as the declaration
  5 | ERROR | [x] Line indented incorrectly; expected 2 spaces, found 1
  6 | ERROR | [x] Line indented incorrectly; expected 2 spaces, found 3
--------------------------------------------------------------------------------
PHPCBF CAN FIX THE 2 MARKED SNIFF VIOLATIONS AUTOMATICALLY
--------------------------------------------------------------------------------

Installing Coder Sniffer

PHP_CodeSniffer是一个用于标记PHP,JavaScript和CSS文件并监测违反编码标准定义的编码标准集库。(After you install coder Sniffer ,see command line options for running it here

Coder 包含PHP CodeSniffer的“嗅探器”。这些“嗅探器”告诉PHP CodeSniffer代码是否符合Drupal编码标准。具体说,有两个规则集,Drupal 和 DrupalPractice.前者旨在强制通用标准,而后者针对希望避免常见错误的模块开发人员。

1. Install PHP CoderSniffer

2. Install Coder

3. Register Drupal standards in PHP Codersniffer

4. Run the sniffs

你可以通过Composer安装Coder Sniffer,着将自动安装PHPCS及其依赖项。

这里我们采用Composer安装。

建议将编码器安装在一个中心的位置,因为我们无需为每个Drupal站点下载安装。

Install Coder and PHPCS with Composer

1. 确认是否安装Composer:

$ which composer
/usr/local/bin/composer
$ composer -V
Composer version 1.5.6 2017-12-18 12:09:18

composer's installation instructions

Global Coder Install

1. Install Coder(8.x-2.x)in your global Composer directory in your home directory

$ composer global require drupal/coder

在大多数系统上,Coder将安装在~/ .composer/vendor/drupal/coder 目录中

2. You can check the installed location by doing:

$ composer global show -P
// ...
drupal/coder                                   /home/notroot/.composer/vendor/drupal/coder
// ...

3. 设置$PATH变量

To make the phpcs and phpcbf commands avaliable globally, add those to your $PATH variable in ~/.profile,~/.bash_profile,~/.bashrc or ~/.zshrc.

export PATH="$PATH:$HOME/.composer/vendor/bin"

Register Coder Standards

PHPCS 附带一些已经注册的标准。上面暗转了Coder Sniffer,我们需要注册Drupal和DrupalPractice标准,以便PHPCS可以使用它们。我们使用Composer安装。

Composer Installer Plugin

$ composer global require drupal/coder:^8.2.12
$ composer global require dealerdirect/phpcodesniffer-composer-installer

上面的安装命令会返回

PHP CodeSniffer Config installed_paths set to ~/.composer/vendor/drupal/coder/coder_sniffer


Verify Registered Standards

$ phpcs -i
The installed coding standards are Zend, PEAR, PSR2, MySource, PHPCS, Squiz, PSR1, DrupalPractice and Drupal

Runing PHP CodeSniffer


Check Drupal coding standards

$ phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /file/to/drupal/example_module

上面命令最好将结果导出到一个文档中,方便我们翻看(如果你自信你的编码,可以不用):

$ phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md /file/to/drupal/example_module > codecheck.txt

Check Drupal coding standards and ignore composer and node.js directories

$ phpcs --standard=Drupal --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md --ignore=node_modules,bower_components,vendor /file/to/drupal/example_module > codecheck.tex


还有其他PHPCS命令的使用方式以及设置别名方便使用的方法,请查看Drupal官方文档


返回web开发教程...