|
| 1 | +HOWTO: Drupal Examples For Developers Coding Standards |
| 2 | +======================================================= |
| 3 | + |
| 4 | +Examples uses the same coding standards as Drupal core. |
| 5 | + |
| 6 | +Examples has a `phpcs.xml.dist` file at the root of the project. This file |
| 7 | +specifies the current coding standards 'sniffs' which code in the project must |
| 8 | +pass. |
| 9 | + |
| 10 | +The `phpcs.xml.dist` file is used by a tool called PHP_CodeSniffer (`phpcs`). |
| 11 | + |
| 12 | +Contributors should install `phpcs` in their local Drupal installation, and then |
| 13 | +use that to run `phpcs` against Examples as part of their development and review |
| 14 | +process. |
| 15 | + |
| 16 | +Contributors can also patch the `phpcs.xml.dist` file itself, in order to fix |
| 17 | +the codebase to pass a given rule or sniff. Patches which do this should be |
| 18 | +limited to a single rule or sniff, in order make the patch easier to review. |
| 19 | + |
| 20 | +Installing phpcs |
| 21 | +---------------- |
| 22 | + |
| 23 | +Generally: Use Composer to add Drupal's Coder project to your root |
| 24 | +Drupal project. Coder contains the PHP_CodeSniffer rules we need, and |
| 25 | +also requires `phpcs`. |
| 26 | + |
| 27 | +Then tell `phpcs' where our Drupal-specific rules are. |
| 28 | + |
| 29 | +Like this: |
| 30 | + |
| 31 | + $ cd my/drupal/root/ |
| 32 | + $ composer require drupal/coder |
| 33 | + // Composer installs Coder, which requires PHP_CodeSniffer as well. |
| 34 | + // Configure phpcs to use the Drupal standard rules... |
| 35 | + $ ./vendor/bin/phpcs --config-set installed_paths /path/to/drupal/vendor/drupal/coder/coder_sniffer/ |
| 36 | + // phpcs now knows how to find the Drupal standard. You can test it: |
| 37 | + $ cd core |
| 38 | + $ ../vendor/bin/phpcs -e --standard=Drupal |
| 39 | + // Shows you a bunch of Drupal-related sniffs. |
| 40 | + |
| 41 | +Running phpcs |
| 42 | +------------- |
| 43 | + |
| 44 | +Now you can run phpcs: |
| 45 | + |
| 46 | + $ cd modules/examples |
| 47 | + $ ../../vendor/bin/phpcs -p -s |
| 48 | + // phpcs uses Exampes' phpcs.xml.dist to verify coding standards. |
| 49 | + // -p shows you progress dots. |
| 50 | + // -s shows you sniff errors in the report. |
| 51 | + |
| 52 | +If there are errors, they can sometimes be fixed with `phpcbf`, which is |
| 53 | +part of the `phpcs` package. |
| 54 | + |
| 55 | + $ ../../vendor/bin/phpcbf |
| 56 | + // phpcbf now performs automated fixes. |
| 57 | + |
| 58 | +Always look at the changes to see what `phpcbf` did. |
| 59 | + |
| 60 | +And always re-run `phpcs` in order to discover whether `phpcbf` handled all the |
| 61 | +errors. |
0 commit comments