Contains 2 different executable:
phpcs
: Scan PHP, JavaScript and CSS files for violation.phpcfb
: Automatically correct coding standard violations.
- Supports PHP >= 5.4
-
Install PHP_codeSniffer
composer require --dev squizlabs/php_codesniffer
-
Scan files
$phpcs app --standard=PSR12 FILE: /home/user13/code/ci-phpcs-demo/app/Book.php --------------------------------------------------------------------------------------------------- FOUND 2 ERRORS AFFECTING 1 LINE --------------------------------------------------------------------------------------------------- 6 | ERROR | [ ] Each class must be in a namespace of at least one level (a top-level vendor name) 6 | ERROR | [x] Opening brace of a class must be on the line after the definition --------------------------------------------------------------------------------------------------- PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY --------------------------------------------------------------------------------------------------- Time: 20ms; Memory: 6MB
-
Use
-n
to ignorewarning
level violations.phpcs -n app
-
Use
--report=summary
to output a summary of the issues.phpcs -s --report=summary
-
To automatically fix errors:
$ phpcbf F 1 / 1 (100%) PHPCBF RESULT SUMMARY ---------------------------------------------------------------------------- FILE FIXED REMAINING ---------------------------------------------------------------------------- /home/user13/code/ci-phpcs-demo/app/Book.php 2 0 ---------------------------------------------------------------------------- A TOTAL OF 2 ERRORS WERE FIXED IN 1 FILE
phpcbf
will make a "best effort". phpcbf
will report any unfixed errors.
When not set, via configuration or CLI, PHPCS defaults to PEAR
standard.
-
Use
-i
to list available code standards.$ phpcs -i The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz and Zend
-
Use
-e
with a installed standard to list available checks.$ phpcs --standard=PSR12 -e ... PSR12 (17 sniffs) ----------------- PSR12.Classes.AnonClassDeclaration PSR12.Classes.ClassInstantiation PSR12.Classes.ClosingBrace ...
By default, PHPCS assumes all files have UTF-8 encoding.
phpcs --encoding=shiftjis app
PHPCS attempts to find a valid configuration by looking for files in the following order:
.phpcs.xml
, phpcs.xml
, .phpcs.xml.dist
, phpcs.xml.dist
The following CLI
phpcs --colors -p -s --standard=PSR12 src tests
... Can be replace with a configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- Define a cache file. Add this to .gitignore -->
<arg name="cache" value=".phpcs-cache"/>
<!-- Turn colors ON. -->
<arg name="colors"/>
<!-- Show progress of the run. -->
<arg value="p"/>
<!-- Show sniff codes in all reports -->
<arg value="s" />
<!-- Use PSR12 standards -->
<rule ref="PSR12"/>
<file>src</file>
<file>tests</file>
</ruleset>
- Ignore globs using the CLI
phpcs --ignore=*/tests/*,*/data/* /path/to/code
- To ignore an entire file
<?php
// phpcs:ignoreFile -- this is not a core file
$xmlPackage = new XMLPackage;
- To ignore a code block
$xmlPackage = new XMLPackage;
// phpcs:disable
$xmlPackage['error_code'] = get_default_error_code_value();
$xmlPackage->send();
// phpcs:enable
- To ignore a specific rule only
// phpcs:disable Generic.Commenting.Todo.Found
// TODO: Add an error message here.
$xmlPackage->send();
// phpcs:enable
Slevomat adds rules and styling.
Slevomat Coding Standard for PHP_CodeSniffer provides sniffs that fall into three categories:
- Functional - improving the safety and behavior of code
- Cleaning - detecting dead code
- Formatting - rules for consistent code looks
composer require --dev slevomat/coding-standard
Homepage: https://marketplace.visualstudio.com/items?itemName=ValeryanM.vscode-phpsab
- Extension requires locally install PHPCS
"[php]": {
"editor.formatOnSave": true,
"editor.defaultFormatter": "valeryanm.vscode-phpsab"
},
"phpsab.fixerEnable": true,
"phpsab.snifferEnable": true,
"phpsab.autoRulesetSearch": true,
"phpsab.snifferShowSources": true,
"phpsab.snifferMode": "onSave"