Refactoring legacy PHP code

Spaghetti Code - John Trainer Flicker
‘Spaghetti Code’ cc John Trainor – flickr

Spaghetti code is frightening, but it is something that we all have hanging out there on the Internet that our 14 year old self would be amazed by.  Spaghetti code is technically known as legacy code.  I struggled with refactoring a larger application I did into a MVC architecture.  It was about half of a million lines of PHP code.  Refactoring it was an uphill battle and was well worth it.  Wanting to call it quits was tempting and now I can share with you pitfalls and strategies to use when evaluating your next project.  I’ll go through the steps and thought process I had when it came to refactoring my PHP application.

Step 1: Accept that your PHP code is legacy and determine your objectives

Your code is legacy.  Period.  The things that you are implementing today will be considered legacy by the time you are done reading this article.  The fact that you are reading a blog post about refactoring your legacy code means that it is legacy.

The hardest thing for me was to accept that my project was coded poorly compared to the new software techniques I learned.  Sure it had some newer features for the customer but the code base was struggling heavily.  The customer was starting to have recurring problems with the application and I had no good way to test it or troubleshoot it.

I really wanted to nuke this thing from orbit and just start over.  I’d fantasize about starting from a clean slate and not having any issues any more.  Phrases like: Test Driven Development, 100% code coverage, modular design, semantic versioning, continuous integration, etc. all rolled through my head.  Then I saw Paul M. Jones give a talk at Midwest PHP 2014.

When you get excited about nuking code from orbit – you need to take a step back and evaluate the technical debt you currently have.

I found out that nuking my code from orbit (or anyone’s code) is not the answer.  It only took me a conference and a talk by @pmjones to convince me otherwise – nevertheless, I chose to refactor the code base in incremental steps to help repay my technical debt.  Acceptance set in.

You can probably identify tons of things wrong with the application at hand.  For me it was the continuous bug I was encountering with keeping track of employee time.  This was the biggest thorn.  The next low-hanging fruit for me was moving things out of the bloated functions.php file and into actual classes that could be auto-loaded.

Step 2: Take out the low-hanging fruit (technical debt)

'Low Hanging Fruit' cc Ian Carrol/flickr
‘Low Hanging Fruit’ cc Ian Carrol – flickr

Having the low hanging fruit gives you a quick win that is needed for repaying your technical debt.  If you go through refactoring your entire PHP code base without accomplishing something for weeks – you will be less likely to do it and stick with the regiment.

Also, the most important thing that Paul mentions while identifying and starting to make incremental changes is to “Keep your application running”.  You cannot afford downtime on this application.  If it means spinning off a module and having a custom router control the application flow then that is what you need to do to take out the low hanging fruit.

Step 3: Develop, test, and reduce risks

Now that you are removing all of the includes and requires from your legacy application you feel a bit better right? Good! Now you can start focusing on developing those features that your customer needed yesterday.  Don’t fall into the mistake of underestimating the time like I did!

I fell into a trap where I wanted to get features done for the customer and get on to the next paying job.  This trap skips testing and analysing future project risks while introducing bugs into the existing software.

Bugs erode trust.

Now that you know the proper way to code you must stick with the regiment.  You must continually improve and have that time allocated for on your projects otherwise you will erode the trust that you need as a developer.

Step 4: Plan the next release

Rinse, rather, repeat.  Since you are done implementing your new code along with some reduction in technical debt – plan your next release.  I listen to what the customer is saying and try to balance that with any goals I may have with the project.  Maybe the next iteration I will finally spend 10% of budgeted time towards writing regression tests since there have been some backwards compatibility issues.

REST route with Zend Framework 1

Rest Route with Zend Framework 1At LPi I was in charge of programming a REST server that would communicate with our internal data source.  I was under the requirement of using Zend Framework 1 as the REST server – which isn’t ideal but it is workable!  Building a rest route with Zend should be easy – but again… this is utilizing Zend Framework 1.  This post details out to utilize a rest route in Zend 1.

