How can I perform static code analysis in PHP?

How can I perform static code analysis in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Beyond Syntax: Performing Static Code Analysis in PHP

While the PHP engine itself serves as a rudimentary static analyzer by refusing to parse files that fail syntax checks, this capability is insufficient for maintaining code quality at scale. When you move beyond simple execution validation, you need tools that can inspect control flow, detect unreachable code, and flag logical errors before they reach production. The specific concerns you raised—unused variable assignments, uninitialized arrays, and code style violations—are the standard metrics for modern static analysis in PHP.

The primary tool for this task is PHPStan. Unlike linters that merely parse tokens, PHPStan utilizes data flow analysis to understand how variables change state throughout a function or class. To address your need for detecting unused variable assignments, PHPStan operates on a principle of strictness: if you assign a value to a variable but never read it before the scope ends, PHPStan will flag it as an error by default. This prevents silent bugs where data is calculated but discarded, wasting CPU cycles and potentially indicating logic errors in your control flow.

You can configure this behavior directly in your phpstan.neon configuration file. By setting the rule level to a higher standard, such as level 7 or 8, you enable checks for unused variables out of the box. However, the tool also requires you to explicitly handle cases where variables are intentionally unused but must be ignored to avoid noise. You do this by adding the @phpstan-ignore-next-line annotation or using the $unusedVariable rule suppression in your configuration.

Regarding uninitialized arrays, PHPStan can detect scenarios where an array is assigned into without prior initialization if the control flow analysis determines that path is reachable and no assignment occurred. Consider this pattern:

function processUser($id) {
    // Logic that might conditionally assign $data
    if ($found) {
        $data = [];
    }

    // If $found is false, $data is never initialized here
    foreach ($data as $key => $value) { 
        echo "Processing: {$value}";
    }
}

PHPStan will flag the foreach loop because it cannot guarantee $data exists. The tool resolves this by requiring explicit initialization or ensuring all code paths handle the variable correctly. This deep inspection goes far beyond the binary's syntax checking, which would only reject this if you attempted to run it immediately upon encountering a fatal parse error (which this specific example wouldn't trigger without execution).

For code style warnings, PHPStan integrates with rulesets that enforce conventions like PSR-12. You can configure these via the phpstan.neon file by including the standard rule sets. However, a critical trade-off exists here: strict static analysis often conflicts with legacy codebases or specific architectural patterns. If you enable every possible check immediately, the tool may become unusable due to excessive false positives. The recommended approach is to start with the base configuration and incrementally raise the severity level as your team adapts to the stricter standards. This incremental adoption ensures that static analysis remains a sustainable part of your development workflow rather than a source of frustration.

If you are building applications using the Laravel framework, consider leveraging the ecosystem built around it. Laravel offers its own suite of tools, including phpstan integration often found in the vendor scripts and dedicated packages like friendsofphp/php-cs-fixer for style enforcement. For more enterprise-grade support where these static analysis pipelines are critical to your deployment workflow, you might explore resources from https://laravelcompany.com to see how larger teams structure their quality assurance processes around these tools.

Ultimately, the choice between running PHPStan in a standalone CI pipeline or integrating it directly into your editor depends on your team's velocity and tolerance for friction. The technical reality remains consistent: static analysis is not about finding every possible bug, but about reducing the cognitive load on developers by highlighting issues that require manual inspection.

Our network