PHP_CodeSniffer Documentation

repository·4.x·Indexed 23 days ago

https://github.com/phpcsstandards/php_codesniffer

A toolset for detecting and automatically fixing violations of PHP coding standards. It includes two primary utilities: phpcs for detecting violations in PHP, JS, and XML files, and phpcbf (PHP Code Beautifier and Fixer) for automatic correction. The tool supports various standards such as PSR12, PEAR, and Zend, and allows for custom configuration via ruleset.xml files, CLI flags, and installed_paths.

Tokens
29.6K
Snippets
29
Records
307
Agent score
82%

What's inside PHP_CodeSniffer

  1. What is PHP_CodeSniffer

    4.x

    PHP_CodeSniffer consists of two primary scripts designed to maintain code cleanliness and consistency:

    1. phpcs: Tokenizes PHP files to detect violations of defined coding standards.
    2. phpcbf: Automatically corrects coding standard violations found by phpcs.

    This package is the official continuation of the original Squizlabs PHP_CodeSniffer package.

  2. Handle PHP 8.2 Disjunctive Normal Form (DNF) types

    4.x

    Starting from version 3.10.0, PHP_CodeSniffer includes tokenizer support for PHP 8.2 Disjunctive Normal Form (DNF) types.

    Key updates include:

    • New tokens T_TYPE_OPEN_PARENTHESIS and T_TYPE_CLOSE_PARENTHESIS are used to represent parentheses in DNF types. These tokens include parenthesis_opener and parenthesis_closer indexes, and tokens between them include a nested_parenthesis index.
    • The following methods now support DNF types: File::getMethodProperties(), File::getMethodParameters(), and File::getMemberProperties().
    • The Generic.PHP.LowerCaseType sniff has been updated to support DNF types.
  3. How addFixableError() and addFixableWarning() behave

    4.x
    In version 2.0.0RC1, addFixableError() and addFixableWarning() were updated to return true only if the fixer is actually enabled. This allows developers to avoid manual checks like if ($phpcsFile->fixer->enabled === true) before attempting to apply a fix.
  4. Configure ruleset file discovery and precedence

    4.x

    PHP_CodeSniffer automatically searches for configuration files to define standards.

    • Search Path: PHPCS looks for phpcs.xml in the current directory and parent directories.
    • File Precedence: If both phpcs.xml and phpcs.xml.dist are present, phpcs.xml takes precedence.
    • Command Line Override: If you specify a standard directly on the command line, the phpcs.xml file is ignored. However, if no standard is specified via CLI, the ruleset file is used.
  5. Handle deprecated ExactMatch filter methods for 4.0 compatibility

    4.x

    In version 3.9.0, the methods getBlacklist() and getWhitelist() in the PHP_CodeSniffer\Filters\ExactMatch class were deprecated and will be removed in version 4.0. They are replaced by getDisallowedFiles() and getAllowedFiles().

    To ensure a custom filter is compatible with both PHP_CodeSniffer 3.9.0+ and 4.0+, implement both sets of methods. When both are present, the new methods (getDisallowedFiles and getAllowedFiles) take precedence.

  6. Use external coding standards via full paths

    4.x
    You can specify the full filesystem path to a coding standard on the command line. This allows you to use standards that are stored outside of the PHP_CodeSniffer Standard directory. When using external standards, you can also specify full paths within the CodingStandard.php include and exclude methods. Note that classes, directories, and files should be named as if the standard were part of the internal PHP_CodeSniffer structure.
  7. Manage output streams (STDOUT vs STDERR)

    4.x

    As of 4.0.0beta1, all status, debug, and progress output is sent to STDERR instead of STDOUT. Only report output is sent to STDOUT.

    If you are piping output to a file and want to capture the entire run (including status and timing), you must pipe both STDERR and STDOUT to the same file.

  8. Handle End-of-Line (EOL) characters in custom sniffs

    4.x
    PHP_CodeSniffer auto-detects the EOL character. When writing pattern sniffs, do not hard-code \n or \r\n. Instead, use the EOL keyword or access the character via the $phpcsFile->eolChar property to ensure compatibility with the detected line endings.
  9. Handle invalid sniff properties in custom rulesets

    4.x

    In version 3.8.0, the handling of invalid sniff properties in custom rulesets changed:

    • Individual Sniffs: Setting an invalid property for an individual sniff will now result in an error and halt execution. A descriptive error message will be provided.
    • Complete Standards/Categories: If properties are set for a complete standard or category, they will only be applied to sniffs that explicitly support them; otherwise, they are silently ignored.
    • Inline Annotations: Invalid properties set via inline annotations will trigger an Internal.PropertyDoesNotExist error on line 1 of the scanned file but will not halt execution.

    For Sniff Developers: It is strongly recommended to explicitly declare any user-adjustable public properties. Using #[\\[AllowDynamicProperties\\]] will not affect properties being set via rulesets.

  10. Deprecated Generator methods in PHP_CodeSniffer

    4.x

    As part of the preparation for version 4.0, several print* methods in the Generator classes have been deprecated in favor of getFormatted* methods. You should update your custom generators to use the new naming convention to ensure compatibility with future versions.

    Deprecated Text methods:

    • printTitle() $\rightarrow$ getFormattedTitle()
    • printTextBlock() $\rightarrow$ getFormattedTextBlock()
    • printCodeComparisonBlock() $\rightarrow$ getFormattedCodeComparisonBlock()

    Deprecated Markdown methods:

    • printHeader() $\rightarrow$ getFormattedHeader()
    • printFooter() $\rightarrow$ getFormattedFooter()
    • printTextBlock() $\rightarrow$ getFormattedTextBlock()
    • printCodeComparisonBlock() $\rightarrow$ getFormattedCodeComparisonBlock()

    Deprecated HTML methods:

    • printHeader() $\rightarrow$ getFormattedHeader()
    • printToc() $\rightarrow$ getFormattedToc()
    • printFooter() $\rightarrow$ getFormattedFooter()
    • printTextBlock() $\rightarrow$ getFormattedTextBlock()
    • printCodeComparisonBlock() $\rightarrow$ getFormattedCodeComparisonBlock()