How to Configure Drupal Coding Standards for VS Code Editor
https://www.zyxware.com/sites/default/files/styles/user_image/public/default_images/index.png?itok=2YmREnrP
BY harikrishna.kelappurath
2 years ago
Technical-Solution
0 comments comment

Visual Studio Code is one of the most popular and lite editor software. This article describes how Drupal coding standards can be implemented in Visual Studio Code Editor. VS code (Visual Studio Code) has various plugins to help code. VS code also has plugins for checking coding standards. ’PHPCS', the linter plugin for Visual Studio Code provides an interface to PHP CodeSniffer. It is used with files that have the “PHP” language mode. Linter is any tool that detects and flags errors in programming languages, including stylistic errors. VS code has plugin for PHP CodeSniffer.

How to setup Drupal coding standard in VS Code using PHP CodeSniffer

To implement Drupal coding standard in VS code initially you have to install/update VS code to newest version in your system

 sudo apt-get install code  

Open VS code and install phpcs extensions from extension bar.

1.jpg

CodeSniffer can be installed system wise by using pear and project wise using composer. For installing CodeSniffer we need pear in our system. Install pear if it isn’t installed already.

   sudo apt-get install php-pear

Currently CodeSniffer version greater than 3.0 will not support the Drupal coding standard, so I prefer version 2.9.1, which is PHP_CodeSniffer-2.9.1.

  sudo pear install PHP_CodeSniffer-2.9.1

if you have already installed PHP_CodeSniffer version greater than 3.0, then uninstall that version by sudo pear uninstall PHP_CodeSniffer and install the version 2.9.1.

Install composer if it is not installed yet

 sudo apt install composer

Type composer in terminal to verify installation

2.jpg

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

 sudo composer global require drupal/coder

Check installation location by

 composer global show -P 

Install dealerdirect/phpcodesniffer-composer-installer package

 sudo composer global require dealerdirect/phpcodesniffer-composer-installer

Install CodeSniffer to composer

  sudo composer require --dev squizlabs/php_codesniffer

Or you can simply edit the global composer.json file and update composer

 {
	  “require”:{
	    “squizlabs/php_codesniffer”:”2.9.1”,
    “drupal/coder”:”^8.2”	
  }
}

Set coder path

sudo phpcs --config-set installed_paths  [path of 'drupal/coder’]/coder_sniffer

You can view the path by typing

  composer global show -P

Usually path is

 ~/.composer/vendor/drupal/coder/coder_sniffer


3.jpg

To see registered PHP CodeSniffer coding standards

 phpcs -i


4.jpg

Set Drupal coding standard as default

sudo phpcs --config-set default_standard Drupal


5.jpg

Enable the phpcs plugin in the VS code and reload.

6.jpg

To work in Drupal project, update the composer of the drupal project after all the above steps.

composer update


7.jpg

After updating you can see that in your project Drupal coding standard by CodeSniffer is installed.

View

 your_project_directory/vendor/drupal/coder/coder_sniffer

Then add array elements

‘default_stanedard’ => ‘Drupal’,

'installed_paths' => '../../drupal/coder/coder_sniffer/',

to $phpCodeSnifferConfig array, which you can find in

your_project_directory/vendor/squizlabs/php_codesniffer/CodeSniffer.conf

The file should look like this.

8.jpg


<?php
$phpCodeSnifferConfig = array (
 'installed_paths' => '../../drupal/coder/coder_sniffer/',
 'default_standard' => 'Drupal',
)
?>

Now you can check out coding standard in VS code. An example picture is given. The red underline indication and the popup contain ‘[phpcs]’ shows coding standard error.

9.jpg

Or we can simply type phpcs yourfile_name.php in terminal it will list out all the coding standard errors in the file.

phpcs yourfile_name.php


10.jpg

In Drupal, following coding standards is mandatory. By using Visual Studio Code Editor we can learn and understand Drupal coding standards while working on a project and it will also help to avoid mistakes in coding.


RELATED ARTICLE

/themes/custom/zyxpro_light/images/placeholder.png
close

on 04th April 2007 / by webmaster
Linux is the epitome of the FLOSS model. Technical Solution Linux installation Leave a reply Your email address will not be published. Required fields are marked * Dewey J. Corl (not verified) access_time 29 Nov 2020 - 22:02 Linux is not ready for the general public! (I am a Ubuntu user). For proof, 1. try to simply print labels on a Dymo label printer. It takes a LOT of work to get to the ease of label printing already available in Windows and Mac. 2. try syncing a modern Palm based PDA. Yes, jpilot and others will sync the main data, but pictures and midi files do not get transferred without a lot of extra setup. These are examples of applications that are not ready for the general public. Since an operating system only supports applications, Linux is not ready to be a common desktop for the general public. While we are waiting for the applications to catch up, keep up the good work!! Anoop John (not verified) access_time 29 Nov 2020 - 22:02 Dewey Yes you may be right. But these are not applications that a common man would use. A common man would use one of these applications - Internet browser, Chat Client, Email Client, Document Writer, Presentation Creator, Spreadsheet Editor, Movie Viewer, Music Player, CD/DVD Writer. Both examples you cited are more specific applications that only a small percentage of the whole population uses. It will take time before those hardware vendors identify the need from their perspective to address the Linux community. Krishnadas (not verified) access_time 29 Nov 2020 - 22:02 Dear Mr Anoop, Thank you very much for the free installation of LINUX done in my PC. I am getting immersed in the LINUX and seeing the versatility. To my surprise, I am able to read one of my old backup CD(wherein lot of my valuable file exist) done in DIRECT CD wizard (a custom cd writing software of Easy Cd creator in WIN98) which could not be read in XP. Installation of old version of easycd creator/direct cd program was not possible in XP. I thought that possible i lost all data. Very pleasant start! Srikanth N. S. (not verified) access_time 29 Nov 2020 - 22:02 Hi Anoop, My friend is thrilled with Ubuntu. One of his grouses with windows was that after OS installation, everything else is to be installed separately which is a real headache. WHereas if u install Linux, everything is installed as a package and he is thrilled to bits! Let linux installation spread in Trivandrum and your service in this direction is highly appreciated. Kepp up the good work Regards. PDA Freak (not verified) access_time 29 Nov 2020 - 22:02 Is it possible to install linux on a packardbell notebook ? Pagination Current page 1 Page 2 Next page Next › Last page Last » Add new comment
Leave a reply
Your email address will not be published. Required fields are marked *

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
The content of this field is kept private and will not be shown publicly.
CAPTCHA This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.