Install & configure PHP CodeSniffer on MAC OS X

PHP_CodeSniffer is a set of two PHP scripts; the main phpcs script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a second phpcbf script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.

Download PEAR
$ curl -O
$ sudo php -d detect_unicode=0 go-pear.phar

Below is a suggested file layout for your new PEAR installation.  To change individual locations, type the number in front of the directory.  Type 'all' to change all of them or simply press Enter to
accept these locations.
 1. Installation base ($prefix): /usr/local/pear
 2. Temporary directory for processing: /tmp/pear/install
 3. Temporary directory for downloads: /tmp/pear/install
 4. Binaries directory: /usr/local/pear/bin
 5. PHP code directory ($php_dir) : /usr/local/pear/share/pear
 6. Documentation directory: /usr/local/pear/docs
 7. Data directory: /usr/local/pear/data
 8. User-modifiable configuration files directory : /usr/local/pear/cfg
 9. Public Web Files directory: /usr/local/pear/www
10. System manual pages directory: /usr/local/pear/man
11. Tests directory: /usr/local/pear/tests
12. Name of configuration file: /private/etc/pear.conf

After download, install and configure, you should check the version by the command:
$ pear version

Run the following command to install PHP_CodeSniffer
$ sudo pear install PHP_CodeSniffer

To verify phpcs works:
$ phpcs example.php

If you receive the following error:
Warning: include_once(PHP/CodeSniffer/autoload.php): failed to open stream: No such file or directory in /usr/local/bin/phpcs on line 14
Warning: include_once(): Failed opening 'PHP/CodeSniffer/autoload.php' for inclusion (include_path='.:') in /usr/local/bin/phpcs on line 14
Fatal error: Class 'PHP_CodeSniffer\Runner' not found in /usr/local/bin/phpcs on line 17

Please add to php.ini
$ sudo vim /etc/php.ini
this line:
include_path = ".:/usr/local/pear/share/pear"

No comments:

Post a Comment