CodingStandards(Drupal代碼檢測(cè))-創(chuàng)新互聯(lián)

本文內(nèi)容主要是Drupal開發(fā)中代碼檢測(cè),簡(jiǎn)單羅列了Coder和PHPCS的安裝和使用的步驟。如需詳情請(qǐng)參看Drupal官方文檔。

成都創(chuàng)新互聯(lián)公司專注于王屋網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供王屋營(yíng)銷型網(wǎng)站建設(shè),王屋網(wǎng)站制作、王屋網(wǎng)頁設(shè)計(jì)、王屋網(wǎng)站官網(wǎng)定制、小程序制作服務(wù),打造王屋網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供王屋網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

本文參考文檔:

Coderhttps://www.drupal.org/project/coder/

Installing Coder Snifferhttps://www.drupal.org/node/1419988

Command Line Usagehttps://www.drupal.org/node/1587138

Coder可以檢查你的Drupal代碼,與編碼標(biāo)準(zhǔn)(coding standards)和其他最佳寫法對(duì)比。它可以從php_codesniffer使用phpcbf命令,修復(fù)違反編碼標(biāo)準(zhǔn)的代碼。這試用于所有版本的Drupal,所以你也可以使用Coder8.x-2.x去檢查Drupal7的代碼。

Coder不是一個(gè)模塊,它是用來作為命令行工具和集成到開發(fā)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是一個(gè)用于標(biāo)記PHP,JavaScript和CSS文件并監(jiān)測(cè)違反編碼標(biāo)準(zhǔn)定義的編碼標(biāo)準(zhǔn)集庫(kù)。(After you install coder Sniffer ,see command line options for running it here)

Coder 包含PHP CodeSniffer的“嗅探器”。這些“嗅探器”告訴PHP CodeSniffer代碼是否符合Drupal編碼標(biāo)準(zhǔn)。具體說,有兩個(gè)規(guī)則集,Drupal 和 DrupalPractice.前者旨在強(qiáng)制通用標(biāo)準(zhǔn),而后者針對(duì)希望避免常見錯(cuò)誤的模塊開發(fā)人員。

1. Install PHP CoderSniffer

2. Install Coder

3. Register Drupal standards in PHP Codersniffer

4. Run the sniffs

你可以通過Composer安裝Coder Sniffer,著將自動(dòng)安裝PHPCS及其依賴項(xiàng)。

這里我們采用Composer安裝。

建議將編碼器安裝在一個(gè)中心的位置,因?yàn)槲覀儫o需為每個(gè)Drupal站點(diǎn)下載安裝。

Install Coder and PHPCS with Composer

1. 確認(rèn)是否安裝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

在大多數(shù)系統(tǒng)上,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. 設(shè)置$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 附帶一些已經(jīng)注冊(cè)的標(biāo)準(zhǔn)。上面暗轉(zhuǎn)了Coder Sniffer,我們需要注冊(cè)Drupal和DrupalPractice標(biāo)準(zhǔn),以便PHPCS可以使用它們。我們使用Composer安裝。

Composer Installer Plugin

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

上面的安裝命令會(huì)返回

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

上面命令最好將結(jié)果導(dǎo)出到一個(gè)文檔中,方便我們翻看(如果你自信你的編碼,可以不用):

$ 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命令的使用方式以及設(shè)置別名方便使用的方法,請(qǐng)查看Drupal官方文檔。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

文章名稱:CodingStandards(Drupal代碼檢測(cè))-創(chuàng)新互聯(lián)
文章地址:http://www.muchs.cn/article22/dsoicc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供小程序開發(fā)網(wǎng)站設(shè)計(jì)、服務(wù)器托管、網(wǎng)站改版、自適應(yīng)網(wǎng)站網(wǎng)站策劃

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)頁設(shè)計(jì)公司