You are here
Drupal PHP Code review and beautification made easy
Hola,
It has been long time I invested time for automated code reviews. In Drupal 7 and previous versions there were few useful debug tools and modules such as coder module for same purpose. Coder with Krumo was very useful debug tool and could give you overview of issues present under the module you just custom developed. It used Drupal version's coding standards for showing minor, major errors in your code.
Things are bit different now with Drupal 8 coming. We have better tools as we have moved from Non-Object oriented Drupal versions to actually object oriented Drupal codes. It makes more sense to use the tools available for PHP for the code review such as PHP CodeSniffer.
PHP CodeSniffer- It is very useful tool for automated code review for Drupal 8 and PHP coding conventions.
If you wish to build it from the source code it is present
https://github.com/squizlabs/PHP_CodeSniffer.git here.
If you have the pear package of PHP installed on your system it will be easy for you to install PHPCS just use following command:
pear install PHP_CodeSniffer
For PEAR 2 people you can use following command:
php pyrus.phar install pear/PHP_CodeSniffer
Alternatively if you use mac and use .phar extensions to install packages you can use following commands:
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar
php phpcs.phar -h
curl -OL https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar
php phpcbf.phar -h
Ohh. I forgot to mention its not only code review but code beautifier tool which is PHP CBF it checks for the code sniffer output and fixes the code indentation and beautification by itself with simple commands. This is super useful for developers while writing code itself or once they complete the module they can code review with PHPCS.
Using this tool simply requires terminal / command prompt and can be used as follows:
phpcs /path/to/code/myfile.php
To use code beautification and fixing tool use following command:
phpcbf /path/to/code/myfile.php
It will fix the warnings and notices related to code indentation in your module quicker than anything.
Please note: phpcbf will not fix all the issues shown in the phpcs because it may not be able to fix issues which are logical or syntax related.
For instance, issues reported by phpcs for documentation / number of characters per line cannot be fixed by phpcbf:
1. Missing short description in doc comment
2. Line exceeds 85 characters
3. PHP version not specified
4. Missing @category tag in file comment
5. Missing @package tag in file comment
6. Missing @license tag in file comment
7. Missing @link tag in file comment
8. Missing @return tag in function comment
9. New line for { curly brackets
For detailed usage you should visit :
https://github.com/squizlabs/PHP_CodeSniffer/wiki
Overall PHPCS and PHPCBF are perfect tools for developers who are interested in neat and clean code with all conventions followed, in short A QUALITY CODE development.
Hope you guys liked it... Adios por ahora !!!