Fixes & guides

PHPStan baseline: adopt static analysis on a legacy project without fixing everything first

Generate a PHPStan baseline, stop new errors from getting in, then raise the level step by step. A tested routine for old PHP codebases.

On this page
  1. 1. Install and choose a starting level
  2. 2. Generate the baseline
  3. 3. Keep the baseline honest
  4. 4. Raise the level gradually
  5. 5. Pair it with an upgrade
  6. Baseline or ignoreErrors?

Running PHPStan on an old codebase for the first time often reports hundreds or thousands of errors. A baseline records today’s errors and ignores them, so the build passes and PHPStan only fails on new problems. You then pay down the old ones over time.

1. Install and choose a starting level

composer require --dev phpstan/phpstan

Create phpstan.neon (or phpstan.dist.neon to commit, letting developers override locally):

parameters:
    level: 5
    paths:
        - src
        - tests
    phpVersion: 80400   # the PHP version you deploy to

Levels go from 0 (basic checks) to 10 (strictest, including treating mixed strictly). Level 5 checks argument types and is a good first target; start at 2 if the codebase is very loose. If you use a framework, add its extension (for example larastan/larastan for Laravel or phpstan/phpstan-symfony) so magic methods are understood.

2. Generate the baseline

vendor/bin/phpstan analyse --generate-baseline

This writes phpstan-baseline.neon. Include it:

includes:
    - phpstan-baseline.neon

Run vendor/bin/phpstan analyse again: it should report no errors. Commit both files and add the command to CI.

3. Keep the baseline honest

  • Unmatched entries fail the build. When someone fixes a baselined error, PHPStan reports “Ignored error pattern … was not matched”. Regenerate the baseline in that pull request, which makes it shrink over time.
  • Never regenerate to hide a new error. In review, a growing baseline file is a red flag.
  • Use --error-format=github in GitHub Actions to get inline annotations.

4. Raise the level gradually

  1. Bump level by one.
  2. Run --generate-baseline again so only the new level’s errors are recorded.
  3. Fix the cheapest category first. vendor/bin/phpstan analyse --error-format=table groups by file; many errors share one root cause, such as a missing return type on a widely used method.

Add types at the boundaries first: return types on repositories and services, and @param array shapes such as array{id: int, email: string} on functions that pass arrays around.

5. Pair it with an upgrade

PHPStan with phpVersion set to your target release catches calls to removed functions and changed signatures before you upgrade. Combine it with Rector for automatic fixes, and use the upgrade checklist for the changes neither tool can prove.

Baseline or ignoreErrors?

Use ignoreErrors with a comment for errors you have decided to accept permanently, such as a known false positive in generated code. Use the baseline for debt you intend to fix. Keeping them separate makes the intent obvious.

Found a mistake or something out of date? Tell us and we will fix it. See our editorial policy.

A good place to start

What are you working on?

Tools, guides, PHP versions and the archive.

ToolJSON ↔ PHP arrayConvert JSON to a PHP array literal with short or array() syntax, or turn a PHP array back into JSON. Runs in your browser; nothing is uploaded.ToolSerialized data inspectorRead PHP serialized strings as a tree, find wrong string lengths after a search-and-replace, and rebuild them with correct byte counts. Parsed safely in your browser.ToolComposer constraint testerSee exactly which versions a Composer constraint such as ^2.1, ~1.4 or >=8.1 <8.4 allows, in plain English, and test versions against it.ToolPHP date() format builderBuild a PHP date() or DateTime::format() string with a live preview in any time zone, plus a reference for every format character.ToolPHP upgrade checklistPick your current and target PHP versions and get every breaking change, deprecation and tooling step in between, as a checklist you can copy.GuideAllowed memory size exhaustedWhat PHP's memory_limit fatal error means, how to tell a real leak from a big job, and fixes for the usual causes: loading whole tables, unbounded arrays, Composer and recursion.GuideComposer dependency conflictsA step-by-step way to read Composer's conflict output, find the package that is blocking you, and fix it without --ignore-platform-reqs or deleting the lock file.Guidecomposer install vs updatecomposer install reproduces composer.lock; composer update rewrites it. Here is what each command does, when to use which, and a safe production deploy routine.GuideComposer uses the wrong PHP versionComposer says "your php version (8.1.x) does not satisfy that requirement" when your server runs 8.3? The cause is usually the CLI binary, config.platform.php, or platform_check.php.GuideEloquent N+1 queriesHow to spot N+1 queries in Laravel, fix them with eager loading, withCount() and constrained loads, and make Laravel throw an exception the next time one slips in.GuideHeaders already sentThe warning names the file and line that produced output. Here is how to read it, the five usual culprits (BOM, whitespace, echo, warnings, closing tags) and fixes that do not just hide it.GuideWriting a talk proposalWhat reviewers look for in a call-for-papers submission: a specific title, an abstract for attendees, notes for the committee, and a talk-sized idea. With a worked example and a checklist.GuideLaravel queue jobs not runningJobs dispatched but never processed, stuck in the jobs table, or running old code? Work through the connection, the queue name, the worker, failed jobs and deploy restarts.PHP versionsWhat's new in PHP 8.4PHP 8.4 (released 21 November 2024) brought property hooks, public private(set) visibility, new without parentheses, lazy objects, an HTML5 DOM parser and array_find(). Plus the deprecations that matter.PHP versionsWhat's new in PHP 8.5PHP 8.5 (released 20 November 2025) added the |> pipe operator, a standards-compliant URI extension, clone() with property overrides, #[\NoDiscard] and array_first()/array_last().PHP versionsWhat's new in PHP 8.6PHP 8.6 was released on 19 November 2026. Partial function application, clamp(), readonly property defaults, Io\Poll and Time\Duration, plus the session and ValueError changes to check before upgrading.GuidePHP array to JSONWhy json_encode() sometimes gives you an object instead of a list, how to keep floats and slashes intact, and how to catch the silent false that malformed UTF-8 returns.GuideDuplicate webhooks in PHPWebhook providers retry, so the same event can arrive twice. Verify the signature, record the event ID with a unique key, answer fast and process in a queue: a pattern that works in plain PHP, Laravel and Symfony.GuidePHPStan baseline for legacy codeGenerate a PHPStan baseline, stop new errors from getting in, then raise the level step by step. A tested routine for old PHP codebases.GuidePSR-4 autoloadingHow PSR-4 maps namespaces to folders, how to configure it in composer.json, and why "Class not found" happens: case, typos, a stale autoloader or a namespace that does not match the path.GuideSerialized data broken after search-replaceChanging a domain name with SQL REPLACE() or sed corrupts PHP serialized strings because their byte lengths no longer match. Here is how to spot it and repair it safely.GuideUpgrade PHP with RectorUpgrade a PHP application one version at a time: pin the baseline, let Rector rewrite syntax, let PHPStan find what Rector cannot, and prove it with tests before you switch runtimes.PHP versionsSupported PHP versionsRelease, active support and end-of-life dates for every PHP version.ArchiveSunshinePHP talk archive 2013–2020Talk titles and speakers from eight years of the Miami PHP conference.ArchiveSpeaker indexEveryone who spoke at SunshinePHP, A to Z.