Hey there! You're using an out-of-date browser, so this site probably looks pretty funny. Upgrade your browser for the full experience.
code sniffer

Configure PHP CodeSniffer for Mac OS X

PHP CodeSniffer is a tool that checks code against a defined set of rules, covering anything from whitespace through doc comments to class naming conventions and beyond. We've been using it for about a year and it's worked out great. Our team now has a unified coding style and the CodeSniffer tool ensures that we're always following it.

Adhering to a coding convention can have many benefits including improved readability and reduction in maintenance cost. However, maintaining a consistent coding style can be a tedious task. The problem is confounded when you work as part of a team of developers, each with their own unique coding style.

PHP CodeSniffer is a tool that checks code against a defined set of rules, covering anything from whitespace through doc comments to class naming conventions and beyond. We’ve been using it for about a year and it’s worked out great. Our team now has a unified coding style and the CodeSniffer tool ensures that we’re always following it.

The following commands will install PHP_CodeSniffer on Mac OS X Yosemite and help you overcome a common set up problem.

  1. Install PHP_CodeSniffer

    Per the PHP_CodeSniffer documentation, ensure you have PEAR installed and then run:

    sudo pear install PHP_CodeSniffer

    PHP_CodeSniffer is installed at this point but if you try to run phpcs, you may receive some errors about the include_path:

    phpcs Sample.php
    
    Warning: include_once(PHP/CodeSniffer/CLI.php): failed to open stream: No such file or directory in /usr/local/bin/phpcs on line 21
    
    Warning: include_once(): Failed opening 'PHP/CodeSniffer/CLI.php' for inclusion (include_path='.:') in /usr/local/bin/phpcs on line 21
    
    Fatal error: Class 'PHP_CodeSniffer_CLI' not found in /usr/local/bin/phpcs on line 24
  2. Fix the include_path errors

    To fix the include_path errors, run the following commands:

    sudo mkdir -p /Library/Server/Web/Config/php
    
    sudo touch /Library/Server/Web/Config/php/local.ini
    
    echo 'include_path = ".:'`pear config-get php_dir`'"' | sudo tee -a /Library/Server/Web/Config/php/local.ini
  3. Verify it works

    Now phpcs will run without errors:

    phpcs Sample.php
    
    FILE: Sample.php
    ----------------------------------------------------------------------
    FOUND 3 ERRORS AFFECTING 3 LINES
    ----------------------------------------------------------------------
      8 | ERROR | [ ] Visibility must be declared on method
        |       |     "find" (Squiz.Scope.MethodScope.Missing)
      9 | ERROR | [ ] Opening brace should be on the same line as the
        |       |     declaration
        |       |     (Via.Functions.SingleLineFunctionDeclaration.BraceOnNewLine)
     10 | ERROR | [x] Line indented incorrectly; expected at least 8
        |       |     spaces, found 4
        |       |     (Generic.WhiteSpace.ScopeIndent.Incorrect)
    ----------------------------------------------------------------------
    PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
    ----------------------------------------------------------------------

Many of the developers and Via have integrated phpcs with SublimeText for instant feedback on standard violations. Check out our GitHub repo for information on setting it up as well as our custom ruleset that we have created.

Born and raised in
Louisville, Kentucky.

502.498.8470

223 S. Clay St

Work With Us

  • This field is for validation purposes and should be left unchanged.