Before we begin modernizing our application, we need to make sure we have the necessary prerequisites in place to do the work of refactoring. These are as following:

Revision control (also known as source control or version control) allows us to keep track of the prerequisites:revision control" changes we make to our codebase. We can make a change, then commit it to source control, make more changes and commit them, and push our changes to other developers on the team. If we discover an error, we can revert to an earlier version of the codebase to a point where the error does not exist and start over.

If you are not using a source control tool like Git, Mercurial, Subversion, or some other revision control system, then that's the very first thing you need to put in place. Using source control will be a great benefit to you, even if you don't modernize your PHP application at all.

I prefer Mercurial in many ways, but I recognize that Git is more widely used, and as such I must recommend Git for new users of source control systems.

While it is beyond the scope of this book to discuss how to set up and use a source control system, there are some good Git books and Mercurial books available for free.

Using a consistent prerequisites:style guide" coding style throughout the codebase is an important consideration. Most legacy codebases that I have seen are a mishmash of styles preferred by the various authors over time. One example of this kind of mishmash is the mixing of tabs and spaces for indenting code blocks: the developers early in the project used 2 spaces for indents, developers in the middle of the project used tabs, and recent developers used 4 spaces. This had the effect of putting some child blocks completely out prerequisites:style guide" of line with their parent blocks, either too much indented or not enough, making it difficult to scan for the beginning or end of a block.

We all long for a consistent, familiar coding style. There are few urges stronger than the urge to reformat an unfamiliar or undesired coding style to one that is more preferable. But modifying the existing style, no matter how ugly or inconsistent it is, can give rise to subtle bugs and behavioral changes from something as simple as adding or removing braces in a conditional. Then again, we want the code to be consistent and familiar so that we can read it with a minimum of cognitive friction.

It is tough to give good advice here. I suggest that the only reason to modify the existing style is when it is inconsistent within an individual file. If it is ugly or unfamiliar but otherwise consistent throughout the codebase, reformatting is likely to cause more problems than it solves.

If you decide to reformat, do so only as you move bits of code from one file to another, or as you move files from one location to another. This combines the large change of extraction-and-relocation with the more subtle change of style modification, and makes it possible to test those changes in a single pass.

Finally, you may want to convert to a completely new style, even though the existing one is consistent throughout the codebase. Resist that urge. If your desire to reformat in toto is overwhelming and cannot be ignored, use a publicly documented non-project-specific coding style instead of trying to create or apply your own personal or project-specific style. The code in this book uses the PSR-1 and PSR-2 style recommendations as a reflection of that advice.