PHP_CodeSniffer Documentation

repository·master·Indexed 27 days ago

https://github.com/squizlabs/php_codesniffer

A toolset for detecting and correcting coding standard violations in PHP, JavaScript, and CSS files. It includes phpcs for detecting violations and phpcbf (PHP Code Beautifier and Fixer) for automatically correcting them. Supports installation via Phar files, Composer, Phive, PEAR, and Git.

Tokens
2.2K
Snippets
3
Records
17
Agent score
93%

What's inside PHP_CodeSniffer

  1. Install PHP_CodeSniffer via Composer

    master

    You can install PHP_CodeSniffer globally or as a development dependency in your project using Composer.

    Global Installation: Ensure your composer bin directory (e.g., ~/.composer/vendor/bin/) is in your PATH.

    Project Dependency: Add it to your composer.json to run it via the local vendor/bin directory.

    # Global installation
    composer global require "squizlabs/php_codesniffer=*"
    
    # Project dependency (composer.json)
    {
        "require-dev": {
            "squizlabs/php_codesniffer": "3.*
        }
    }
    
    # Running from vendor bin
    ./vendor/bin/phpcs -h
    ./vendor/bin/phpcbf -h
  2. Install PHP_CodeSniffer via Phive, PEAR, or Git

    master

    PHP_CodeSniffer can be installed using several other methods:

    • Phive: Install as project tools using phive install phpcs and phive install phpcbf.
    • PEAR: Use the PEAR installer with pear install PHP_CodeSniffer.
    • Git Clone: Clone the repository directly and run the scripts from the bin/ directory.
    # Phive
    phive install phpcs
    phive install phpcbf
    ./tools/phpcs -h
    
    # PEAR
    pear install PHP_CodeSniffer
    
    # Git Clone
    git clone https://github.com/squizlabs/PHP_CodeSniffer.git
    cd PHP_CodeSniffer
    php bin/phpcs -h
    php bin/phpcbf -h
  3. Install PHP_CodeSniffer via Phar files

    master

    The quickest way to install PHP_CodeSniffer is to download the standalone Phar files for the phpcs (detector) and phpcbf (corrector) commands using curl or wget. After downloading, you can verify the installation by running the -h flag.

    # Download using curl
    curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
    curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
    
    # Or download using wget
    wget https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
    wget https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
    
    # Then test the downloaded PHARs
    php phpcs.phar -h
    php phpcbf.phar -h
  4. Detect coding standard violations with phpcs

    master
    Use the phpcs script to tokenize PHP, JavaScript, and CSS files to detect violations of a defined coding standard. By default, it uses the PEAR coding standard. You can specify a different standard using the --standard flag.
  5. Configure the Notifysend report

    master

    The Notifysend report allows PHP_CodeSniffer to send desktop notifications via the notify-send CLI tool. You can customize its behavior using the following configuration parameters in your phpcs.xml file or via command line configuration:

    • notifysend_path: The full path to the notify-send executable.
    • notifysend_timeout: The timeout for the notification in milliseconds (default is 3000).
    • notifysend_showok: A boolean (0/1) determining whether to show an "ok, all fine" message when no errors or warnings are found (default is true).
  6. Configure ScopeIndentSniff via ruleset

    master

    The Generic.WhiteSpace.ScopeIndent sniff enforces correct indentation for control structures. You can customize its behavior in your ruleset.xml file using the following properties:

    • indent: The number of spaces used for indentation (default: 4).
    • exact: If set to true, the indentation must match the indent value exactly. If false, the indentation must be at least the indent value but can be more.
    • tabIndent: If set to true, the sniff will use tabs instead of spaces for indentation. Note that the width of the tabs is determined by the --tab-width CLI argument.
    • ignoreIndentationTokens: A list of tokens that should be ignored by this sniff.
  7. Debug ScopeIndentSniff indentation errors

    master

    If you are troubleshooting why indentation is being flagged or how the sniff is calculating expected indents, you can enable debug mode. When debug is true, the sniff outputs detailed information to the console, including:

    • The line number and type of token being processed.
    • The calculated expected indentation.
    • Whether a closure, method prefix, or JS object was found.
    • Adjustments being made to the indentation levels.

    Note: This is typically controlled via the sniff's configuration properties within your ruleset.xml.

  8. Understand ScopeIndentSniff behavior

    master

    The ScopeIndentSniff is a coding standard sniff used to enforce consistent indentation levels for different code structures. It handles various tokens including:

    • PHP Scopes: Closures (T_CLOSURE), anonymous classes (T_ANON_CLASS), and standard scope openers (e.g., if, else, while).
    • PHP Tags: T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, and T_CLOSE_TAG are checked to ensure they align correctly with the surrounding code.
    • Special Keywords: T_ELSE statements are checked for exact indentation to prevent breaking subsequent code blocks.
    • JavaScript Objects: In JS files, T_OBJECT tokens trigger indentation checks for object properties.
    • Exceptions: The sniff automatically skips or applies relaxed rules to:
      • T_START_HEREDOC and T_START_NOWDOC (content-dependent indentation).
      • T_CONSTANT_ENCAPSED_STRING and T_DOUBLE_QUOTED_STRING (multi-line strings).
      • T_DOC_COMMENT_OPEN_TAG (doc comments).
      • Chained method calls (T_OBJECT_OPERATOR or T_NULLSAFE_OBJECT_OPERATOR) when exact indentation is already set.
  9. Configure ScopeIndentSniff properties

    master

    When defining the Generic.WhiteSpace.ScopeIndent sniff in a ruleset, you can set these properties:

    PropertyTypeDefaultDescription
    indentinteger4The number of spaces code should be indented.
    exactbooleanfalseIf true, indent must be exactly $indent spaces. If false, indent must be at least $indent spaces.
    tabIndentbooleanfalseIf true, fixes will use tabs instead of spaces.
    ignoreIndentationTokensarray[]List of tokens not needing to be checked for indentation.