PHPStan 2.4.0 was released on 4 December 2026, exactly ten years after Ondřej Mirtes published the first article about PHPStan. The release notes call it an “explosion of features”: it delivers several long-requested type features and closes 131 issues.

This tutorial walks through the changes you can use in everyday PHP code. We ran each example with PHPStan 2.4.0 at level 9 on PHP 8.4. For three of them we also ran PHPStan 2.3.5 on the same file to show the difference.

Upgrade

composer require --dev phpstan/phpstan:^2.4 -W
vendor/bin/phpstan --version

If you use Larastan, note that Larastan 3.16.0 (released the same day) requires PHPStan 2.4.0 or newer.

1. Recursive type aliases

You can now write a type alias that refers to itself. The classic example is JSON, which is a value that can contain more JSON values:

/**
 * @phpstan-type JsonValue scalar|null|array<JsonValue>|array<string, JsonValue>
 */
final class Json
{
    /** @param JsonValue $value */
    public static function countLeaves(mixed $value): int
    {
        if (!is_array($value)) {
            return 1;
        }
        $total = 0;
        foreach ($value as $child) {
            $total += self::countLeaves($child);
        }
        return $total;
    }
}

Json::countLeaves(['a' => [1, 2], 'b' => null]);              // OK
Json::countLeaves(['when' => new \DateTimeImmutable()]);      // error

PHPStan accepted the first call and reported the second one:

Parameter #1 $value of static method App\Json::countLeaves() expects
array<JsonValue>|bool|float|int|string|null, array<string, DateTimeImmutable> given.

Recursive types are useful for trees, nested config arrays, menu structures and API payloads that used to fall back to mixed.

2. Generic type aliases

Type aliases can now take type parameters. A common use is a result shape:

/**
 * @phpstan-type Result<T> array{ok: true, value: T}|array{ok: false, error: string}
 */
final class Results
{
    /** @return Result<int> */
    public function find(int $id): array
    {
        return $id > 0
            ? ['ok' => true, 'value' => $id]
            : ['ok' => false, 'error' => 'not found'];
    }
}

dumpType() on (new Results())->find(1) gave array{ok: false, error: string}|array{ok: true, value: int}, so T was filled in correctly.

One gotcha we hit: if the class also declares @template T, PHPStan reports typeAlias.shadowTemplate, because the alias parameter shadows the class template. Use a different name, such as TValue, for one of them.

3. Type aliases above functions and methods

Until now, @phpstan-type and @phpstan-import-type only worked on classes. In 2.4 you can put them above a function or method:

/**
 * @phpstan-type Point array{x: int, y: int}
 * @param list<Point> $points
 * @return Point
 */
function first_point(array $points): array
{
    return $points[0] ?? ['x' => 0, 'y' => 0];
}

PHPStan 2.4.0 accepts this with no errors. PHPStan 2.3.5 didn’t understand the alias. It treated Point as a class name and reported class.notFound and two follow-on errors.

4. class-string-map for service containers

The new class-string-map<T of X, T> type is an alias for array<T of class-string<X>, new<T>>. In other words, it’s a map where each key is a class name and the value is an instance of that class. That’s exactly what a service container or registry stores:

interface Service {}
final class Mailer implements Service {}

final class Container
{
    /** @var class-string-map<T of Service, T> */
    private array $services = [];

    /**
     * @template T of Service
     * @param class-string<T> $id
     * @param T $service
     */
    public function set(string $id, Service $service): void
    {
        $this->services[$id] = $service;
    }

    /**
     * @template T of Service
     * @param class-string<T> $id
     * @return T
     */
    public function get(string $id): Service
    {
        return $this->services[$id] ?? throw new \RuntimeException($id);
    }
}

$c = new Container();
$c->set(Mailer::class, new Mailer());
\PHPStan\dumpType($c->get(Mailer::class));   // App\Mailer

On 2.3.5 the same @var tag was a parse error (Unexpected token "of"). The release also supports templates declared by array keys, for example array<K of key-of<Config>, Config[K]>.

5. Readonly properties set in a helper method

This one fixes a real annoyance. Many classes initialise readonly properties in a private method that only the constructor calls:

final class Money
{
    public readonly int $cents;
    public readonly string $currency;

    public function __construct(string $amount)
    {
        $this->parse($amount);
    }

    private function parse(string $amount): void
    {
        [$value, $currency] = explode(' ', $amount) + [1 => 'EUR'];
        $this->cents = (int) round((float) $value * 100);
        $this->currency = $currency;
    }
}

PHPStan 2.3.5 reported property.readOnlyAssignNotInConstructor for both assignments. PHPStan 2.4.0 reported nothing, because it now allows initialising readonly properties in methods that only the constructor calls.

Other notable changes

  • @phpstan-assert above property hooks is supported, and assert tags of overriding methods are checked.
  • Side effects of built-in functions: 352 built-in functions and methods without side effects are now treated as pure, and clamp() is treated as having no side effects.
  • Remembered property types survive calls on $this that can’t write them, which removes some “might have changed” false positives.
  • Bleeding edge gets stricter checks. For example, $a = rand(0, 1) ? $mixed : null; requireInt($a); is now reported even on level 5, where it was previously only reported on level 9 or 10. The --fix option can also fix missing and too-wide return types, with a new projectType (open/closed) config parameter.
  • Performance: the notes list several speed-ups, such as parsing each PHPDoc once, remembering function lookups and writing worker results to a spool file.

Hundreds of bug fixes landed too, many in loop analysis, array shapes and closures. Expect your error count to change after upgrading. Most changes should remove false positives, but some may show new, correct errors.

Upgrade checklist

  1. Upgrade in a branch and run your normal phpstan analyse command.
  2. Look at new errors before you touch the baseline. Many will be real bugs that better inference has found.
  3. Remove baseline entries reported as unmatched, since 2.4 no longer produces those errors.
  4. Then try the new features: replace mixed in recursive structures with a recursive alias, and move repeated array shapes into generic aliases.

Takeaway

PHPStan 2.4.0 is the biggest feature release of the 2.x series so far. Recursive and generic type aliases, function-level @phpstan-type, class-string-map and smarter readonly handling let you describe real PHP code without workarounds. Upgrade with Composer, check the new errors, and read the full PHPStan 2.4.0 release notes for the complete list.