-
Notifications
You must be signed in to change notification settings - Fork 30
Drupal Coding Standards
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
Preferencesdialog window, selectLanguages & Frameworks>PHP>Code Sniffer - Set the
Configurationdropdown field toLocal - Click the ellipsis next to the
Configurationfield and enter the full path tovendor/bin/phpcs(vendor\squizlabs\php_codesniffer\bin\phpcs.batin Windows) in the repository - Click
Validateto verify that the code sniffer script is found correctly - Click
OK - Return to the
Preferencesdialog 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 standardfield to refresh the list of available standards - Select
Drupalfrom the field's dropdown list - Click
OK - Exit from
PhpStorm - Open
<repository-root>/.idea/inspectionProfiles/Project_Default.xmlin an editor - Replace "Drupal" with "Drupal,DrupalPractice" in the
valueattribute of theCODING_STANDARDoptionelement - Save the file
- Launch the
PhpStormprogram again - Return to
PHP>Quality Tools>PHP Code Sniffer Validationin theEditor>Inspectionspanel of thePreferencesdialog window, following the navigation steps provided above - Verify that the
Coding standardfield now showsDrupal,DrupalPractice - Click
OKto close thePreferencesdialog 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.
- Drupal Issues Watchlist
- Onboarding
- Our SOPs
- Content Model
- Vocabularies: Site Sections
- Glossifier
- UX Features
- Drupal Implementation Design
- Development
- QA
- Project Management
- IA Guidance
- Product