PHP 7: Maintaining and Upgrading Legacy Apps · Refactoring Legacy PHP Safely

Static Analysis on Legacy Code

Adopt PHPStan with a baseline and raise strictness over time.

PHPStan runs fine on PHP 7.x code (PHPStan itself needs PHP 7.4+). Start at level 0–2, generate a baseline so CI fails only on new errors, then raise levels gradually. Add phpVersion to analyze compatibility with your target version, and use the phpstan-deprecation-rules extension to find deprecated usage before upgrading.

# phpstan.neon
parameters:
  level: 3
  phpVersion: 70400          # analyze as PHP 7.4
  paths: [src, lib, public]
  excludePaths: [vendor, lib/third_party]
includes:
  - phpstan-baseline.neon
  - vendor/phpstan/phpstan-deprecation-rules/rules.neon

Common pitfalls

  • Turning on max level and drowning in thousands of errors.
  • Never shrinking the baseline.
  • Analyzing vendored third-party code.

Key points

  • Baseline existing errors
  • Raise levels gradually
  • Use deprecation rules before upgrades

Further reading: PHPStan: baseline

Practice questions

How does a baseline help adopt static analysis on legacy code?

Answer

It records current errors so CI only fails on newly introduced ones, letting the team improve gradually instead of fixing thousands of issues upfront.

Why set phpVersion in PHPStan config?

Answer

So analysis reflects the PHP version you run or target, catching usage of features or removed functions incompatible with it.

Open in the app to mark lessons complete, take notes and track your progress.