Check your coding style on Drupal 8
Coding style?
Building software is a complex and sometimes tedious process in which you make errors and mistakes. Testing for errors is mostly done by running your website / code through tests either manually or automatically.
Checking for your code style like formatting and documentation flaws you can use a code sniffer. For PHP you can run phpcs
using PHP_CodeSniffer.
Drupal core provides core/phpcs.xml.dist
to tell phpcs what to test for.
How to run it
Run all tests on current directory.
cd core
../vendor/bin/phpcs -p -s
where -p
shows the progress and -s
does a sniff.
It result in
............................................................ 60 / 7477 (1%)
............................................................ 120 / 7477 (2%)
............................................................ 420 / 7477 (6%)
...........................S................................ 480 / 7477 (6%)
............................................................ 540 / 7477 (7%)
............................................................ 780 / 7477 (10%)
...............S......S..................................... 840 / 7477 (11%)
............................................................ 900 / 7477 (12%)
............................................................ 960 / 7477 (13%)
............................................................ 1320 / 7477 (18%)
............................................................ 1380 / 7477 (18%)
.......................................SSSSSSSSSSSSSSSSSS... 1440 / 7477 (19%)
...
Check for Drupal standards
Using the switch --standard=Drupal
you can check your code for Drupal related stuff. This uses coder.
cd core
../vendor/bin/phpcs --standard=Drupal modules/views/views.module
FILE: ...sers/clemens/Sites/drupal/d8/www/core/modules/views/views.module
----------------------------------------------------------------------
FOUND 23 ERRORS AND 8 WARNINGS AFFECTING 27 LINES
----------------------------------------------------------------------
92 | ERROR | [x] Inline comments must end in full-stops,
| | exclamation marks, colons, question marks, or
| | closing parentheses
94 | ERROR | [ ] If the line declaring an array spans longer than
| | 80 characters, each element should be broken
| | into its own line
116 | ERROR | [ ] If the line declaring an array spans longer than
| | 80 characters, each element should be broken
| | into its own line
117 | ERROR | [ ] If the line declaring an array spans longer than
| | 80 characters, each element should be broken
| | into its own line
120 | ERROR | [x] Each index in a multi-line array must be on a
| | new line
121 | ERROR | [x] Each index in a multi-line array must be on a
| | new line
121 | ERROR | [x] Each index in a multi-line array must be on a
| | new line
121 | ERROR | [x] Each index in a multi-line array must be on a
| | new line
121 | WARNING | [x] A comma should follow the last multiline array
| | item. Found: ]
etc
----------------------------------------------------------------------
PHPCBF CAN FIX THE 11 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Note the last line telling some stuff can be automatically fixed. Awesome!
Not interested @ all
You can filter on the sniff you are interested in using the --sniff
option.
cd core
../vendor/bin/phpcs --standard=Drupal modules/views/views.module --sniffs=Drupal.Commenting.HookComment
FILE: ...sers/clemens/Sites/drupal/d8/www/core/modules/views/views.module
----------------------------------------------------------------------
FOUND 0 ERRORS AND 6 WARNINGS AFFECTING 6 LINES
----------------------------------------------------------------------
249 | WARNING | Format should be "* Implements hook_foo_BAR_ID_bar()
| | for xyz_bar().", "* Implements hook_foo_BAR_ID_bar()
| | for xyz-bar.html.twig.", "* Implements
| | hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.", or "*
| | Implements hook_foo_BAR_ID_bar() for block
| | templates."
274 | WARNING | Format should be "* Implements hook_foo_BAR_ID_bar()
| | for xyz_bar().", "* Implements hook_foo_BAR_ID_bar()
| | for xyz-bar.html.twig.", "* Implements
| | hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.", or "*
| | Implements hook_foo_BAR_ID_bar() for block
| | templates."
287 | WARNING | Format should be "* Implements hook_foo_BAR_ID_bar()
| | for xyz_bar().", "* Implements hook_foo_BAR_ID_bar()
| | for xyz-bar.html.twig.", "* Implements
| | hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.", or "*
| | Implements hook_foo_BAR_ID_bar() for block
| | templates."
638 | WARNING | Format should be "* Implements hook_foo()."
650 | WARNING | Format should be "* Implements hook_foo_BAR_ID_bar()
| | for xyz_bar().", "* Implements hook_foo_BAR_ID_bar()
| | for xyz-bar.html.twig.", "* Implements
| | hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.", or "*
| | Implements hook_foo_BAR_ID_bar() for block
| | templates."
827 | WARNING | Format should be "* Implements hook_foo_BAR_ID_bar()
| | for xyz_bar().", "* Implements hook_foo_BAR_ID_bar()
| | for xyz-bar.html.twig.", "* Implements
| | hook_foo_BAR_ID_bar() for xyz-bar.tpl.php.", or "*
| | Implements hook_foo_BAR_ID_bar() for block
| | templates."
----------------------------------------------------------------------
Time: 157ms; Memory: 12.75Mb