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] [files_or_directories...]
PARAMETERS
--standard=
The coding standard to use.
Can be a predefined standard (e.g., PSR1, PSR12, Squiz, Zend) or the path to a custom ruleset.
-p, --progress
Show progress of the run.
-s, --sniffs=
A comma-separated list of sniffs to fix.
Only fixes these specified sniffs.
--ignore=
A comma-separated list of file/directory patterns to ignore.
Files or directories matching these patterns will not be processed.
--exclude=
A comma-separated list of sniffs to exclude from fixing.
Violations for these sniffs will not be corrected.
--severity=
The minimum severity of an error or warning to fix.
Defaults to 5.
--tab-width=
The number of spaces each tab represents.
Defaults to the tab width defined in the coding standard.
--suffix=
Write fixed files to a new file with the specified suffix.
By default, original files are overwritten.
--report=
Generate a report in a specific format (e.g., full, summary, json, xml).
Defaults to full
.
--report-file=
Write the report to the specified file instead of stdout.
--stdin
Read input from stdin.
Must be used with --stdin-path
.
--stdin-path=
The path to the file when reading from stdin.
Required when --stdin
is used to correctly identify the file type and apply rules.
-v, --verbose
Print verbose output.
Use -vv
or -vvv
for more verbosity.
--version
Print version information.
--help
Print help information.
DESCRIPTION
phpcbf (PHP Code Beautifier and Fixer) is a powerful command-line tool that automatically corrects most coding standard violations detected by PHP_CodeSniffer. It analyzes code against a defined coding standard (e.g., PSR-1, PSR-2, PSR-12, or custom standards) and attempts to fix issues such as incorrect indentation, spacing, naming conventions, and other stylistic errors. Unlike phpcs, which only reports violations, phpcbf modifies the source code directly, saving developers time and effort in manually adhering to project coding guidelines. It's often used as part of CI/CD pipelines or pre-commit hooks to ensure code consistency.
CAVEATS
While phpcbf is highly effective, it cannot fix all types of coding standard violations. Some issues might require manual intervention, especially those related to logical flow or complex structural changes. It's crucial to use phpcbf with version control (e.g., Git) so that changes can be easily reviewed and reverted if necessary. Always test code after automated fixes to ensure functionality remains intact and no unintended side effects occur.
<B>CONFIGURATION FILES</B>
phpcbf can be configured using a phpcs.xml
or phpcs.xml.dist
file placed in the project root.
This file allows specifying the default standard, ignored paths, custom sniffs, and other settings, making it easier to maintain consistent code quality across a project without always typing long command-line arguments.
<B>INTEGRATION WITH IDES/EDITORS</B>
Many modern IDEs and text editors (like VS Code, PhpStorm, Sublime Text) have plugins that integrate phpcbf.
This allows for real-time code fixing upon saving or as part of a format-on-save action, providing immediate feedback and ensuring code adheres to standards during development.
HISTORY
phpcbf is an integral part of the PHP_CodeSniffer project, which was initially created by Greg Sherwood in 2006. phpcbf itself was introduced later as a companion tool to phpcs to provide automated fixing capabilities, significantly enhancing the developer workflow by reducing the manual effort required to conform to coding standards. Its development continues as part of the broader PHP_CodeSniffer effort to support new PHP versions, improve sniff accuracy, and add new features.
SEE ALSO
phpcs(1)