Drupal Coding Standards - NCIOCPL/cgov-digital-platform GitHub Wiki
This project follows Drupal Coding Standards and uses the PHP Code Sniffer tool to help ensure that our own code conforms to those standards. Here are the instructions for hooking up two of the popular PHP IDEs to the tool, which is installed by composer install
when you set up your development environment. These instructions assume you have cloned the repository, run composer install
, and installed Visual Studio Code or PhpStorm.
(N.B.: The code sniffer is looking at the code in the file system, not at what you have staged for commit. So if you do the following:
- edit your code in your IDE
- see that the IDE is reporting violations of the coding standards in your code
- stage the changes anyway (
git add ...
) - fix the coding standard violation but do not stage those fixes
- commit what you staged in step 3
... the pre-commit
hook will not be able to prevent the commit from proceeding. Later on the pipeline will catch the problems and fail the build. You can avoid this annoyance by making sure you only commit code which your IDE determines is clean. If you do not use an IDE capable of checking for coding standards violations, you probably shouldn't commit staged code which differs from unstaged versions of that code in the file system.
- From the menu, select
View
>Extensions
- Type
phpcs
- Click
Install
- Click
Reload to Activate
VS Code takes care of the rest.
- From the menu, select
File
>Settings
(on Mac,PhpStorm
>Preferences
) - In the
Preferences
dialog window, selectLanguages & Frameworks
>PHP
>Code Sniffer
- Set the
Configuration
dropdown field toLocal
- Click the ellipsis next to the
Configuration
field and enter the full path tovendor/bin/phpcs
(vendor\squizlabs\php_codesniffer\bin\phpcs.bat
in Windows) in the repository - Click
Validate
to verify that the code sniffer script is found correctly - Click
OK
- Return to the
Preferences
dialog window (see earlier steps), selectEditor
>Inspections
- In the right panel, expand
PHP
- Under
PHP
, expandQuality Tools
- Under
Quality Tools
, selectPHP Code Sniffer Validation
- Click the checkbox next to this selection to turn on the use of the coding standards checking tool
- Optionally turn on
Show sniff name
- Click the circular arrows icon next to the
Coding standard
field to refresh the list of available standards - Select
Drupal
from the field's dropdown list - Click
OK
- Exit from
PhpStorm
- Open
<repository-root>/.idea/inspectionProfiles/Project_Default.xml
in an editor - Replace "Drupal" with "Drupal,DrupalPractice" in the
value
attribute of theCODING_STANDARD
option
element - Save the file
- Launch the
PhpStorm
program again - Return to
PHP
>Quality Tools
>PHP Code Sniffer Validation
in theEditor
>Inspections
panel of thePreferences
dialog window, following the navigation steps provided above - Verify that the
Coding standard
field now showsDrupal,DrupalPractice
- Click
OK
to close thePreferences
dialog window
To verify that your IDE is correctly configured, add a new file test.php
in the blt/src
directory of the repository, with the following content:
<?php
/**
* @file
* @author Klem Kadiddlehopper
*/
Hover your mouse over the @author
tag. If your editing environment is correctly configured, you should see a popup which says @author tags are not usually used in Drupal, because over time multiple contributors will touch the code anyway
.