Skip to main content

PHPCS - PHP Code Sniffer

https://github.com/squizlabs/PHP_CodeSniffer

PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.

Vortex comes with pre-configured PHPCS ruleset for Drupal projects.

Usage

Check for violations

vendor/bin/phpcs

or

ahoy lint-be

Fix violations

vendor/bin/phpcbf

or

ahoy lint-fix

Configuration

See configuration reference.

All global configuration takes place in the phpcs.xml file.

By default, PHPCS will check against the following rules:

  • Drupal
  • DrupalPractice
  • Generic.Debug.ESLint
  • PHPCompatibility

Targets include custom modules and themes, settings and tests.

Adding or removing targets:

<file>path/to/dir_or_file</file>

Run checks against platform version specified in composer.json key config.platform.php:

<config name="testVersion" value="8.1"/>

Ignoring

Ignoring rules globally takes place in the phpcs.xml file:

<!-- Comment about why this rules is excluded. -->
<rule ref="DrupalPractice.General.ClassName.ClassPrefix">
<exclude-pattern>*\/dir\/another\/*\.php</exclude-pattern>
<exclude-pattern>*\/dir\/another\/*\.inc</exclude-pattern>
</rule>

To ignore all PHPCS rules within a file, place in the file header:

// phpcs:ignoreFile

To ignore a specific rule within a file, place in the file header:

// phpcs:disable <rule_name>

To ignore rule for the code block:

// phpcs:disable <rule_name>
$a = 1;
// phpcs:enable <rule_name>

To ignore only the current and the next line:

// phpcs:ignore
$a = 1;

Ignoring fail in CI

This tool runs in CI by default and fails the build if there are any violations.

Set VORTEX_CI_PHPCS_IGNORE_FAILURE environment variable to 1 to ignore failures. The tool will still run and report violations, if any.