Refactoring as Part of Porting PHP4 to PHP7

Table Gateway

This pattern is simple, but works well for old PHP code, because you pass database rows around as arrays rather than objects.

I think retaining the old SQL is a must, because using an ORM would require writing queries in the ORM’s style. That’s new code, and new code has more bugs.

Using the TG, I had to rewrite some code to pass around arrays instead of using the Article object.

Taking Down the Logic Pyramids

I didn’t have too many logic pyramids, but there were some, and there was also the logic spaghetti where you have the same if-statement structure re-used throughout a function.

Rather than maintain multiple code paths through a single function, I broke out the different cases into different functions.

Totally Revising the Views

There were a bunch of “render” functions that would emit all the different versions of the article: html, html + php, and json.

The code was broken out into subroutines, but there wasn’t any overarching architecture to keep the views separate. So a new structure was created, and centered around producing an associative-array version of the article, including its comments and attachments.

I focused on producing that data structure once. Then, in the different views, instead of touching the database, I can just use that big array.

It’s still a work in progress, but it is coming along, just like the rest of this.

Leave a comment

Your email address will not be published. Required fields are marked *