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

PHP, Javascript & React
Wordpress to Sanity Data Script

By:Nick Stewart on 3/11/2021

One of the biggest challenges of moving from one CMS to another is getting your existing data to the new platform. In our case, we were moving our existing WordPress platform, which had been around for years, over to the Sanity.io platform.

Read More »
Developing the New via.studio

By:Alec Robertson on 6/14/2021

A deep dive into creating a fast, user-focused experience on the latest web technology stack for the new via.studio website.

Read More »