On Meet Magento 2014 I attended an interesting presentation about Continues Integration in Magento from Damian Luszczymak. One important aspect of Continuous Integration is that as a team of developers everyone should follow the same coding guidelines. If you use PHPStorm, you can use PHP CodeSniffer to ensure that the code you are writing as a team is readable by your colleagues.
“Why is this important?” you might ask. Well… if every programmer in your team is programming in the same style, it’s easier for programmer A to read, edit or debug the code of programmer B.
Setting it all up
Installing PHP CodeSniffer on OSX is extremely easy. Just open up your best friend the terminal and install it:
$ sudo pear install PHP_CodeSniffer
Now, when all is set and done, check if PHP CodeSniffer works by entering the command phpcs in your terminal. If all went well, you should see just a blinking cursor. You can press Ctrl+C now to exit it. If you get a bunch of PHP-errors telling you they can’t include a file, you need to update include_path your in /etc/php.ini :
First locate where your pear is installed (on my system that’s /usr/local/pear ) and add this path including share/pear as a path to your include_path in /etc/php.ini :
include_path = ".:/usr/local/pear/share/pear/"
Now executing phpcs on your command line should not throw any errors.
Check where your phpcs is located by executing which phpcs in your terminal. In PHPStorm, you need to enter this path in the configuration at Project Settings > PHP > Code Sniffer. By clicking on the ‘Validate’-button you can check if everything is correct:
Now the second thing you need to do for your project configuration is at Project Settings > Inspections > PHP’ enable ‘PHP Code Sniffer Validation’ and select the Coding standard you wish to use. You might have to press the refresh button to see them all but they’re there:
That’s it: your PHPStorm now shows you where your code is not following the rules:
Update: I also found this nice tool to generate your own ruleset.
Visitors give this article an average rating of 3.5 out of 5.
How would you rate this article?
★ ★ ★ ★ ★