-
Notifications
You must be signed in to change notification settings - Fork 0
PHP Linting Guide
Research for CodeSniffer by Ivan Odiel Magtangob
- https://github.com/squizlabs/PHP_CodeSniffer
- https://github.com/squizlabs/PHP_CodeSniffer/wiki/Configuration-Options
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.
To avoid modifying the VM, the two PHP_CodeSniffer scripts were installed into the repository using curl
:
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
To use either script:
phpcs /path/to/code/myfile.php
phpcbf /path/to/code/myfile.php
PHP_CodeSniffer can be further configured to ignore certain warnings, enable coloured output messages, etc.. Settings
such as these can be implemented in a CodeSniffer.conf
file:
<?php
$phpCodeSnifferConfig = array (
'colors' => '1',
'ignore_warnings_on_exit' => '1',
);
?>
Some other configuration options can also be added within the PHP files to be linted, or specified in the command when executing the script. For example, to ignore all of a certain error:
phpcs --exclude=Generic.Files.LineEndings /path/to/code/myfile.php