The original thinking for the routing was to pass it to Zend\Controller\Router\Regex.  Now I must admit, it seemed elegant at first… the fallacy in using regex becomes clear as the project begins to grow with more and more routes.

The API can adapt quickly and should have an elegant way to use RESTful resources.  Introducing a simple Rest Route modification.  It quickly checks the method before loading the rest of the application in.

Rest Route with Zend Framework 1


Example to use Rest Route

To use the new Rest Route all you need to do is pass your method along with the rest of your route.  It currently only checks the method but could easily be abstracted to do more checks when necessary.

The only “gotcha” is that you will need to register your routes as a plugin and pass the request through the plugin.  This will allow you to avoid using PHP server variables and utilize the framework.

Let me know what you think or if you have any questions.  I’d be happy to share more!

Baby sign language for Henry

Today, Henry is a little over eight months and signed his first word clearly.  “Milk” is now signed clearly!  A child grows at an extraordinary rate within their first year and language is one of those growth areas.

Henry signs milkYouTube of Henry signing

Kym and I started signing with our first, Charlotte.  Charlotte took to signing two months earlier than Henry and still signs at the age of three.  As with most things in life when your child finally grasps a concept you have been showing them.  You have to expose that “A-hah!” moment and take advantage as much as possible.  We are now teaching Henry Mom, Dad, Eat, Diaper and other basic baby signs.

How to teach your baby sign language

We introduced sign language to both of our children as soon as they could pay attention for five seconds.  It takes a lot of patience but it is worth it.  Imagine your baby being upset or crying and within seconds telling you that he wants milk – amazing!  Follow these steps and gradually increase the signs when your child learns more.

  1. Pick basic baby signs (Mom, Dad, Milk)
  2. When Mom is holding baby continue to sign “Mom”.
  3. When baby holds concentration on you during the day sign the word and say aloud.

It is most helpful when the children are toddlers and the speech is difficult for adults to understand.  Remember meeting a toddler and looking at the parent’s saying, “What did she say”? That language barrier is broken with signing and is easily teachable to others.  Most day cares know sign language as well so your child will be able to communicate effectively and naturally with his or her teacher.

We had luck with our children learning sign language and it is helping us communicate more effectively with them.  Let me know in the comments below if you have any questions that I can help with :).

PHP ecommerce integration for The Elegant Farmer

PHP ecommerce integration and The Elegant Farmer’s Apple Pie Baked in a Paper Bag.  A match made in heaven allowed this brick and mortar store to expand its ecommerce operations.  Their crisp apples, crunchy topping, and sugary crust will make your mouth water! I’m not a food editor by any means but I know good food when I taste it and was able to assist them in getting online.

The Elegant Farmer is a Wisconsin based business that primarily sells pies (really good pies).  They had the unique problem of having to ship perishable pies next day air, packed with ice, to customers so wonderful pies would not be ruined by mother nature.  The farmer’s paired up with a fulfilment center and me to connect the two systems and make them “talk” – if you will.

The original ecommerce system was Squirrel Cart.  I wrote a cron script that would query the MySQL database for orders and submit them to the fulfilment center in a specified format.  After the fulfilment center completed their processing they would keep a semi-colon delimited file on their FTP server to be fetched and parsed to retrieve the tracking information for the orders.

The code was written fairly modular.  When The Elegant Farmer switched the ecommerce platform to Open Cart I made a few modifications to the script to make it work.


The Elegant Farmer was able to lower their pricing by half and made their tens of orders rise to hundreds of orders.  The Elegant Farmer’s success allowed them to invest more time into increasing their products listed online.


With any PHP ecommerce integration there are some challenges.  The challenge in this instance came down to getting the proper file formatting to the fulfilment center.  I encountered other challenges when I was trying to migrate to a different ecommerce solution but they were mainly data challenges.

I learned a lot about this project while doing it and will be contributing a PHP ecommerce plugin for several different commerce systems in the near future.  This plugin is aimed at connecting stores to other fulfilment centers.