Coding - PHP

Refactoring PHP Code

This tutorial is meant to introduce PHP programmers to the technique known as code refactoring, and explain why it should be part of any programmer’s development plan. If you are brand new to PHP and need a more basic tutorial first, check out our Introduction to PHP lesson, then come back to this one to build on that foundation.

What is Refactoring?

Code refactoring is defined by software expert and author Martin Fowler as “a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior.” In simple terms, it’s the process of improving code in small, easily debugged increments without changing its end-user functionality. While we usually think of code maintenance and updates mostly when we need the code to do something new, refactoring reminds us that almost any code can be made better, anytime.

Refactoring is to coding as editing is to writing. Hemingway once said, in slightly more extreme language, that the first draft of everything is awful. Just as a writer needs to always be editing and rewriting to get their prose and style just right, a programmer should see better, more readable, more efficient code as a constant process.

The Values of Refactoring

Coders, and engineers in general, are often very practical in the way that they see code. If it works well, and there’s been no complaint from the client, why change it? This is fair, and often I fall into seeing my code this way too, so it’s important to realize the general values that we shoot for when refactoring code in any language. The goal of refactoring should be to improve one or more of these:

  • Efficiency (both in terms of processing time and length of code)
  • Readability (how easily could another programmer read and understand your code?)
  • Extensibility (is your code logical and easily built upon?)

Common Refactoring Methods

There are too many different ways to refactor code than we can go over in one article. But they have been grouped into some useful categories. Remember as you read through these: the point of refactoring is to make small changes that accumulate to being significant improvements in code. Some of these might seem minor or insignificant, but they add up.

1. Renaming Classes, Methods, Variables, etc.
This is where the most strictly practical programmers really turn up their nose. Why should it matter what a variable or class is called, as long as it makes sense to the programmer who uses them? There are a few reasons. First, you may not always be the programmer working with this code. As a freelance coder myself, I know that there is every likelihood someone else will be doing the next update on the software that I write. Also, even if you do continue to work with this code, you may not look at it for months or years. Will the obscure (or nonexistent) naming convention you use today make sense, even to you, at some point in the future? This is pretty much purely an issue of readability, but that doesn’t mean that consistent, sensible naming isn’t important.

2. Extract and Combine Classes and Functions
As an application gets more complex, functions and classes often begin to overlap and step on each other’s toes. Programmers often find themselves reusing the same code more than once, but in the heat of the moment, when functionality (or simply uptime) is the driving factor, it’s easy to skip the step of combining those chunks of code that should be combined and separating those that should be separate. This is a matter not only of readability but also of efficiency and extensibility, since unnecessarily repetitive code hurts all of these code values.

3. Move Code
Functions and classes often end up placed in code not where they belong, in a sensible and logical order, but chronologically, in order of when they were added. After all, it’s much easier to glob new code on to the end of the old than to integrate it the right way. Like the last method, this one affects all three values, and putting code snippets into meaningful order can go a long way toward cleaning up any code, especially in an application that has been modified since its first version.

4. Scope Changes
There is a set of concepts in refactoring known as Pull Up and Push Down. All this really means is that sometimes, code can more efficiently be placed at either a more general or more specific scope. Maybe a function is inside a class now but should really be on its own so that other classes can use it. On the other hand, maybe a class is only called one time and it’s hard to see future functions needing it either, so its scope can be brought down to that one use rather than being available generally. This one is all about efficiency and extensibility.

Our Starting Code

Now that we’ve slogged through all the theory, let’s get our hands on some real code and look at some ways that we can improve it through refactoring principles. Here’s a pretty simple PHP application:


echo 'CODE EXAMPLE: REFACTORING.<br /><br />';

function wordCount($input) {
  $count = str_word_count($input);
  echo 'The text includes '.$count.' words.<br />';

function charCount($input) {
  $charcount = strlen($input);
  echo 'The text includes '.$charcount.' characters.<br />';

function mostCommonWords($input) {
  $words = str_word_count($input, 1); 
  $frequency = array_count_values($words);
  $frequency = array_keys($frequency);
  $mostcommon = $frequency[0];
  echo 'The most common word in the text is '.$mostcommon.'.<br />';

$done = $_POST['formsubmit'];
$fulltext = $_POST['fulltext'];

if ($done) {
} else {
  // FORM
  echo '<FORM method="post"><TEXTAREA name="fulltext"></TEXTAREA>
	<INPUT type="hidden" name="formsubmit" value="1" />
	<INPUT type="submit" value="Analyze!" /></FORM>';

This script displays an HTML form with a single text area, then after the form is submitted, tells the user how many words and characters are in the text as well as the most common word in it. The script works as it is, which is why we are talking about refactoring rather than repair. But there are at least three changes that can be made to improve the readability, efficiency, and extensibility of the code. Before I get into the solutions, look the code over and think about what changes you might make, then we came up with similar answers.

A Few Changes

There are two variables that I would rename in this script: $done and $count. Both are vague and could easily come up again if the script became a longer, more complicated application. Let’s change those to $textformsubmit and $wordcount, respectively.

The first two of our three functions (wordCount() and charCount()) really don’t have to be functions at all. Their content is basically a pre-existing function plus an echo() to display results. Even if more was added to this script, these functions wouldn’t be likely to be useful again, because the echo() is specific to this one use. I would take these out of functions and place their code where they are used, in the form processing area of the script.

There’s also a little cleanup item. At the very beginning of the script, there’s an echo() that creates a bold header for our script. Since that is simple text, with no variables, and since it occurs right next to an opening or closing PHP tag, we can move it outside of PHP and into regular HTML, giving PHP just a tiny bit less work to do.

Here’s how the script should look after our changes:



 function mostCommonWords($input) {
  $words = str_word_count($input, 1); 
  $frequency = array_count_values($words);
  $frequency = array_keys($frequency);
  $mostcommon = $frequency[0];
  echo 'The most common word in the text is '.$mostcommon.'.<br />';

 $textformsubmit = $_POST['formsubmit'];
 $fulltext = $_POST['fulltext'];

 if ($textformsubmit) {
   $wordcount = str_word_count($fulltext);
   $charcount = strlen($fulltext);
   echo 'The text includes '.$wordcount.' words.
The text includes '.$charcount.' characters.
'; mostCommonWords($fulltext); } else { // FORM echo '<FORM method="post"><TEXTAREA name="fulltext"></TEXTAREA> <INPUT type="hidden" name="formsubmit" value="1" /> <INPUT type="submit" value="Analyze!" /></FORM>'; } ?>

Not only is this code a bit shorter, but it also makes more sense, with no unnecessary functions or vaguely named variables. Are there more changes that could be made to this code? Probably. Have a look and see if you can make this script even more readable, efficient, and extensible. Then get in the habit of running every piece of PHP code you write through this same process, and revisiting old code every once in a while for a regularly scheduled refactoring. Your code will get a little better each time. As always, happy programming.

About the author

Ian Rose is a web developer, blogger, and writer living in Portland, OR. In his day job, he develops WordPress plugins and custom PHP solutions, focusing on nonprofit clients. By night, he attempts to write both fiction and nonfiction. Ian's site is Seaworthy Web Solutions

Share this post

Leave a Comment

Subscribe To Our Newsletter