Skip to content

v1.2.0

Choose a tag to compare

@calebdw calebdw released this 25 Aug 01:59
· 9 commits to master since this release
1d58a65

PHPStan Laravel 1.2.0

PostgreSQL schema dumps are finally a first-class source of Eloquent model properties in PHPStan Laravel.

This is a major expansion of schema support. PostgreSQL projects can now analyse the plain-text pg_dump files produced by Laravel's schema:dump command instead of generating replacement migrations, maintaining parallel PHPDoc metadata, or asking a MySQL-focused parser to make sense of PostgreSQL SQL.

First-class PostgreSQL schema support

Install the new optional parser alongside PHPStan Laravel:

composer require --dev calebdw/phpstan-laravel:^1.2 calebdw/pg-schema-parser:^1.0

When it is the only SQL parser installed, the default auto driver selects it automatically. If the project also has a MySQL parser installed, select PostgreSQL explicitly:

parameters:
    laravel:
        sqlParser: postgres

PHPStan Laravel then reads the normal connection-named dumps under database/schema, including files such as pgsql-schema.sql, and uses them to infer model properties without connecting to the database.

The PostgreSQL integration understands much more than basic CREATE TABLE statements:

  • PostgreSQL's canonical integer, floating-point, boolean, character, date/time, UUID, JSON, network, binary, and other built-in type names
  • PostgreSQL enums as precise literal-string unions
  • Domains resolved to their underlying types
  • PostgreSQL arrays as the string literals returned by raw PDO attributes
  • Nullable and NOT NULL columns
  • Tables in public as ordinary Laravel table names
  • Tables outside public with their schema-qualified names preserved
  • Unknown, extension-provided, and application-defined types with a safe string fallback

Model casts continue to apply on top of the inferred database type. For example, an uncast jsonb attribute reflects PDO's raw string value, while an Eloquent array, collection, object, or custom cast produces the corresponding cast type.

Plain-text dumps are supported. PostgreSQL custom and directory-format dumps are not SQL text and cannot be scanned.

PostgreSQL enums retain their values

A database enum is no longer reduced to an unhelpful string:

CREATE TYPE public.account_status AS ENUM (
    'active',
    'suspended',
    'closed'
);

CREATE TABLE public.accounts (
    status public.account_status NOT NULL
);

The resulting model property is inferred as:

$account->status;
// 'active'|'closed'|'suspended'

If the model applies a backed-enum cast, normal Eloquent cast inference takes over and returns the PHP enum type instead.

Schema dumps and later migrations work together

Migration discovery now follows Laravel's own directory traversal: only migration files directly inside each configured directory are scanned. Nested directories such as database/migrations/archive are not replayed implicitly over a current schema dump.

This configuration scans application and modular migrations while leaving nested archives alone:

parameters:
    laravel:
        migrationDirectories:
            - database/migrations
            - app/Domain/*/database/migrations

Projects that intentionally organize active migrations into nested directories can opt in with another wildcard path:

parameters:
    laravel:
        migrationDirectories:
            - database/migrations
            - database/migrations/*

The first path scans direct migrations; the second scans direct migrations in each immediate child directory.

The directory documentation now also makes replacement semantics explicit. A configured migrationDirectories or schemaDirectories list replaces its conventional default. Include database/migrations or database/schema in the list when adding locations rather than replacing them.

More reliable schema parser failures

The phpmyadmin driver now wraps both lexer and parser failures in PHPStan Laravel's SqlParserFailure. Previously, input the lexer could not tokenize, including PostgreSQL array syntax or PostgreSQL 18's \restrict meta-command, could escape as a vendor exception instead of identifying the unreadable schema dump.

SQL-standard CHARACTER and CHARACTER VARYING spellings are also recognized as strings rather than falling through to mixed.

Upgrade notes

  • PostgreSQL projects should install calebdw/pg-schema-parser and use sqlParser: postgres when another SQL parser is also present.
  • Migration directories are no longer recursive by default. Add an explicit wildcard path if nested migration directories contain active migrations.
  • More accurate schema types and nullability may reveal real type mismatches or make old baseline entries and inline ignores unmatched.
  • Projects without squashed schema dumps still need no SQL parser; parser dependencies remain optional and are resolved only when a dump is read.

Documentation

  • Added PostgreSQL installation and configuration guidance.
  • Added PostgreSQL schema support to the comparison with Larastan.
  • Clarified effective migration and schema directory defaults.
  • Added separate examples for replacing and extending default directories.
  • Documented explicit opt-in for nested migration directories.
  • Added an FAQ entry for enum-keyed collections.

Full changelog: v1.1.0...v1.2.0