DEV Community

Andrei Dascalu
Andrei Dascalu

Posted on

3

Refactoring Legacy - Intro

This post is a summary of a couple of years spent battling a legacy application. This is not (necessarily) a how-to because I have yet to declare a "mission accomplished" here and whether it's a success or not, it's too soon to call.

While this represents a particular set of circumstances, I believe it can help you ask the right questions in the beginning, take into account a variety of paths and also give the benefit of the doubt to those whose mess you've inherited (they were doing their best at the time).

The application:

  • legacy PHP, started with early 5.x versions and patched just enough so that it runs on 7.2
  • really old-school: html is formed by echo-ing stuff in random scripts that are "require'd" in the right order. Warnings are totally silenced, nobody cared about a missing array item when fetching a deeper array entry.
  • evidence that someone tried to MVC the whole thing, but not by introducing, say, Symfony components but by creating scripts that act like views (just echo stuff, but without buffering it into a final string - echo wherever/whenever) and some classes - but missing a proper service layer
  • database access done via a class that mysqli_connects, no caching of any kind
  • some models carry state but still choke-full of static methods that bring fresh data, as if someone wanted to go more like Doctrine-style but someone else later wanted a poor man's Eloquent Active Record
  • sometimes the same query ended up being done against the DB 10s of times during a request (well, 29 was the top I've seen myself)
  • running on manually maintained VMs, deploy manually via "git pull" then clear some local static files then restart Apache
  • the whole thing was becoming multi-tenant
  • the application was heavily dependent on data fetched from remote APIs and had to push data back to the same APIs (some where REST, some SOAP)
  • non-existent error handling, WARNING were silent, catching was used to ignore Errors (for the most part), virtually no logging.
  • objects were used for data models only for the DB, for SOAP/REST(JSON) only arryas/stdClass were used to serialise/deserialise (always assuming that some item exists via the developer's knowledge of the structure of a given request).

The Challenges:

  1. migrate to a cloud provider and add scalability (automatic, preferably)
  2. create a layered system of environments for each tenant with automatic deployment on a QA environment and manual promotion for local (our) acceptance then customer acceptance then prod.
  3. tackle a number of bugs in the code. Right off the bat it was clear most were linked to the hidden warnings, where the code would for example expect $array['level1']['level2'] but 'level2' may be missing due to some buggy change yet that fact was hidden from developers since warnings were silenced.
  4. create a system of aggregated structure logging
  5. integrate more external APIs
  6. tackle an existing feature backlog
  7. improve performance

Let's explore them, as part of the series!

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay