PHP_CodeSniffer para WordPress

Este post solo tiene la finalidad de registrar el proceso de instalación de PHP_CodeSniffer y configurarlo para los requerimientos de WordPress, en MAC OS El Capitan.

Primero instalamos PEAR: Install PEAR and PECL on Mac OS X

curl -O http://pear.php.net/go-pear.phar
sudo php -d detect_unicode=0 go-pear.phar

Podemos Configurarlo en la instalación o bien como en mi caso, luego:

sudo pear config-set php_bin /usr/bin/php
sudo pear config-set bin_dir /usr/local/bin

luego instalamos PHP_CodeSniffer

sudo pear install PHP_CodeSniffer

Y voilà:

phpsc --version

Debería funcionar, pero no, da algunos errores:

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

Necesitamos hacer una configuración más: http://viastudio.com/configure-php-codesniffer-for-mac-os-x/

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

y con eso corregimos los errores de include_path y tenemos nuestro comamndo phpsc funcionando en la consola.

Ahora vamos a integrar el WordPress Coding Standards for PHP_CodeSniffer, para ajustarnos a sus parametros.

git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
sudo phpcs --config-set installed_paths /path/to/wpcs

ahora probamos la herramienta

phpcs --standard=WordPress path/to/file.php

o

phpcs --standard=WordPress path/to/fdir

y este es un posible resultado:

----------------------------------------------------------------------
FOUND 6 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
16 | ERROR | [x] Expected 1 spaces after opening bracket; 0 found
16 | ERROR | [x] Expected 1 spaces before closing bracket; 0 found
16 | ERROR | [ ] Expected next thing to be an escaping function
| | (like esc_html_e() or esc_attr_e()), not '_e'
19 | ERROR | [x] Expected 1 spaces after opening bracket; 0 found
19 | ERROR | [x] Expected 1 spaces before closing bracket; 0 found
19 | ERROR | [ ] Expected next thing to be an escaping function
| | (like esc_html_e() or esc_attr_e()), not '_e'
----------------------------------------------------------------------
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------
Time: 117ms; Memory: 11Mb

y tal como lo indica ahi, con:

phcbf --standard=WordPress path/to/file.php

resolvemos automáticamente alguno de los errores.