Modernizing a Legacy PHP Project: A Story From the Field

At the first meeting in February, the client's technical team said one sentence that I wrote down word for word: "If we touch it, it breaks; if we don't, it rots." The subject was a 12-year-old PHP project. PHP 5.6, no tests, no documentation; of the two people who wrote the system, one had retired and the other couldn't be reached. But the system worked, and a serious share of the company's revenue flowed through it. Six months later, that same system runs on PHP 8.2, its critical flows are covered by tests, and it ships weekly deploys. In this post I'll describe how those six months went, for anyone carrying a similar load on their back.

First, know this: you're not alone. In Türkiye there are countless systems still running on old PHP versions that have become the backbone of the business; projects like these come to us several times a year, and the story always starts the same way — the author is gone, there's no documentation, but the revenue flows through it. So the situation in this post isn't an exception; it's a genre.

A confession up front: the hardest part of these projects isn't technical, it's psychological. If a team has lived for years in a "don't you dare touch it" culture, even the first refactoring proposal produces anxiety. That's why our first job wasn't writing code; it was building a safety net that would generate trust.

Our second job, to be precise. In the first week we did nothing but take inventory: which PHP version, which extensions, which dependencies (there wasn't even a composer.json; dependencies were folders copied in by hand), which cron jobs, which servers. It was a boring week, but the payoff was big: any time estimate given without seeing the real boundaries of the system is an optimistic lie.

We took a snapshot before touching anything

The safety net is called characterization tests: end-to-end tests that record the system's current behavior — right or wrong, exactly as it is. The subtlety is this: these tests don't pin down "how the system should work," they pin down "how the system works right now." If there's a behavior that's known to be wrong, it goes on the record too, because who knows which customer is doing business based on that "wrong" behavior. (One was. We later learned that three dealers had built their processes around an odd rounding behavior on the invoicing screen.)

We sat down with the client and identified the 20 most critical flows — login, order creation, invoicing, the monthly report and the like — and automated all of them. It was a two-week investment. Without that net, every refactoring would have been, quite literally, a gamble.

Steps for building a safety net with characterization tests: pick the critical flows, record current behavior, turn it into end-to-end tests, change with confidence.
The tests pin down not how the system should work, but how it works right now.

For some of the tests we used the "golden master" technique: we saved the current output of critical screens and reports to files exactly as it was, and after every change we compared the new output with the old. It's not an elegant method, but it's extremely effective — especially in calculation code with a decade of business rules buried inside, it's the fastest way to know you haven't changed the behavior by a hair.

Static analysis drew the map

Mapping a 12-year-old codebase by eye takes months; we handed the job to tools. We started PHPStan at its lowest level: first we cleared the errors at level 0, then we climbed level by level. Each level makes a different family of the code's diseases visible — dead code, unreachable branches, variables that were never defined, type mismatches. Within a few weeks we had an honest map: what's solid, what's rotten, what's never used at all.

We left the mechanical transformations to Rector. Old syntax, deprecated function calls, the hundreds of small compatibility fixes that version jumps bring — doing these by hand is both slow and error-prone. The combination of automated transformation plus characterization tests took most of the fear out of the version jumps. Human hands touched only the genuinely risky spots, and there we didn't rush.

PHPStan's baseline feature was worth its weight in gold in this process: you dump the existing errors into a file and mark them as "known debt"; from then on, the tool blocks only newly introduced errors. That way, without having to pay off 12 years of debt overnight, you stop the debt from growing on day one. In other words, you don't cancel training because the sandbag is full; you just ban adding any more sand to it.

Static analysis and automated transformation tools in a legacy PHP project: PHPStan levels, Rector and the baseline feature.
The debt stopped growing on day one; 12 years of debt wasn't paid off overnight.

Changing the system without stopping it: the strangler pattern

The big question is always the same: "Should we rewrite it from scratch?" Our answer is almost always no. A big-bang rewrite freezes working business for months, throws away the institutional knowledge embedded in the old system, and when it's done (if it's ever done) you end up with a new system full of new bugs.

Instead, we applied the strangler (strangler fig) pattern: every new request and every serious change was handled in modern modules, and the old code was left behind piece by piece. We consolidated database access into a single layer — from raw mysql_* calls to PDO-based repositories — and step by step turned the forest of global variables into dependency injection. At the end of every sprint the system was live and working. I'm stressing this on purpose, because this is where the secret lies: modernization isn't a project, it's a regimen. Like a diet; the moment you take a weekend off, it slides back.

Comparison of the risks and gains of a from-scratch rewrite versus incremental modernization with the strangler pattern.
Modernization isn't a project, it's a regimen; the system was live at the end of every sprint.

The unsung hero of this regimen was the deploy pipeline. In the third month we retired the manual FTP uploads and built a simple but real pipeline: nothing goes live unless the tests pass, and every release can be rolled back with a single command. The comfort we call "weekly deploys" doesn't come from courage; it comes from this pipeline.

The bill and the payoff of six months

Here's how the numbers look:

  • From PHP 5.6 to 8.2: roughly a 40% improvement in response times; the same traffic now runs on fewer servers.
  • Test coverage on critical flows from 0% to 85%; "deploy fear" dropped off the meeting agenda, and weekly releases became routine.
  • Number of dependencies with known security vulnerabilities from 23 to 0.

But in my view the most valuable outcome doesn't show up in the numbers: the team got its courage to touch the system back. A team that six months ago held a meeting even for a CSS change now comes forward with its own refactoring proposals.

A word for management, too: defend the budget for this kind of work by comparing it with the "let's rewrite it from scratch" option. The hidden costs of a rewrite — months of frozen feature development, the loss of business rules embedded in the code, a period of keeping two systems alive in parallel — are almost always more expensive than incremental modernization. Our six-month bill was less than half of a from-scratch rewrite quote obtained for the same system; and on top of that, the business didn't stop for a single day and not a single customer was lost.

One last word, if you own a system like this: legacy code is not something to be ashamed of. That system has run for years, made money and carried the company to where it is today — this is what software looks like when it has "lived." What's shameful isn't having legacy code; it's abandoning it to its fate. With the right strategy, it can be carried into the future without stopping the business for a single day; we've done it many times. If you'd like to talk about where to start with your system, just send us a message — we'll do the first analysis without embarrassing or judging anyone, promise.