|
| 1 | +# GPA Lab PHP Code Standards |
| 2 | + |
| 3 | +This collection of PHP CodeSniffer rules helps ensure consistency across PHP projects developed and managed by the GPA Lab. It helps to maintain a high level of code quality and standardizes the team's approach to PHP projects. |
| 4 | + |
| 5 | +The repository contains two ruleset: |
| 6 | + |
| 7 | +- [GPA Lab Standard](#general-projects) - Base rules applicable to any PHP project |
| 8 | +- [GPA Lab WordPress](#wordpress-projects) - An extension of the WordPress Coding Standards ruleset for use when developing WordPress plugins/themes |
| 9 | + |
| 10 | +## Rulesets |
| 11 | + |
| 12 | +### General Projects |
| 13 | + |
| 14 | +The GPA Lab Standard ruleset is a general set of rule applicable to any PHP project. In order to utilize these standards, you will need to install the following dependencies into your project: |
| 15 | + |
| 16 | +- `gpalab/coding-standards` - The ruleset defined in this repository. |
| 17 | +- `squizlabs/php_codesniffer` - A tool that conducts static code analysis in order to identify and fix problems. |
| 18 | +- `dealerdirect/phpcodesniffer-composer-installer` - A Composer plugin that handles the registration of standards with PHP CodeSniffer. |
| 19 | + |
| 20 | +The recommended way to install these dependencies is to add them to the development requirements listed in your project's `composer.json` file. The result will look something like this: |
| 21 | + |
| 22 | +```json |
| 23 | +{ |
| 24 | + "name": "my-project", |
| 25 | + "require-dev": { |
| 26 | + "gpalab/coding-standards": "v1.0.0", |
| 27 | + "squizlabs/php_codesniffer": "^3.6.2", |
| 28 | + "dealerdirect/phpcodesniffer-composer-installer": "^v0.7.1" |
| 29 | + } |
| 30 | +} |
| 31 | +``` |
| 32 | + |
| 33 | +With this done, run `composer install` to install the dependencies to your project. |
| 34 | + |
| 35 | +Once installed, you will need to identify which ruleset PHP Code Sniffer should utilize. The easiest way to do so is by adding a PHP Code Sniffer configuration file such as `.phpcs.xml` to the root of your project. In this file, use the `rule` tag to reference the GPA Lab standard ruleset using the key `GPA-Lab`. The result will look something like this: |
| 36 | + |
| 37 | +```xml |
| 38 | +<?xml version="1.0"?> |
| 39 | +<ruleset name="My Project"> |
| 40 | + |
| 41 | + <!-- List of files/directories to check. --> |
| 42 | + <file>*.php</file> |
| 43 | + |
| 44 | + <!-- Exclude vendor files from testing. --> |
| 45 | + <exclude-pattern>*/vendor</exclude-pattern> |
| 46 | + |
| 47 | + <!-- Load the Standard GPA Lab ruleset --> |
| 48 | + <rule ref="GPA-Lab" /> |
| 49 | + |
| 50 | +</ruleset> |
| 51 | +``` |
| 52 | + |
| 53 | +### WordPress Projects |
| 54 | + |
| 55 | +This repository also contains a ruleset for use with WordPress projects (such as plugins and themes). In addition to the PHP best practice and style rules in the standard ruleset, this ruleset includes Wordpress-specific rules and best practices. |
| 56 | + |
| 57 | +The process for installing the WordPress ruleset is largely the same. However, since we are extending the WordPress ruleset, you will also need to install the following peer dependencies: `php-compatibility`, `phpcompatibility-wp`, and `wp-coding-standards/wpcs`. The resulting composer.json file will look like this: |
| 58 | + |
| 59 | +```json |
| 60 | +{ |
| 61 | + "name": "my-wordpress-project", |
| 62 | + "require-dev": { |
| 63 | + "gpalab/coding-standards": "v1.0.0", |
| 64 | + "squizlabs/php_codesniffer": "^3.6.2", |
| 65 | + "dealerdirect/phpcodesniffer-composer-installer": "^v0.7.1", |
| 66 | + "phpcompatibility/php-compatibility": "*", |
| 67 | + "phpcompatibility/phpcompatibility-wp": "^2.1.3", |
| 68 | + "wp-coding-standards/wpcs": "^2.3.0" |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +Your PHP Code Sniffer configuration file will look the same, except that you should use the `GPA-Lab-WordPress` reference key rather than the `GPA-Lab` key. |
| 74 | + |
| 75 | +```xml |
| 76 | +<?xml version="1.0"?> |
| 77 | +<ruleset name="My WordPress Project"> |
| 78 | + |
| 79 | + <!-- List of files/directories to check. --> |
| 80 | + <file>*.php</file> |
| 81 | + |
| 82 | + <!-- Exclude vendor files from testing. --> |
| 83 | + <exclude-pattern>*/vendor</exclude-pattern> |
| 84 | + |
| 85 | + <!-- Load the Standard GPA Lab ruleset --> |
| 86 | + <rule ref="GPA-Lab-WordPress" /> |
| 87 | + |
| 88 | +</ruleset> |
| 89 | +``` |
| 90 | + |
| 91 | +## Usage |
| 92 | + |
| 93 | +### CI |
| 94 | + |
| 95 | +Often times, the easiest way to run PHP Code Sniffer in a continuous integration context is by adding an NPM script to execute the test. For example: |
| 96 | + |
| 97 | +```json |
| 98 | +"lint:PHP": "./vendor/bin/phpcs -n", |
| 99 | +``` |
| 100 | + |
| 101 | +This script requires that PHP Code Sniffer is installed into the project's vendor directory, so make sure that you run `composer install` or `composer update` before executing this command. |
| 102 | + |
| 103 | +_Note:_ The `-n` flag indicates that warnings should be omitted. |
| 104 | + |
| 105 | +### VSCode |
| 106 | + |
| 107 | +There are a number of ways to integrate PHP Code Sniffer into your local IDE. For VSCode, we have found that the combination of these two extensions works best: |
| 108 | + |
| 109 | +- [PHP Intelephense](https://marketplace.visualstudio.com/items?itemName=bmewburn.vscode-intelephense-client) |
| 110 | +- [PHP Sniffer & Beautifier](https://marketplace.visualstudio.com/items?itemName=ValeryanM.vscode-phpsab) |
| 111 | + |
| 112 | +## Thanks |
| 113 | + |
| 114 | +This project owes a huge debt to the [WordPress Coding Standards](https://github.com/WordPress/WordPress-Coding-Standards) project, which provided both inspiration and a sane set of defaults on which we were able to build. |
0 commit comments