### Reference sniff by file path Source: https://github.com/squizlabs/php_codesniffer/wiki/Annotated-Ruleset This example shows how to reference a sniff using its absolute or relative file path when the sniff is not installed or its code is unknown. ```XML ``` -------------------------------- ### Install PHP_CodeSniffer using PEAR Source: https://github.com/squizlabs/php_codesniffer/blob/master/README.md Install PHP_CodeSniffer using the PEAR installer. This makes the phpcs and phpcbf commands available system-wide. ```bash pear install PHP_CodeSniffer ``` -------------------------------- ### Align Assignment Operators by Start Source: https://github.com/squizlabs/php_codesniffer/wiki/Customisable-Sniff-Properties Demonstrates code formatted with assignment operators aligned by their start. ```PHP $foo = 'foo'; $foo .= 'bar'; ``` -------------------------------- ### View Configuration Options Output Source: https://github.com/squizlabs/php_codesniffer/wiki/Advanced-Usage This is an example of the output when viewing configuration options. ```text Array ( [default_standard] => PEAR [zend_ca_path] => /path/to/ZendCodeAnalyzer ) ``` -------------------------------- ### Install PHP_CodeSniffer using Phive Source: https://github.com/squizlabs/php_codesniffer/blob/master/README.md Install PHP_CodeSniffer as a project tool using Phive. Commands can then be run from the tools directory. ```bash phive install phpcs phive install phpcbf ``` -------------------------------- ### PHP Code Example Source: https://github.com/squizlabs/php_codesniffer/wiki/Advanced-Usage A simple PHP file used to demonstrate the level map output. This code is for illustrative purposes. ```php ``` -------------------------------- ### List Installed Coding Standards Source: https://github.com/squizlabs/php_codesniffer/wiki/Usage Use the -i flag to display a list of all coding standards currently installed and available for use with PHP_CodeSniffer. ```bash $ phpcs -i The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend ``` -------------------------------- ### PHP String Concatenation Example Source: https://github.com/squizlabs/php_codesniffer/wiki/Customisable-Sniff-Properties Illustrates the default zero-space padding around concatenation operators in PHP. ```PHP $foo = $number.'-'.$letter; ``` -------------------------------- ### Set Installed Standard Paths Source: https://github.com/squizlabs/php_codesniffer/wiki/Configuration-Options Adds custom paths where PHP_CodeSniffer should look for installed coding standards. ```bash $ phpcs --config-set installed_paths /path/to/one,/path/to/two ``` -------------------------------- ### Example of Multi-line Object Chain Indenting (PEAR.WhiteSpace.ObjectOperatorIndent) Source: https://github.com/squizlabs/php_codesniffer/wiki/Customisable-Sniff-Properties Demonstrates a multi-line object chain where indentation increases and decreases to show balanced method calls. ```php $rootNode ->children() ->booleanNode('foo') ->defaultTrue() ->end() ->scalarNode('bar') ->defaultValue('default') ->end() ->end(); ``` -------------------------------- ### Install PHP_CodeSniffer globally via Composer Source: https://github.com/squizlabs/php_codesniffer/blob/master/README.md Install PHP_CodeSniffer globally using Composer. Ensure your composer bin directory is in your PATH. ```bash composer global require "squizlabs/php_codesniffer=*" ``` -------------------------------- ### Include sniffs from other standards using PHP Source: https://github.com/squizlabs/php_codesniffer/wiki/Version-1.3.0-Upgrade-Guide Example of a CodingStandard.php class that includes sniffs from PEAR and Generic standards. ```php public function getIncludedSniffs() { return array( 'PEAR', 'Generic/Sniffs/Formatting/MultipleStatementAlignmentSniff.php', 'Generic/Sniffs/Functions', ); }//end getIncludedSniffs() ``` -------------------------------- ### Verbose Tokeniser Output - Scope Map Example Source: https://github.com/squizlabs/php_codesniffer/wiki/Advanced-Usage This example shows the verbose tokeniser output for the scope map, detailing token processing for a simple PHP if statement. ```php ``` ```text *** START SCOPE MAP *** Start scope map at 1: T_IF => if Process token 2 []: T_WHITESPACE => Process token 3 []: T_OPEN_PARENTHESIS => ( * skipping parenthesis * Process token 6 []: T_WHITESPACE => Process token 7 []: T_OPEN_CURLY_BRACKET => { => Found scope opener for 1 (T_IF) Process token 8 [opener:7;]: T_WHITESPACE => Process token 9 [opener:7;]: T_WHITESPACE => Process token 10 [opener:7;]: T_ECHO => echo Process token 11 [opener:7;]: T_WHITESPACE => Process token 12 [opener:7;]: T_CONSTANT_ENCAPSED_STRING => 'Condition was true' Process token 13 [opener:7;]: T_SEMICOLON => ; Process token 14 [opener:7;]: T_WHITESPACE => Process token 15 [opener:7;]: T_CLOSE_CURLY_BRACKET => } => Found scope closer for 1 (T_IF) *** END SCOPE MAP *** ``` -------------------------------- ### Exclude an entire standard Source: https://github.com/squizlabs/php_codesniffer/wiki/Annotated-Ruleset This example shows how to include all sniffs from the 'Squiz' standard but exclude any sniffs that originate from the 'Generic' standard. ```XML ``` -------------------------------- ### Show Summary Report with Source Information Source: https://github.com/squizlabs/php_codesniffer/wiki/Reporting Use the -s flag with the summary report to get a concise overview of errors and warnings per file. ```bash phpcs -s --report=summary /path/to/code ``` -------------------------------- ### Specify PEAR Coding Standard Source: https://github.com/squizlabs/php_codesniffer/wiki/Usage Use the --standard argument to specify the coding standard to use for checking files. This example uses the PEAR standard for a specific file. ```bash $ phpcs --standard=PEAR /path/to/code/myfile.inc ``` -------------------------------- ### Customize multiple messages in a sniff Source: https://github.com/squizlabs/php_codesniffer/wiki/Annotated-Ruleset This example shows how to customize different messages within the same sniff, 'Generic.Files.LineLength', by providing unique codes for each message. ```XML Line contains %2$s chars, which is more than the limit of %1$s Line longer than %s characters; contains %s characters ``` -------------------------------- ### Configure Multiple Statement Alignment (Start Alignment) Source: https://github.com/squizlabs/php_codesniffer/wiki/Customisable-Sniff-Properties Set 'alignAtEnd' to false to align assignment operators by their start instead of their end. ```XML ``` -------------------------------- ### Sample Ruleset with Selective Rule Application Source: https://github.com/squizlabs/php_codesniffer/wiki/Annotated-Ruleset This XML file demonstrates how to use phpcs-only and phpcbf-only attributes to apply rules selectively. It shows examples for configuration, file exclusion, sniff exclusion, message severity, and property values. ```xml */3rdparty/* 0 ``` -------------------------------- ### Convert sniff paths to internal codes Source: https://github.com/squizlabs/php_codesniffer/wiki/Version-1.3.0-Upgrade-Guide Examples demonstrating the conversion of file paths for sniffs to their internal PHP_CodeSniffer codes for use in ruleset.xml. ```plaintext BEFORE: Generic/Sniffs/VersionControl/SubversionPropertiesSniff.php AFTER: Generic.VersionControl.SubversionProperties BEFORE: PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php AFTER: PEAR.ControlStructures.ControlSignature BEFORE: Squiz/Sniffs/Strings/DoubleQuoteUsageSniff.php AFTER: Squiz.Strings.DoubleQuoteUsage ``` -------------------------------- ### Level Map Output Example Source: https://github.com/squizlabs/php_codesniffer/wiki/Advanced-Usage The detailed token-by-token output of the level map for the provided PHP code. It shows token type, line number, level, and scope information. ```text *** START LEVEL MAP *** Process token 0 on line 1 [lvl:0;]: T_OPEN_TAG => if Process token 2 on line 2 [lvl:0;]: T_WHITESPACE => Process token 3 on line 2 [lvl:0;]: T_OPEN_PARENTHESIS => ( Process token 4 on line 2 [lvl:0;]: T_VARIABLE => $condition Process token 5 on line 2 [lvl:0;]: T_CLOSE_PARENTHESIS => ) Process token 6 on line 2 [lvl:0;]: T_WHITESPACE => Process token 7 on line 2 [lvl:0;]: T_OPEN_CURLY_BRACKET => { => Found scope opener for 1 (T_IF) * level increased * * token 1 (T_IF) added to conditions array * Process token 8 on line 2 [lvl:1;conds;T_IF;]: T_WHITESPACE => Process token 9 on line 3 [lvl:1;conds;T_IF;]: T_WHITESPACE => Process token 10 on line 3 [lvl:1;conds;T_IF;]: T_ECHO => echo Process token 11 on line 3 [lvl:1;conds;T_IF;]: T_WHITESPACE => Process token 12 on line 3 [lvl:1;conds;T_IF;]: T_CONSTANT_ENCAPSED_STRING => 'Condition was true' Process token 13 on line 3 [lvl:1;conds;T_IF;]: T_SEMICOLON => ; Process token 14 on line 3 [lvl:1;conds;T_IF;]: T_WHITESPACE => Process token 15 on line 4 [lvl:1;conds;T_IF;]: T_CLOSE_CURLY_BRACKET => } => Found scope closer for 7 (T_OPEN_CURLY_BRACKET) * token T_IF removed from conditions array * * level decreased * Process token 16 on line 4 [lvl:0;]: T_WHITESPACE => Process token 17 on line 5 [lvl:0;]: T_CLOSE_TAG => ?> *** END LEVEL MAP *** ``` -------------------------------- ### Configure FunctionComment specialMethods Source: https://github.com/squizlabs/php_codesniffer/wiki/Customisable-Sniff-Properties Define special methods that do not require a @return tag in their docblocks. Includes default methods and an example to ignore another function. ```xml ``` -------------------------------- ### Run PHP_CodeSniffer from Phive tools directory Source: https://github.com/squizlabs/php_codesniffer/blob/master/README.md Execute PHP_CodeSniffer commands after installing them with Phive. The executables are located in the tools directory. ```bash ./tools/phpcs -h ./tools/phpcbf -h ``` -------------------------------- ### Run PHP_CodeSniffer from Composer vendor bin Source: https://github.com/squizlabs/php_codesniffer/blob/master/README.md If PHP_CodeSniffer is installed as a project dependency via Composer, run the commands from the vendor bin directory. ```bash ./vendor/bin/phpcs -h ./vendor/bin/phpcbf -h ``` -------------------------------- ### Customize sniff property for end of line character Source: https://github.com/squizlabs/php_codesniffer/wiki/Annotated-Ruleset This example shows how to customize the 'Generic.Files.LineEndings' sniff by specifying the expected end-of-line character. ```XML ``` -------------------------------- ### Summary report Source: https://github.com/squizlabs/php_codesniffer/wiki/Usage Use the --report=summary argument to get a concise report showing only the count of errors and warnings per file. ```bash $ phpcs --report=summary /path/to/code ``` -------------------------------- ### PHP Function Declaration with Default Arguments Source: https://github.com/squizlabs/php_codesniffer/wiki/Customisable-Sniff-Properties Example of a PHP function declaration with default argument values, demonstrating the default spacing around the equals sign. ```PHP function foo($a='a', $b='b') { // Body. } ``` -------------------------------- ### Verbose Token Processing Output Example Source: https://github.com/squizlabs/php_codesniffer/wiki/Advanced-Usage This output shows the detailed token processing for a PHP code snippet, including token IDs, types, contents, and sniff execution times. It is useful for debugging. ```text *** START TOKEN PROCESSING *** Process token 0: T_OPEN_TAG => if Processing PEAR_Sniffs_ControlStructures_ControlSignatureSniff... DONE in 0.0001 seconds Processing PEAR_Sniffs_ControlStructures_MultiLineConditionSniff... DONE in 0 seconds Processing PEAR_Sniffs_WhiteSpace_ScopeClosingBraceSniff... DONE in 0 seconds Processing PEAR_Sniffs_WhiteSpace_ScopeIndentSniff... DONE in 0 seconds Processing Generic_Sniffs_ControlStructures_InlineControlStructureSniff... DONE in 0 seconds Process token 2: T_WHITESPACE => Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds Process token 3: T_OPEN_PARENTHESIS => ( Process token 4: T_VARIABLE => $condition Processing PEAR_Sniffs_NamingConventions_ValidVariableNameSniff... DONE in 0 seconds Process token 5: T_CLOSE_PARENTHESIS => ) Process token 6: T_WHITESPACE => Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds Process token 7: T_OPEN_CURLY_BRACKET => { Process token 8: T_WHITESPACE => Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds Process token 9: T_WHITESPACE => Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds Process token 10: T_ECHO => echo Process token 11: T_WHITESPACE => Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds Process token 12: T_CONSTANT_ENCAPSED_STRING => 'Condition was true' Process token 13: T_SEMICOLON => ; Process token 14: T_WHITESPACE => Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds Process token 15: T_CLOSE_CURLY_BRACKET => } Process token 16: T_WHITESPACE => Processing Generic_Sniffs_WhiteSpace_DisallowTabIndentSniff... DONE in 0 seconds Process token 17: T_CLOSE_TAG => ?> *** END TOKEN PROCESSING *** ``` -------------------------------- ### Change sniff message type Source: https://github.com/squizlabs/php_codesniffer/wiki/Annotated-Ruleset This example demonstrates changing the message type of a specific sniff message from its default to 'error' or 'warning'. ```XML error warning ``` -------------------------------- ### PHP control structure with alternative syntax and one space before colon Source: https://github.com/squizlabs/php_codesniffer/wiki/Customisable-Sniff-Properties Example of PHP control structure using alternative syntax, adhering to the default rule of one space before the colon. ```PHP if ($foo) : // IF body. else : // ELSE body. endif; ``` -------------------------------- ### Specify Multiple Coding Standards Source: https://github.com/squizlabs/php_codesniffer/wiki/Usage Pass a comma-separated list of standards to the --standard argument to check files against multiple coding standards simultaneously, including a mix of installed and external standards. ```bash $ phpcs --standard=PEAR,Squiz,/path/to/MyStandard /path/to/code/myfile.inc ``` -------------------------------- ### Comprehensive Information Report with Multiple Standards Source: https://github.com/squizlabs/php_codesniffer/wiki/Reporting To get a more thorough analysis, specify multiple standards. This command checks your code against a wider range of conventions, providing more detailed information. ```bash phpcs --standard=Generic,PEAR,Squiz,PSR2,Zend --report=info /path/to/code ``` -------------------------------- ### Hard-coding Include Patterns for Sniffs Source: https://github.com/squizlabs/php_codesniffer/wiki/Annotated-Ruleset Hard-code include patterns to limit sniffs to specific files. Patterns are checked using absolute paths. This example runs Squiz DoubleQuoteUsage only for files matching '*/templates/*' or '*\.tpl$'. ```XML */templates/* *\.tpl$ ``` -------------------------------- ### View Currently Set Configuration Options Source: https://github.com/squizlabs/php_codesniffer/wiki/Advanced-Usage Use this command to display all currently set configuration options and their values. ```bash phpcs --config-show ``` -------------------------------- ### Create ruleset.xml file Source: https://github.com/squizlabs/php_codesniffer/wiki/Version-1.3.0-Upgrade-Guide Use the 'touch' command to create an empty ruleset.xml file in the top-level directory of your custom coding standard. ```bash touch MyStandard/ruleset.xml ``` -------------------------------- ### Set a Configuration Option Source: https://github.com/squizlabs/php_codesniffer/wiki/Advanced-Usage Use this command to set a configuration option globally. Options are written to a global configuration file. ```bash phpcs --config-set