Our WorkAbout UsOur ApproachThe Journal
Contact »
« Back to the journal
PHP, Javascript & React

Configure PHP CodeSniffer for Mac OS X

By:

Joel Jacob

on 3/3/2015

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. sudo pear install PHP_CodeSniffer 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

Install PHP_CodeSniffer

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

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

  1. 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

Fix the include_path errors

To fix the include_path errors, run the following commands:

  1. 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 ----------------------------------------------------------------------

Verify it works

Now phpcs will run without errors:

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.

Share to

Related Posts

Websites
What's the JAMstack?

By:Alec Robertson

As a front-end developer in 2020, it is time that I fulfill the FEDeral mandate that I publish words regarding the JAMStack. If you follow too many developers on Twitter, it’s likely you’ve heard about the Next Big Thing in web development, but I’d like to put this trend (fad? movement?) into context to better understand what’s truly new about this approach and whether it’s worth using.

Read More »
Recursive asynchronous calls with JavaScript and ES6

By:Mark Biek on 1/30/2018

ES6 generator functions make recursive asynchronous calls (relatively) easy.

Read More »