Improve your PHP Code

How to improve the quality of your PHP code? Part 1 – Automated tools

Let’s be honest. PHP has a very bad press when it comes to code quality. By reading this series of articles, you will learn how to improve the quality of your PHP code.

We could blame this on many reasons, but surely it’s not just because the PHP ecosystem lacks proper testing tools. In this article, I want to show you an easy set-up for the basic quality testing of your project. I will not go into details of any particular tool, but rather focus on setting the environment for testing.

There is a demonstration code for this article available on GitHub: – please refer to it if you have any problems with examples from this article.


I assume that you are familiar with PHP 7.1 syntax, you use Composer and follow PSR-4 for autoloading and PSR-1PSR-2 for coding standards. In my examples vendor’s binaries are installed to ./bin directory.

Build tool

We are going to use a few different testing tools, so it’s good to have something that would run them all with just one script. PHing provides us with an excellent solution to this problem. PHing is similar to Apache Ant and allows to easily automate tasks using XML configuration. You install it by running

Then you create some basic build.xml file in your project’s root directory.

In next steps we will add some targets to run by PHing.

Static code analysis

The first thing you can do to improve your code quality at almost no cost is setting up static code analyzers. They will check your code for mistakes by just reading it without really running it. It’s like having a code-review done in a few seconds by a robot. Cool, huh?

Code style

Your code is much more maintainable when it is written using proper styling. Everybody knows that (if you don’t, you should at least start reading “Clean Code” by Robert C. Martin), but still many teams have problems keeping the standards they’ve agreed on. It’s a miracle that we can automate this task with phpcs – PHP Code Sniffer.

You can install it by running

Then add a target for running it in build.xml. Your build.xml should now look like this:

Now you can run ./bin/phing and phpcs will automatically check if you have any mistakes against PSR-1 and PSR-2 coding standards.

Many frameworks, like Symfony, define their own rules for code style and thanks to the community we can have those checked automatically as well. Eg. if you are using Symfony framework, check for information on how to check Symfony’s standards with PHPCS.

Example output of a wrongly formatted file:

No more losing time on checking coding standards during code-review, it will be automated from now on!

Copy/Paste Detector

Duplicated code is bad, everybody knows it. Sometimes we create such code by mistake and we never notice it. Sometimes we do because we are lazy. It’s better to be equipped with a tool which will prompt about this at build time. Here comes PHPCPD – PHP Copy/Paste Detector. You install it by running

Then you just add target to your build.xml:

Example output of a duplicated code check run on vendors directory:

Want really deep code analysis?

If you are starting your project from scratch you should take a look at Phan – it’s a really powerful code analyser which will make your code beautiful. Check it out at

Installation is very easy – just install php-ast extension (in Ubuntu you can try to run sudo apt-get install php-ast) and run:

Then create a config file .phan/config.php with content:

Also create phan target in your build.xml:

You can now run your code analysis and if you made a mistake (eg. declared wrong phpdoc type for a class property), you should see a message like this:

Phan is magical – it reads your whole code and performs multiple checks on it, including comparing phpdoc declarations with real usage of variables, methods, classes, etc. You can check the whole list of features on

Summary of part I

You have now three fully automated tools in your project which will guard your code’s quality. All you need to do is run ./bin/phing manually, or attach it to your git-hook or continuous integration. Your code will be checked for coding standards, duplications and formal mistakes. Just those checks should result in more reliable run-time and less time spent on code review. It’s now time to grab some coffee and celebrate! In next part(s) we will begin writing some proper tests that will cover your business functions.

Subscribe to our newsletter