Konrad Podgórski - Web Developer

Personal blog about developing web applications with PHP

How to write better PHP code

Please keep in mind that this post was written more than 2 years ago and might be outdated.

Simple answer: use automated code inspectors with ruleset (coding standard) that is official for framework you currently use.

Installing Code Sniffer for PHP

    sudo pear install PHP_CodeSniffer

DO NOT use version from other package managers because its seriously outdated at the moment and won’t work with most of IDE’s

Configure IDE

Personally I use PHP Storm, if you use other IDE unfortunately you’re on your own

File -> Default Settings -> PHP -> Code Sniffer

PHP code sniffer configuration

Enable in project

This step has to be repeated in each project, however if you for example develop only Symfony 2 applications you can set it to enabled by default in Default Settings, from this moment each project created will have this validation enabled by default.

File -> Settings -> Inspections -> PHP -> PHP Code Sniffer validation

PHP code sniffer validation

In Coding standard select ruleset you want to use

Ruleset for PSR-2


Ruleset for Symfony 2

https://github.com/opensky/Symfony2-coding-standard If you encounter any problem with this how to please leave a comment so I can improve it. If this post helped you let me know, it’s always nice to know that I could help someone :)


Find this post helpful? Spread the word, thanks.