Skip to content
Legacy Modernization Articles - Aqib Javaid•10 min read•Published

Legacy PHP Code Audit: What to Check Before You Spend More

A practical five-pass legacy PHP code audit: runtime, configuration, database, business workflows and frontend duplication, plus how to turn the findings into a go or no-go decision.

Aqib Javaid
Aqib Javaid
Senior Full-Stack Engineer
Glass audit checklist and magnifying glass over PHP and MySQL blocks

A legacy PHP code audit is the cheapest decision you can buy on a troubled project. Before you approve another sprint, another contractor or a rewrite quote, you need to know what the codebase actually does, where it is fragile, and which problems are worth money.

I was brought in on exactly this kind of project: a freight-forwarding platform built in Core PHP and MySQL that was roughly 95% complete. It had a public site, a customer portal and an admin portal. Nobody wanted to add features on top of it until someone could say what was inside. This article is the audit I ran there, rewritten as a repeatable process you can use whether you hire me for legacy application modernization or someone else. For the NDA-safe details, see the freight-forwarding audit case study.

Why audit first instead of quoting a rewrite#

A rewrite quote from someone who has not read the code is a guess with a price on it. A feature quote on a codebase nobody understands is the same guess, spread over more sprints. In both cases the risk lands on you.

On the freight platform, the client also wanted to move to Laravel eventually. The tempting move was to start the migration straight away. I did the opposite: I treated the migration as a hypothesis and the audit as the test. If you are weighing that choice yourself, my rewrite vs refactor framework covers the decision, and the legacy PHP modernization cost guide covers what the work usually costs once you know the scope.

An audit does three things for a non-technical owner:

  • It turns "the code is a mess" into a ranked list of specific problems.
  • It gives every later estimate a shared baseline, so quotes become comparable.
  • It shows which parts are fine. In my experience that is usually more than the owner fears.

What I check, in the order I check it#

I run the audit as five passes. Each pass produces a short written artefact, not a vague impression. The order matters: the first two passes are cheap and tell me how deep the rest needs to go.

PassQuestion it answersOutputTypical time
1. Runtime and dependenciesCan this even be patched safely?Version and vulnerability listHalf a day
2. Configuration and environmentsDoes it behave the same everywhere?Config ownership mapHalf a day
3. Database schemaIs the data trustworthy?Schema findings, ranked1 day
4. Business workflowsWhere does the money logic live?Workflow map with fragmentation notes1 to 2 days
5. Frontend and structureHow expensive is each change?Duplication reportHalf a day

The timings are what I plan for a mid-sized application, not a promise. A platform with three portals and a few hundred files takes longer than a single admin tool.

Pass 1: runtime and dependencies#

Start by finding out which PHP version production really runs, and compare it with the supported PHP versions list. An unsupported runtime is not automatically a crisis, but it changes the order of work: you cannot patch what you cannot upgrade.

If the project has a composer.json, ask Composer what it knows:

bash
php -v
composer validate --no-check-publish
composer outdated --direct
composer audit

composer audit checks installed packages against known security advisories, as described in the Composer CLI documentation. Many Core PHP projects have no composer.json at all, which is itself a finding: it means libraries were copied in by hand and nobody can tell you their versions. In that case I list every require/include of third-party code and identify the library by its header comments.

Then I run static analysis at a low level just to get a baseline, not to fix anything:

bash
vendor/bin/phpstan analyse src --level=1 --memory-limit=1G

PHPStan's rule levels go from 0 to 10. Level 1 catches undefined variables and unknown methods. The number of errors is a rough signal of how much dead or broken code exists, and I record it so we can show progress later.

Pass 2: configuration and environment governance#

This was one of the more important findings on the freight platform. When configuration is spread across scattered files, behaviour differs between a developer's machine, staging and production, and nobody can predict which value wins. The twelve-factor guidance on config is a good yardstick: configuration that varies by environment should live in one place, outside the code.

Advertisement

My checks here are blunt:

bash
grep -rEn "(password|secret|api_key|DB_HOST)\s*=" --include=*.php . | grep -v vendor
grep -rEn "mysqli_connect|new PDO" --include=*.php . | grep -v vendor

If database connections are opened in forty files, every credential change is a forty-file change. If secrets are committed, rotating them is step one of the project, before any code is touched. On the freight platform, the useful outcome was a dependency map and a plan for shared bootstrap and centralised configuration. That work pays off even if the app never moves to Laravel.

Pass 3: the database tells the truth#

Code lies; data does not. I look at the schema for the things that cause silent corruption:

sql
-- Tables with no primary key
SELECT t.table_name
FROM information_schema.tables t
LEFT JOIN information_schema.table_constraints c
  ON c.table_schema = t.table_schema
 AND c.table_name = t.table_name
 AND c.constraint_type = 'PRIMARY KEY'
WHERE t.table_schema = DATABASE()
  AND t.table_type = 'BASE TABLE'
  AND c.constraint_name IS NULL;

-- Foreign keys that exist at all
SELECT COUNT(*) FROM information_schema.referential_constraints
WHERE constraint_schema = DATABASE();

A logistics system that tracks pre-alerts, packages and customers but declares no foreign keys is relying on application code to keep rows consistent. Then I check orphaned records, duplicated customer/package data between portals, and columns that store several values in one field. I also run EXPLAIN on the five slowest-feeling pages; the MySQL EXPLAIN reference explains what a full table scan looks like. Schema findings go into the report ranked by risk to data, not by how ugly they look.

