Add new comment

High Quality PHP

The historical perception of PHP in the software development community has not been the best. Some think it is a language for non-programmers and tinkerers, generally resulting in software of questionable quality. From its beginnings as a simple scripting language for online guests-books, PHP has transformed itself in the last 15 years to support high-quality software development and enterprise deployment, and it has managed to do so without losing the roots of its initial success.

Getting Things Done with Quality in Mind

The focus of PHP has always been on "getting things done" and the language’s developers still value this goal highly. This practicality has spawned great open-source software such as phpBB, Drupal, Wordpress and various other well known applications that are hosted on millions of servers worldwide. Many PHP developers commit to learning these open-source platforms and developing custom applications on top of them. This approach of building extendable open-source platforms has served the PHP community well and lead to the majority of websites online today being run on PHP-based software.

With the wide-spread adoption and use of PHP, quality assurance and long-term maintainability have become increasingly important. Open source projects and development companies realize that they need to adopt new techniques and tools to continue satisfying their customers in the long term. Over the years, the PHP community has done an amazing job of teaching interested developers about high quality software, the problems of global state, spaghetti-code, and high coupling at local user groups, conferences, and training courses around the world.

In my experience, PHP has often been the “last one at the table,” when new development practices or tools are developed. The Java, Ruby, and Python communities are possibly more bleeding-edge innovative, but there are significant benefits to adopting a wait-and-see approach in these cases. Once a technique or practice has proven itself in practice and matured somewhat, the PHP community quickly assimilates it, benefitting from the experience (good and bad) of the earliest adopters. This has happened in the case of software quality approaches too, including unit-testing, functional-testing, and static code analysis on a very large scale.

You can see the results of this process – very high quality software – in almost every PHP open source project today. Did you know that Drupal and phpBB have thousands of automated tests to ensure the backwards compatibility of their libraries? So do most of the other big open source platforms. Nowadays all the big open-source projects use automated tests to improve the quality of their product. You should learn to do the same for your customer projects.

Tools to Help Improve Software Quality

Now that you know something of how open-source projects are improving the quality of their code, I want to provide an overview on some of the different techniques and tools that are used in PHP today.

Automated Testing

Automated testing allows you to run parts of your application code in a controlled environment – just like performing a chemical or physical experiment under controlled conditions. Your code is executed by a testing tool and you automate the process based on the expected or correct outcomes.

PHPUnit - The long-standing tool of choice for automated PHP testing is PHPUnit, written by Sebastian Bergmann. It allows you to write automated tests for both object-oriented- and functional PHP code.

Behat - Behat is a relatively new tool for automated testing. It works in a very different way to PHPUnit. Behat allows you to write tests in a a human-readable language called Gherkin and parses it into executable test-cases. Behat is a Behavior Driven Development ("BDD") tool, because it focuses on the behavior of systems from a user’s perspective. Writing browsers tests with Behat is very easy. Here's an example from the Gherkin documentation that shows part of a user access test:

  • Scenario: Wilson posts to his own blog
    • Given I am logged in as Wilson
    • When I try to post to "Expensive Therapy"
    • Then I should see "Your article was published."
  • Scenario: Wilson fails to post to somebody else's blog
    • Given I am logged in as Wilson
    • When I try to post to "Greg's anti-tax rants"
    • Then I should see "Hey! That's not your blog!"

Because of their very different approaches to testing, PHPUnit and Behat actually complement each other very well. A growing number of enterprise-level PHP projects are using a combination of both to achieve the best-possible test coverage.

Keep it SOLID

Writing automated tests isn’t easy if you don't develop your software with testability in mind. As a programmer, you should familiarize yourself with the SOLID principles to make object-oriented code more testable. If you prefer functional programming, then you should look into creating lots of pure functions that don't mess with global state or have any other side effects. In general it’s much easier to write automated tests for code that uses no globals and is built out of discrete, "composable" units – be it objects or functions – that can be put together at application runtime.

Static Code Analysis Tools

Static analysis tools don't execute code; they are used to find code that can be optimized in a number of ways. Static analysis tools are good for getting an overview of project quality. They are generally helpful in pointing out the most critical areas in your code in need of improvement. There is a wide range of static analysis tools available that can help:

  • Check for common, consistent coding style (PHP CodeSniffer: "PHP_CodeSniffer is a PHP5 script that tokenises PHP, JavaScript and CSS files to detect violations of a defined coding standard. It is an essential development tool that ensures your code remains clean and consistent. It can also help prevent some common semantic errors made by developers.")
  • Check for programming errors and messy behavior (PHP Mess Detector: "PHPMD takes a given PHP source code base and looks for several potential problems within that source, like possible bugs; suboptimal code; overcomplicated expressions; and unused parameters, methods, and properties.")
  • Check for copy pasted code (PHP Copy-Paste Detector: "phpcpd scans a PHP project for duplicated code.")
  • Compute complexity metrics for code (PHP_Depend: "pdepend can generate a large set of software metrics from a given code base. These values can be used to measure the quality of a software project and they help to identify the parts of an application where a code refactoring should be applied.")
  • For more information and a great collection of tools to improve your PHP code, try the The PHP Quality Assurance Toolchain.

Round Up

PHP has come a long way and is now a proven and reliable enterprise technology. It has a variety of powerful testing tools available to it and a vibrant community that is passionate about software quality best-practices.

Becoming proficient with the selection of quality assurance tools and techniques I have introduced here will help you write better code and applications. These, in turn, will lead to happier clients and website visitors.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Filtered HTML

  • Use [acphone_sales], [acphone_sales_text], [acphone_support], [acphone_international], [acphone_devcloud], [acphone_extra1] and [acphone_extra2] as placeholders for Acquia phone numbers. Add class "acquia-phones-link" to wrapper element to make number a link.
  • To post pieces of code, surround them with <code>...</code> tags. For PHP code, you can use <?php ... ?>, which will also colour it based on syntax.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <h4> <h5> <h2> <img>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.