### Dummy Code Example Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/tests/Core/Generators/Expectations/ExpectedOutputInvalidCodeTitleEmpty.txt This is a dummy code example for testing purposes. ```php // Dummy. ``` ```php // Dummy. ``` -------------------------------- ### Set Installed Standard Paths Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Configuration-Options Configure custom paths where PHP_CodeSniffer should look for installed coding standards. Relative paths must start with './'. ```bash $ phpcs --config-set installed_paths /path/to/one,/path/to/two ``` -------------------------------- ### PHP Code Example: Parentheses with Single Space Padding Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Shows an example of parentheses with a single space for padding, which can be configured using the 'spacing' property. ```php $foo = ( $bar !== 'bar' ); ``` -------------------------------- ### List Installed Coding Standards Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Usage Use the -i argument 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 PEAR, PSR1, PSR2, PSR12, Squiz and Zend ``` -------------------------------- ### Install PHP_CodeSniffer globally with Composer Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/README.md Install PHP_CodeSniffer system-wide using Composer. Ensure your composer bin directory is in your PATH. ```bash composer global require "squizlabs/php_codesniffer=*" ``` -------------------------------- ### Install PHP_CodeSniffer with Phive Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/README.md Install PHP_CodeSniffer as a project tool using Phive. This command requires trusting the GPG key for PHPCS. ```bash phive install --trust-gpg-keys D91D86963AF3A29B6520462297B02DD8E5071466 phpcs phive install --trust-gpg-keys D91D86963AF3A29B6520462297B02DD8E5071466 phpcbf ``` -------------------------------- ### Emacs Report Output Example Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Reporting Example output of an Emacs report, listing errors and warnings with file, line, and column information. ```text path/to/code/fileA.php:1:1: error - Header blocks must be separated by a single blank line path/to/code/fileA.php:10:24: error - Expected 0 spaces after opening parenthesis; 1 found path/to/code/fileA.php:10:26: error - PHP parameter type declarations must be lowercase; expected "string" but found "String" path/to/code/fileA.php:10:26: error - Expected 1 space between type hint and argument "$param"; 2 found path/to/code/fileA.php:12:136: warning - Line exceeds 120 characters; contains 136 characters path/to/code/fileA.php:13:5: error - Line indented incorrectly; expected at least 8 spaces, found 4 path/to/code/fileA.php:13:12: error - TRUE, FALSE and NULL must be lowercase; expected "false" but found "FALSE" ``` -------------------------------- ### Supported Tokenizers Example (Pre PHP_CodeSniffer 4.0.0) Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Coding-Standard-Tutorial This example shows how to specify supported tokenizers for a sniff in versions prior to PHP_CodeSniffer 4.0.0. It allows the sniff to be used with PHP and JavaScript code. ```php /** * A list of tokenizers this sniff supports. * * @var array */ public $supportedTokenizers = [ 'PHP', 'JS', ]; ``` -------------------------------- ### XML Rule Configuration Example Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Version-4.0-User-Upgrade-Guide Demonstrates how to change the message type for a sniff from 'error' to 'warning' in a PHP_CodeSniffer ruleset file. ```xml warning ``` -------------------------------- ### Diff Report Output Example Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Reporting Example output of a diff report, showing changes between the original and modified file content. ```diff --- path/to/code/fileB.php +++ PHP_CodeSniffer @@ -1,7 +1,7 @@ ``` -------------------------------- ### Multi-line Object Chain Indentation Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Example of a multi-line object chain with standard indentation. ```php $rootNode ->children() ->booleanNode('foo') ->defaultTrue() ->end() ->scalarNode('bar') ->defaultValue('default') ->end() ->end(); ``` -------------------------------- ### PHP Code Example: Basic Parentheses Usage Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Illustrates the default behavior of checking padding inside parentheses when they are not used by function declarations, calls, or control structures. ```php $foo = ($bar !== 'bar'); ``` -------------------------------- ### Example JUnit Report Structure Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Reporting This is an example of the XML output generated by PHP_CodeSniffer in JUnit report format. It details test suites, test cases, and failures. ```xml ``` -------------------------------- ### PHPCBF Output Summary Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Fixing-Errors-Automatically This is an example of the summary output after running PHPCBF, showing the number of errors fixed and remaining for each file processed. ```text PHPCBF RESULT SUMMARY ---------------------------------------------------------------------- FILE FIXED REMAINING ---------------------------------------------------------------------- src/Runner.php 95 16 src/Config.php 889 38 ---------------------------------------------------------------------- A TOTAL OF 984 ERRORS WERE FIXED IN 2 FILES ---------------------------------------------------------------------- ``` -------------------------------- ### Valid Sniff with Nested Standard Directory Source: https://github.com/phpcsstandards/php_codesniffer/wiki/About-Standards-for-PHP_CodeSniffer Example of a sniff file when the standard directory is nested deeper within a project structure. ```php ``` -------------------------------- ### Include sniffs by path Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Annotated-Ruleset Include specific sniffs using their absolute or relative file paths when they are not installed or to reference custom sniffs. ```xml ``` -------------------------------- ### Control Signature: No Spaces Before Colon Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Shows an example of control structures using alternative syntax with no space before the colon. ```php if ($foo): // IF body. else: // ELSE body. endif; ``` -------------------------------- ### Register External Coding Standard Path Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Coding-Standard-Tutorial Configures PHP_CodeSniffer to recognize the custom MyStandard directory as an installed coding standard. ```bash phpcs --config-set installed_paths /path/to/MyStandard ``` -------------------------------- ### Valid Code Example: Blank Lines Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/tests/Core/Generators/Expectations/ExpectedOutputUnsupportedSuperfluousCodeElement.md This snippet demonstrates a valid handling of blank lines in code, as per the GeneratorTest Coding Standard. ```php $valid = true; ``` -------------------------------- ### Invalid Directory Structure (Missing Category Sub-directory) Source: https://github.com/phpcsstandards/php_codesniffer/wiki/About-Standards-for-PHP_CodeSniffer An example of incorrect directory layout where the required '[CategoryName]' sub-directory is missing. ```php ``` -------------------------------- ### Include Patterns for Sniff Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Annotated-Ruleset Hard-codes include patterns for a specific sniff, allowing it to run only on files matching these patterns. This example includes the Squiz DoubleQuoteUsage sniff for files matching the provided patterns. ```XML */templates/* * .tpl$ ``` -------------------------------- ### Ruleset XML with a Specific Rule Source: https://github.com/phpcsstandards/php_codesniffer/wiki/About-Standards-for-PHP_CodeSniffer An example of a ruleset.xml file that includes a specific rule, 'Generic.WhiteSpace.DisallowTabIndent', to enforce space indentation. Replace 'YourStandardName' with your actual standard name. ```xml ``` -------------------------------- ### Specify Coding Standard by Name Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Usage Use the --standard argument to specify a coding standard by its installed name, such as PSR12. This ensures code is checked against the correct ruleset. ```bash $ phpcs --standard=PSR12 /path/to/code/myfile.inc ``` -------------------------------- ### Verbose Token Processing Output Example Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Advanced-Usage This output shows the detailed token processing for a PHP file when using the -vvv verbosity level. It includes token IDs, types, contents, and the sniffs executed on each token with their running times. ```text *** START TOKEN PROCESSING *** Process token 0: T_OPEN_TAG => Process token 2: T_IF => if Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\ControlStructures\ControlSignatureSniff... DONE in 0.0002 seconds Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\ControlStructures\MultiLineConditionSniff... DONE in 0.0001 seconds Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\WhiteSpace\ScopeClosingBraceSniff... DONE in 0.0001 seconds Processing PHP_CodeSniffer\Standards\Generic\Sniffs\ControlStructures\InlineControlStructureSniff... DONE in 0.0001 seconds Process token 3: T_WHITESPACE => Process token 4: T_OPEN_PARENTHESIS => ( Process token 5: T_VARIABLE => $condition Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionCallSignatureSniff... DONE in 0.0001 seconds Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\NamingConventions\ValidVariableNameSniff... DONE in 0.001 seconds Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\FunctionCallArgumentSpacingSniff... DONE in 0.0001 seconds Process token 6: T_CLOSE_PARENTHESIS => ) Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionCallSignatureSniff... DONE in 0.0001 seconds Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\FunctionCallArgumentSpacingSniff... DONE in 0.0001 seconds Process token 7: T_WHITESPACE => Process token 8: T_OPEN_CURLY_BRACKET => { Process token 9: T_WHITESPACE => Process token 10: T_WHITESPACE => Process token 11: T_ECHO => echo Process token 12: T_WHITESPACE => Process token 13: T_CONSTANT_ENCAPSED_STRING => 'Condition was true' Process token 14: T_SEMICOLON => ; Process token 15: T_WHITESPACE => Process token 16: T_CLOSE_CURLY_BRACKET => } Processing PHP_CodeSniffer\Standards\PEAR\Sniffs\Functions\FunctionCallSignatureSniff... DONE in 0.0001 seconds Processing PHP_CodeSniffer\Standards\Generic\Sniffs\Functions\FunctionCallArgumentSpacingSniff... DONE in 0.0002 seconds Process token 17: T_WHITESPACE => Process token 18: T_CLOSE_TAG => ?> *** END TOKEN PROCESSING *** ``` -------------------------------- ### Level Map Output Example Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Advanced-Usage The detailed token-by-token analysis of a PHP file, showing scope, level, and token type. This output is generated by PHP_CodeSniffer's internal tools for debugging and analysis. ```text *** START LEVEL MAP *** Process token 0 on line 1 [col:1;len:5;lvl:0;]: T_OPEN_TAG => Process token 2 on line 2 [col:1;len:2;lvl:0;]: T_IF => if Process token 3 on line 2 [col:3;len:1;lvl:0;]: T_WHITESPACE => Process token 4 on line 2 [col:4;len:1;lvl:0;]: T_OPEN_PARENTHESIS => ( Process token 5 on line 2 [col:5;len:10;lvl:0;]: T_VARIABLE => $condition Process token 6 on line 2 [col:15;len:1;lvl:0;]: T_CLOSE_PARENTHESIS => ) Process token 7 on line 2 [col:16;len:1;lvl:0;]: T_WHITESPACE => Process token 8 on line 2 [col:17;len:1;lvl:0;]: T_OPEN_CURLY_BRACKET => { => Found scope opener for 2:T_IF * level increased * * token 2:T_IF added to conditions array * Process token 9 on line 2 [col:18;len:0;lvl:1;conds;T_IF;]: T_WHITESPACE => Process token 10 on line 3 [col:1;len:4;lvl:1;conds;T_IF;]: T_WHITESPACE => Process token 11 on line 3 [col:5;len:4;lvl:1;conds;T_IF;]: T_ECHO => echo Process token 12 on line 3 [col:9;len:1;lvl:1;conds;T_IF;]: T_WHITESPACE => Process token 13 on line 3 [col:10;len:20;lvl:1;conds;T_IF;]: T_CONSTANT_ENCAPSED_STRING => 'Condition was true' Process token 14 on line 3 [col:30;len:1;lvl:1;conds;T_IF;]: T_SEMICOLON => ; Process token 15 on line 3 [col:31;len:0;lvl:1;conds;T_IF;]: T_WHITESPACE => Process token 16 on line 4 [col:1;len:1;lvl:1;conds;T_IF;]: T_CLOSE_CURLY_BRACKET => } => Found scope closer for 8:T_OPEN_CURLY_BRACKET * token T_IF removed from conditions array * * level decreased * Process token 17 on line 4 [col:2;len:0;lvl:0;]: T_WHITESPACE => Process token 18 on line 5 [col:1;len:2;lvl:0;]: T_CLOSE_TAG => ?> *** END LEVEL MAP *** ``` -------------------------------- ### Generate Information Report with Multiple Standards Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Reporting To get a comprehensive report, specify multiple standards using the --standard argument. This will analyze your code against a wider range of conventions. ```bash phpcs --standard=Generic,PEAR,Squiz,PSR2,Zend --report=info /path/to/code ``` -------------------------------- ### Run PHP_CodeSniffer from Phive tools directory Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/README.md Execute phpcs and phpcbf commands when installed as a project tool via Phive. The executables are located in the tools directory. ```bash ./tools/phpcs -h ./tools/phpcbf -h ``` -------------------------------- ### Selective Rule Application in ruleset.xml Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Annotated-Ruleset This example demonstrates how to use `phpcs-only` and `phpcbf-only` attributes to control rule application. Use `phpcs-only` to apply a rule only when checking code, and `phpcbf-only` to apply a rule only when fixing code. ```xml */3rdparty/* 0 ``` -------------------------------- ### View Current Configuration Options Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Advanced-Usage Use the --config-show argument to display all currently set configuration options and their values. ```bash $ phpcs --config-show Using config file: path/to/PHP_CodeSniffer/CodeSniffer.conf colors: 1 default_standard: PSR12 report_width: 150 ``` -------------------------------- ### Enforce Boolean Operator Placement Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Ensure boolean operators in control structure conditions consistently appear at the beginning or end of a line. This example forces them to the start of the line. ```xml ``` -------------------------------- ### Enable Progress Output by Default Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Configuration-Options Set the 'show_progress' configuration option to '1' to enable progress output by default. This cannot be set using `--runtime-set`. ```bash $ phpcs --config-set show_progress 1 ``` -------------------------------- ### Create Sniff Directory and File Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Coding-Standard-Tutorial Creates the necessary directory and PHP file for a custom sniff within the coding standard. ```bash cd Sniffs mkdir Commenting touch Commenting/DisallowHashCommentsSniff.php ``` -------------------------------- ### Use Generic.WhiteSpace.LanguageConstructSpacing instead of Squiz.WhiteSpace.LanguageConstructSpacing Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/CHANGELOG-4.x.md The `Squiz.WhiteSpace.LanguageConstructSpacing` sniff has been removed. Use the `Generic.WhiteSpace.LanguageConstructSpacing` sniff instead. ```php Use the Generic.WhiteSpace.LanguageConstructSpacing sniff instead. ``` -------------------------------- ### Test downloaded PHAR files Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/README.md Verify that the downloaded phpcs.phar and phpcbf.phar files are working correctly by running their help commands. ```bash php phpcs.phar -h php phpcbf.phar -h ``` -------------------------------- ### Create Coding Standard Directory Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Coding-Standard-Tutorial Sets up the basic directory structure for a new PHP_CodeSniffer coding standard named MyStandard. ```bash mkdir MyStandard mkdir MyStandard/Sniffs ``` -------------------------------- ### Align Assignment Operators by Token Start Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Configures the MultipleStatementAlignment sniff to align the start of assignment tokens instead of their end. Use when `alignAtEnd` is set to false. ```xml ``` -------------------------------- ### Configure Function Spacing (General Rule) Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Sets 1 blank line before and after functions in all cases by setting only the general spacing property. Use as a shortcut when uniform spacing is desired. ```xml ``` -------------------------------- ### CSV Report Example Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Reporting Example output of a CSV report generated by PHP_CodeSniffer. It includes columns for File, Line, Column, Type, Message, Source, Severity, and Fixable status. ```csv File,Line,Column,Type,Message,Source,Severity,Fixable "path/to/code/fileA.php",1,1,error,"Header blocks must be separated by a single blank line",PSR12.Files.FileHeader.SpacingAfterTagBlock,5,1 "path/to/code/fileA.php",10,24,error,"Expected 0 spaces after opening parenthesis; 1 found",Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterOpen,5,1 "path/to/code/fileA.php",10,26,error,"PHP parameter type declarations must be lowercase; expected \"string\" but found \"String\"",Generic.PHP.LowerCaseType.ParamTypeFound,5,1 "path/to/code/fileA.php",10,26,error,"Expected 1 space between type hint and argument \"$param\"; 2 found",Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterHint,5,1 "path/to/code/fileA.php",12,136,warning,"Line exceeds 120 characters; contains 136 characters",Generic.Files.LineLength.TooLong,5,0 "path/to/code/fileA.php",13,5,error,"Line indented incorrectly; expected at least 8 spaces, found 4",Generic.WhiteSpace.ScopeIndent.Incorrect,5,1 "path/to/code/fileA.php",13,12,error,"TRUE, FALSE and NULL must be lowercase; expected \"false\" but found \"FALSE\"",Generic.PHP.LowerCaseConstant.Found,5,1 ``` -------------------------------- ### Create ruleset.xml File Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Coding-Standard-Tutorial Creates the ruleset.xml file required for a PHP_CodeSniffer coding standard. ```bash cd MyStandard touch ruleset.xml ``` -------------------------------- ### Generator Class Method Replacements in PHP_CodeSniffer 4.0 Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Version-4.0-Developer-Upgrade-Guide Various print*(): void methods in Generator classes have been replaced with get*(): string methods. Developers extending Generator classes should implement the new get*() methods. ```php PHP_CodeSniffer\Generators\Text::printTitle() -> PHP_CodeSniffer\Generators\Text::getFormattedTitle() PHP_CodeSniffer\Generators\Text::printTextBlock() -> PHP_CodeSniffer\Generators\Text::getFormattedTextBlock() PHP_CodeSniffer\Generators\Text::printCodeComparisonBlock() -> PHP_CodeSniffer\Generators\Text::getFormattedCodeComparisonBlock() PHP_CodeSniffer\Generators\Markdown::printHeader() -> PHP_CodeSniffer\Generators\Markdown::getFormattedHeader() PHP_CodeSniffer\Generators\Markdown::printFooter() -> PHP_CodeSniffer\Generators\Markdown::getFormattedFooter() PHP_CodeSniffer\Generators\Markdown::printTextBlock() -> PHP_CodeSniffer\Generators\Markdown::getFormattedTextBlock() PHP_CodeSniffer\Generators\Markdown::printCodeComparisonBlock() -> PHP_CodeSniffer\Generators\Markdown::getFormattedCodeComparisonBlock() PHP_CodeSniffer\Generators\HTML::printHeader() -> PHP_CodeSniffer\Generators\HTML::getFormattedHeader() PHP_CodeSniffer\Generators\HTML::printToc() -> PHP_CodeSniffer\Generators\HTML::getFormattedToc() PHP_CodeSniffer\Generators\HTML::printFooter() -> PHP_CodeSniffer\Generators\HTML::getFormattedFooter() PHP_CodeSniffer\Generators\HTML::printTextBlock() -> PHP_CodeSniffer\Generators\HTML::getFormattedTextBlock() PHP_CodeSniffer\Generators\HTML::printCodeComparisonBlock() -> PHP_CodeSniffer\Generators\HTML::getFormattedCodeComparisonBlock() ``` -------------------------------- ### Set Path to PHP Binary Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Configuration-Options Specify the location of the PHP binary for the `Generic.PHP.Syntax` sniff to use for checking PHP file syntax. ```bash $ phpcs --config-set php_path /path/to/php ``` -------------------------------- ### Ruleset XML for Namespace Prefix Source: https://github.com/phpcsstandards/php_codesniffer/wiki/About-Standards-for-PHP_CodeSniffer Illustrates the `ruleset.xml` configuration needed to specify a namespace prefix for a standard. ```xml ``` -------------------------------- ### Basic Class Definition Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/tests/Core/Generators/Expectations/ExpectedOutputStructureDocs.html A simple class definition used in code comparison examples. ```php class Code {} ``` ```php class Comparison {} ``` -------------------------------- ### PHP Concatenation with Single Spacing Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Example of string concatenation with single spaces around the operator. ```PHP $foo = $number . '-' . $letter; ``` -------------------------------- ### Example of Newline After Not Operator Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Illustrates a scenario where a newline follows the '!' operator, which can be ignored by the sniff if configured. ```php if ( ($foo || $bar) ) { } ``` -------------------------------- ### PHP Concatenation with Zero Spacing Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Example of string concatenation with zero spaces around the operator, as enforced by default. ```PHP $foo = $number.'-'.$letter; ``` -------------------------------- ### Configure For Loop: Single Space Padding Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties XML configuration to set `requiredSpacesAfterOpen` and `requiredSpacesBeforeClose` to 1 for the Squiz.ControlStructures.ForLoopDeclaration sniff. ```xml ``` -------------------------------- ### Invalid Class Definition (Missing Title) Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/tests/Core/Generators/Expectations/ExpectedOutputInvalidDocumentationTitleMissing.md An example of an invalid class definition due to a missing title attribute in its documentation. ```php class Comparison {} ``` -------------------------------- ### Using a Custom Ruleset XML File Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Annotated-Ruleset Specify a custom ruleset XML file using the --standard argument with the phpcs command. ```bash $ phpcs --standard=/path/to/custom_ruleset.xml test.php ``` -------------------------------- ### Control Signature: Default Alternative Syntax Spacing Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties Demonstrates the default spacing for control structures using alternative syntax, with one space before the colon. ```php if ($foo) : // IF body. else : // ELSE body. endif; ``` -------------------------------- ### Empty Class Definition Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/tests/Core/Generators/Expectations/ExpectedOutputInvalidStandardNoContent.html An example of an empty class definition in PHP. This is used to represent a standard element without content. ```php class Code {} ``` ```php class Comparison {} ``` -------------------------------- ### Configure Generic.Formatting.MultipleStatementAlignment to produce errors Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/CHANGELOG-4.x.md To make the `Generic.Formatting.MultipleStatementAlignment` sniff produce errors instead of warnings, include this rule in your `ruleset.xml` or `[.]phpcs.xml[.dist]` file. ```xml error ``` -------------------------------- ### Configure ForEach Loop: Single Space Padding Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Customisable-Sniff-Properties XML configuration to set `requiredSpacesAfterOpen` and `requiredSpacesBeforeClose` to 1 for the Squiz.ControlStructures.ForEachLoopDeclaration sniff. ```xml ``` -------------------------------- ### Use Generic.Formatting.SpaceAfterCast instead of NoSpaceAfterCast Source: https://github.com/phpcsstandards/php_codesniffer/blob/4.x/CHANGELOG-4.x.md The `Generic.Debug.CSSLint` sniff has been removed. Use the `Generic.Formatting.SpaceAfterCast` sniff instead, setting the `$spacing` property to `0` for similar behavior. ```php Use the Generic.Formatting.SpaceAfterCast sniff instead with the $spacing property set to 0. ``` -------------------------------- ### Enable Progress Reporting Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Usage Use the -p argument to see a single-character status for each file being checked. This is useful for monitoring progress on large codebases. ```bash $ phpcs /path/to/project -p ...S........W.........S..................................... 60 / 110 (54%) ..........EEEE.E.E.E.E.E.E.E.E..W..EEE.E.E.E.EE.E. 110 / 110 (100%) ``` -------------------------------- ### Customize line length limits Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Annotated-Ruleset Configure the 'Generic.Files.LineLength' sniff to show warnings for lines longer than 90 characters and errors for lines longer than 100 characters. ```xml ``` -------------------------------- ### Set Runtime Configuration Options Source: https://github.com/phpcsstandards/php_codesniffer/wiki/Advanced-Usage Use the --runtime-set argument to apply configuration options for a single PHP_CodeSniffer run only. Note that not all options support this method. ```bash $ phpcs --runtime-set