Pass 4: where the business rules actually live#

This pass takes the longest and gives the most value. I trace each real workflow end to end, for example: a customer buys something overseas, files a pre-alert, the warehouse receives the parcel, customs is handled, and local delivery happens. For each step I note which files own the logic.

What I was looking for on the freight platform:

  • Fragmented logic: the same rule implemented differently in the customer portal and the admin portal.
  • Hardcoded business values: rates, statuses and thresholds embedded in code instead of configuration or the database.
  • Synchronisation gaps: customer and package data kept in step by convention, not by design.
  • Edge cases: what happens on a partial receipt, a changed address, or a duplicate pre-alert.

The output is a table of workflows with a column for "single source of truth: yes or no". Every "no" is a future bug and a future estimate line.

Pass 5: frontend and structural duplication#

Last, I measure how expensive change is. On the freight platform I found shared headers, sidebars and navigation duplicated across portals, near-identical forms in the customer and admin areas, and inconsistent CSS and JavaScript. None of that breaks production today. It does mean a single label change touches many files, which inflates every future estimate.

A quick way to quantify it: count how many files contain the same block.

bash
grep -rl "<nav class=\"sidebar\"" --include=*.php . | wc -l

If one sidebar exists in twenty files, extracting a shared layout is a small, safe, high-return first task.

How I turn findings into a decision#

An audit that ends with a 40-page list changes nothing. I sort every finding into one of three buckets:

  1. Fix now: security holes, data-loss risks, unsupported runtime blocking patches.
  2. Fix while you build: duplication and fragmented logic, tackled when a feature touches that area.
  3. Leave alone: ugly but stable code with no business impact.

Then I answer the migration question with evidence. On the freight platform I estimated moving to Laravel at roughly 40 to 60 working hours, covering application separation, configuration setup, environment handling, migration and validation of existing workflows. That was an estimate, and the migration was not executed in that engagement. The recovery work continued on the existing stack, which was the right call because the audit showed the app was close to finished and the cheapest risk reduction was documenting dependencies and centralising configuration.

This is the trade-off nobody wants to hear: sometimes the audit says do not migrate yet. If the app is nearly complete and revenue-generating, the lowest-risk path is stabilise, document, then migrate in slices. If the data layer is the real problem, a framework change will not fix it. I wrote about that in my account of migrating a Core PHP platform to Laravel without losing client data, where the hard part was the data, not the framework.

⚠️Warning

Be wary of any audit that recommends a full rewrite without showing the workflow map and the schema findings behind that recommendation. The evidence should be in the document.

When an audit is not worth paying for#

Be honest about the limits:

  • A tiny app: if the whole thing is a few hundred lines, reading it is the audit.
  • A rewrite is already decided for non-technical reasons: for example, an acquisition forces a platform change. You still want a data and workflow inventory, but not a full audit.
  • No access to production: an audit without database access and the real deployment is a partial one. Say so in the scope.

Frequently asked questions#

How long does a legacy PHP code audit take?#

For a mid-sized application with a few portals, I plan roughly five working days of reading, running tools and writing the report. A small app can take one or two days; a large multi-service system can take two weeks.

What should I receive at the end?#

A written report with a ranked findings list, a workflow map, a configuration and dependency map, and a recommendation with an estimate range. You should be able to hand it to any other developer and get a comparable quote.

Do I need to give the auditor production access?#

Read-only database access and a copy of the deployed code is enough for most of the audit. I never need write access, and credentials should be rotated afterwards if they were shared.

Can I audit the code with automated tools alone?#

Tools such as PHPStan and composer audit cover pass 1 well. They cannot tell you that two portals implement the same pricing rule differently. That needs someone to read the workflows.

Key takeaways#

  • Audit before you fund a rewrite, a migration or more features. It is the cheapest risk reduction available.
  • Check in order: runtime and dependencies, configuration, database, business workflows, frontend duplication.
  • Sort findings into fix now, fix while you build, and leave alone, so the report leads to a decision.
  • Treat "do not migrate yet" as a valid result when the app is close to done.

If you have an inherited PHP codebase and need a clear answer before spending more, I do this through my legacy application modernization service, and you can book a call to scope an audit.

Share this technical insight with your network

Share to LinkedIn or Facebook with key takeaways, featured media, and direct links.

Aqib Javaid

About the author

Aqib Javaid

Senior Full-Stack Engineer

8+ years building Laravel, React/Next.js and AI-powered products for startups and growing businesses - SaaS platforms, APIs, integrations and the performance work that keeps them fast. Read the case studies to see how that plays out on real projects.

4.9 from 76 client reviews

📁 Production Case Study

Case Study: PPCWA: Jamaica Freight Forwarding Audit

I was brought in to audit and stabilize a nearly completed freight-forwarding platform built with custom Core PHP and MySQL. The platform manages the journey from international online purchases and overseas warehouse receiving through pre-alerts, package tracking, customs handling, and local delivery. My work focused on understanding the existing system before further development: auditing operational workflows, reviewing the database structure, assessing frontend maintainability, mapping dependencies, and evaluating configuration and environment governance. I also assessed the architectural path toward a future Laravel migration.

Related Technical Articles

View all articles →
✦ Let's Build Together

Have a complex technical project in mind?

Available for full-stack engineering, performance audits, cloud deployments, and high-concurrency systems architecture.