#2512·larastan

Make parseModelCastsMethod default to true — or warn when a model defines casts() while it is off

Author: JensR93Created Jul 13, 2026Updated Jul 13, 2026

Summary

Laravel 11+ generates models with the casts() method by default, but Larastan silently ignores it unless parseModelCastsMethod: true is set. The failure mode is not just "wrong types" — it can flag whole code regions as dead. I'd like to propose making the flag default to true, or emitting a warning when a model defines casts() while the flag is off.

Mechanism

Laravel merges the casts() method into the cast map in HasAttributes::initializeHasAttributes() — a trait initializer that only runs in the constructor. Larastan builds model instances via newInstanceWithoutConstructor() (ModelCastHelper), so getCasts() only ever sees the $casts property (empty in Laravel 11-style codebases). The AST fallback for the method only runs behind parseModelCastsMethod.

Why this is silent and dangerous

The breakage only shows where the schema-derived column type and the cast diverge (json/array/enum) — for int/datetime columns the schema type already matches, which makes the gap look partial (see #2509, where "other casts work fine" is exactly this illusion).

For json columns the attribute falls back to string|null. Reads like $model->meta['key'] ?? null then resolve to always-null, strict comparisons become always-false, and guard blocks "always terminate" — entire code paths get reported unreachable and skipped from analysis, hiding real errors behind them. With a baseline in place, those false positives freeze invisibly.

Real-world datapoint: in our ~80-model codebase (all models use the casts() method), enabling the flag revived 15 wrongly-dead code regions (baselined Unreachable statement + method.unused families) and net-resolved ~300 baseline errors.

Proposal (any of these would help)

  1. Default parseModelCastsMethod to true — the AST parse only triggers for models that actually define the method, so the cost is scoped.
  2. Or: emit a tip/warning when a model defines casts() while the flag is off (cheap to detect in ModelCastHelper).
  3. Or at minimum: document the failure mode (dead-code false positives, not just missing cast types) prominently next to the parameter.

Thanks for Larastan — and for the flag already existing; this is only about discoverability of a silent default.