https://github.com/acquia/coding-standards-php
PHP_CodeSniffer rules (sniffs) for Acquia coding standards
https://github.com/acquia/coding-standards-php
Last synced: about 2 months ago
JSON representation
PHP_CodeSniffer rules (sniffs) for Acquia coding standards
- Host: GitHub
- URL: https://github.com/acquia/coding-standards-php
- Owner: acquia
- License: gpl-2.0
- Created: 2019-05-10T19:09:34.000Z (almost 6 years ago)
- Default Branch: develop
- Last Pushed: 2024-10-07T19:46:34.000Z (7 months ago)
- Last Synced: 2025-02-15T03:04:46.244Z (2 months ago)
- Homepage:
- Size: 172 KB
- Stars: 19
- Watchers: 12
- Forks: 13
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
- Security: SECURITY.md
Awesome Lists containing this project
README
# Acquia Coding Standards for PHP
[](https://packagist.org/packages/acquia/coding-standards)
[](https://packagist.org/packages/acquia/coding-standards)
[](https://packagist.org/packages/acquia/coding-standards)
[](https://packagist.org/packages/acquia/coding-standards)
[](https://github.com/acquia/coding-standards-php/actions/workflows/orca.yml)Acquia Coding Standards for PHP is a collection of [PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) rules (sniffs) for Acquia coding standards for PHP projects, including Drupal extensions.
## Rules
Acquia Coding Standards for PHP includes a selection of sniffs from the following rulesets:
* [Drupal Code Sniffer](https://packagist.org/packages/drupal/coder) encapsulates [Drupal coding standards](https://www.drupal.org/coding-standards ) and best practices for module development.
* [PHP_CodeSniffer](https://packagist.org/packages/squizlabs/php_codesniffer) itself contains several broadly applicable rulesets.
* [PHPCompatibility](https://github.com/PHPCompatibility/PHPCompatibility) checks for PHP cross-version compatibility with all supported language versions.
* [Slevomat Coding Standard](https://github.com/slevomat/coding-standard) provides functional (safety and behavior), cleaning (dead code), and formatting (consistent look) sniffs.## Rulesets
Rules are split into rulesets according to the project *framework* and *audience*:
* Projects targeting the Drupal community, e.g., Drupal modules, should adopt a *Drupal* ruleset. All others should adopt a (more generalized) *PHP* ruleset.
* Public projects, e.g., open-source Drupal modules, should adopt a *non-strict* ruleset to facilitate external collaboration. All others should adopt a more opinionated *internal* ruleset.There are four permutations of these guidelines, leading to four rulesets to choose from:
* [AcquiaDrupalMinimal](src/Standards/AcquiaDrupalMinimal/ruleset.xml) is based on the Drupal coding standard and is intended for use on all public Drupal projects.
* [AcquiaDrupalStrict](src/Standards/AcquiaDrupalStrict/ruleset.xml) is based on AcquiaDrupal and adds the more opinionated DrupalPractice standard. It is intended for use on all internal Drupal projects.
* [AcquiaPHPMinimal](src/Standards/AcquiaPHPMinimal/ruleset.xml) is based on PSR-12 and is intended for use on all public non-Drupal projects.
* [AcquiaPHPStrict](src/Standards/AcquiaPHPStrict/ruleset.xml) is based on AcquiaPHP and adds additional, more opinionated standards. It is intended for use on all internal, non-Drupal projects.## Installation & usage
1. Add Acquia Coding Standards for PHP to your project via Composer:
```bash
composer require --dev acquia/coding-standards
```1. Inform PHP CodeSniffer of the location of the standard and its dependencies:
* It is strongly recommended that you use a Composer plugin to handle this for you, e.g., [`DealerDirect/phpcodesniffer-composer-installer`](https://github.com/DealerDirect/phpcodesniffer-composer-installer):
```bash
composer config extra.phpcodesniffer-search-depth 4
composer require --dev dealerdirect/phpcodesniffer-composer-installer
```* Alternatively, add a script to your `composer.json` to handle it:
```json
{
"scripts": {
"post-install-cmd": "@install-coding-standards",
"post-update-cmd" : "@install-coding-standards",
"install-coding-standards": "\"vendor/bin/phpcs\" --config-set installed_paths vendor/acquia/coding-standards/src/Standards,vendor/drupal/coder/coder_sniffer,vendor/phpcompatibility/php-compatibility,vendor/slevomat/coding-standard"
}
}
```1. Check code for standards compliance:
```bash
./vendor/bin/phpcs --standard=AcquiaDrupalStrict --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml path/to/code
```Automatically fix any standards violations possible:
```bash
./vendor/bin/phpcbf --standard=AcquiaDrupalStrict --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml path/to/code
```The `--extensions` argument must match the chosen code standard. For AcquiaPHP, use `--extensions=php,inc,test,css,txt,md,yml`.
1. Optionally create a [default configuration file](https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file) for your project so you don't have to provide the command-line arguments every time (i.e., below). Here's a working example: [`example/phpcs.xml.dist`](example/phpcs.xml.dist).
```bash
./vendor/bin/phpcs
```Modify `phpcs.xml.dist` to suit your project, especially to set the preferred code standard and matching extensions.
1. Optionally add code checking to your [Git pre-commit hook](https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) to prevent committing code with violations. Since client-side Git hooks are not copied when a repository is cloned, you might like to use an automated solution like [`BrainMaestro/composer-git-hooks`](https://packagist.org/packages/BrainMaestro/composer-git-hooks) to manage them, for example: [`example/composer.json`](example/composer.json).
1. Optionally [configure PHP Code Sniffer integration in PhpStorm](https://www.jetbrains.com/help/phpstorm/using-php-code-sniffer.html) or your IDE or code editor of choice. You can import [`example/PhpStormInspections.xml`](example/PhpStormInspections.xml) to set up default integration for new projects. When you open or create a new project for the first time, PhpStorm should automatically detect and set up PHPCS inspections based on these defaults. You will just need to uncheck the “installed standards paths” in the inspection settings for your project so that PhpStorm can find the Acquia Coding Standards.
## Contribution
Contributions are welcome! See [CONTRIBUTING.md](CONTRIBUTING.md).
## License
Copyright (C) 2019 Acquia, Inc.
This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.