phpcbf
Automatically fix PHP code style violations
TLDR
Fix issues in the specified directory (defaults to the PEAR standard)
Display a list of installed coding standards
Specify a coding standard to validate against
Specify comma-separated file extensions to include when sniffing
A comma-separated list of files to load before processing
Don't recurse into subdirectories
SYNOPSIS
phpcbf [options] file(s) | directory(s)
PARAMETERS
--standard=
The name of the coding standard to apply. Examples: PSR1, PSR2, Zend, Squiz.
--sniffs=
A comma-separated list of sniffs to include or exclude. Sniffs can be specified using their full name or part of their name.
--ignore=
A comma-separated list of patterns to ignore files and directories. Use wildcards (*) for flexible matching.
--extensions=
A comma-separated list of file extensions to check (e.g., php,inc,phtml).
--severity=
The minimum severity level of errors to fix (1-9).
--error-severity=
The severity level for errors (default is 5).
--warning-severity=
The severity level for warnings (default is 5).
-n
Do not interact with the user. Useful for CI/CD environments.
-v[v][v]
Increase verbosity level. Can be used multiple times for more detail.
--report=
Type of report to output. Options: full, summary, diff, csv, json, emacs, source.
--basepath=
A basepath to remove from all file paths in reports.
--suffix=
The suffix to add to the filename after fixing.
--parallel=
The number of files to process simultaneously.
--bootstrap=
A comma-separated list of files to execute before processing.
DESCRIPTION
The phpcbf command is a command-line tool used to automatically correct coding standard violations in PHP code. It's part of PHP_CodeSniffer, a broader toolset for ensuring code quality. phpcbf goes beyond simple linting by actively rewriting code to adhere to a defined set of coding standards, such as PSR-1, PSR-2, Symfony, Zend, and more.
It works by parsing PHP files, identifying code style issues based on configured rulesets (ruleset.xml or similar), and then applying automated fixes. If a violation cannot be automatically corrected, phpcbf will leave the code as is and typically report the issue. Running phpcbf can drastically improve code consistency, readability, and maintainability within a project. This tool is extremely useful during development and as a part of CI/CD pipelines to automatically maintain coding standards.
CAVEATS
Not all coding standard violations can be automatically fixed by phpcbf. Some violations require manual intervention. Backups are not created by default, so ensure you have a version control system or create manual backups before running phpcbf.
USAGE EXAMPLE
To fix all PHP files in the 'src' directory according to the PSR-2 standard, use: phpcbf --standard=PSR2 src/. To fix a single file: phpcbf --standard=PSR1 myfile.php
CONFIGURATION
phpcbf reads its configuration from a ruleset.xml file (or similar configuration file). This file specifies the coding standards, sniffs, and other settings that will be used during the code fixing process.
HISTORY
phpcbf is a tool that is a component of PHP_CodeSniffer. It was created to automatically fix code formatting issues. Over time, it has evolved to support various coding standards and has become a valuable tool in PHP development workflows to maintain code consistency.
SEE ALSO
phpcs(1)