Automatically correct coding standard violations with phpcbf
masterphpcbf script is used to automatically correct coding standard violations detected by phpcs.repository·master·Indexed 27 days ago
https://github.com/squizlabs/php_codesnifferA toolset for detecting and correcting coding standard violations in PHP, JavaScript, and CSS files. It includes phpcs for detecting violations and phpcbf (PHP Code Beautifier and Fixer) for automatically correcting them. Supports installation via Phar files, Composer, Phive, PEAR, and Git.
phpcbf script is used to automatically correct coding standard violations detected by phpcs.You can install PHP_CodeSniffer globally or as a development dependency in your project using Composer.
Global Installation:
Ensure your composer bin directory (e.g., ~/.composer/vendor/bin/) is in your PATH.
Project Dependency:
Add it to your composer.json to run it via the local vendor/bin directory.
# Global installation
composer global require "squizlabs/php_codesniffer=*"
# Project dependency (composer.json)
{
"require-dev": {
"squizlabs/php_codesniffer": "3.*
}
}
# Running from vendor bin
./vendor/bin/phpcs -h
./vendor/bin/phpcbf -hPHP_CodeSniffer can be installed using several other methods:
phive install phpcs and phive install phpcbf.pear install PHP_CodeSniffer.bin/ directory.# Phive
phive install phpcs
phive install phpcbf
./tools/phpcs -h
# PEAR
pear install PHP_CodeSniffer
# Git Clone
git clone https://github.com/squizlabs/PHP_CodeSniffer.git
cd PHP_CodeSniffer
php bin/phpcs -h
php bin/phpcbf -hThe quickest way to install PHP_CodeSniffer is to download the standalone Phar files for the phpcs (detector) and phpcbf (corrector) commands using curl or wget. After downloading, you can verify the installation by running the -h flag.
# Download using curl
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
# Or download using wget
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
# Then test the downloaded PHARs
php phpcs.phar -h
php phpcbf.phar -hphpcs script to tokenize PHP, JavaScript, and CSS files to detect violations of a defined coding standard. By default, it uses the PEAR coding standard. You can specify a different standard using the --standard flag.Cbf report implementation is specifically designed to support the auto-fixing features of the PHPCBF script. It is not intended or allowed to be selected as a report via the --report flag in the standard phpcs command line tool.The Notifysend report allows PHP_CodeSniffer to send desktop notifications via the notify-send CLI tool. You can customize its behavior using the following configuration parameters in your phpcs.xml file or via command line configuration:
notifysend_path: The full path to the notify-send executable.notifysend_timeout: The timeout for the notification in milliseconds (default is 3000).notifysend_showok: A boolean (0/1) determining whether to show an "ok, all fine" message when no errors or warnings are found (default is true).The Generic.WhiteSpace.ScopeIndent sniff enforces correct indentation for control structures. You can customize its behavior in your ruleset.xml file using the following properties:
indent: The number of spaces used for indentation (default: 4).exact: If set to true, the indentation must match the indent value exactly. If false, the indentation must be at least the indent value but can be more.tabIndent: If set to true, the sniff will use tabs instead of spaces for indentation. Note that the width of the tabs is determined by the --tab-width CLI argument.ignoreIndentationTokens: A list of tokens that should be ignored by this sniff.If you are troubleshooting why indentation is being flagged or how the sniff is calculating expected indents, you can enable debug mode. When debug is true, the sniff outputs detailed information to the console, including:
Note: This is typically controlled via the sniff's configuration properties within your ruleset.xml.
To use the Notifysend report, specify it using the --report flag when running phpcs. This will trigger desktop notifications based on the results of the scan.
phpcs --report=Notifysend /path/to/codeThe ScopeIndentSniff is a coding standard sniff used to enforce consistent indentation levels for different code structures. It handles various tokens including:
T_CLOSURE), anonymous classes (T_ANON_CLASS), and standard scope openers (e.g., if, else, while).T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, and T_CLOSE_TAG are checked to ensure they align correctly with the surrounding code.T_ELSE statements are checked for exact indentation to prevent breaking subsequent code blocks.T_OBJECT tokens trigger indentation checks for object properties.T_START_HEREDOC and T_START_NOWDOC (content-dependent indentation).T_CONSTANT_ENCAPSED_STRING and T_DOUBLE_QUOTED_STRING (multi-line strings).T_DOC_COMMENT_OPEN_TAG (doc comments).T_OBJECT_OPERATOR or T_NULLSAFE_OBJECT_OPERATOR) when exact indentation is already set.When defining the Generic.WhiteSpace.ScopeIndent sniff in a ruleset, you can set these properties:
| Property | Type | Default | Description |
|---|---|---|---|
indent | integer | 4 | The number of spaces code should be indented. |
exact | boolean | false | If true, indent must be exactly $indent spaces. If false, indent must be at least $indent spaces. |
tabIndent | boolean | false | If true, fixes will use tabs instead of spaces. |
ignoreIndentationTokens | array | [] | List of tokens not needing to be checked for indentation. |