Skip to content

Type a custom caster's get() by its native return type when it has no docblock - #2294

Merged
alies-dev merged 12 commits into
4.xfrom
1662-caster-native-return-type
Nov 16, 2026
Merged

alies-dev merged 12 commits into
4.xfrom
1662-caster-native-return-type

Conversation

@alies-dev

@alies-dev alies-dev commented Nov 13, 2026 •

Copy link
Copy Markdown
Collaborator

A model attribute cast to a custom caster whose get() has only a native return type (get(...): Money, no @return docblock) is now typed as Money, with or without @implements CastsAttributes<Money, Money>.

Before

// app/Http/Controllers/PaymentController.php
public function show(Payment $payment): string
{
    return $payment->amount->format();
}

// app/Billing/Payment.php
protected $casts = ['amount' => MoneyCast::class];

// app/Casts/MoneyCast.php
public function get(Model $model, string $key, mixed $value, array $attributes): Money
ERROR: MixedReturnStatement - app/Http/Controllers/PaymentController.php:13:16 - Could not infer a return type (see https://psalm.dev/138)
        return $payment->amount->format();

ERROR: PossiblyNullReference - app/Http/Controllers/PaymentController.php:13:34 - Cannot call method format on possibly null value (see https://psalm.dev/083)
        return $payment->amount->format();

ERROR: MixedMethodCall - app/Http/Controllers/PaymentController.php:13:34 - Cannot determine the type of $payment->amount when calling method format (see https://psalm.dev/015)
        return $payment->amount->format();

3 errors found

After

No errors found!

Issue to Solve

  • A property cast through $casts or casts() to a custom caster whose get() declares only a native return type resolved to (TGet:CastsAttributes as mixed)|null instead of Money.
  • Without a docblock of its own, Psalm answers Codebase::getMethodReturnType() for such a get() with the interface's documenting TGet|null.
  • A @return Money docblock on get() was the only workaround.

Related

Fixes #1662

Solution Description

  • CastResolver::resolveCastsAttributesGet() uses the caster's declared signature return type when the only docblock documenting get() is the interface's TGet|null.
  • The signature is expanded in the caster's context, so self, static and parent (including those from a trait) name a caster class, not the model: self/parent bind to the class the method appears in (the one that imported a trait), static to the configured caster.
  • A Castable whose castUsing() returns several casters (@return IntCast|StringCast) now reads as the union of what each returns, and is left to Psalm when any member cannot be resolved (it used to take the first resolvable member).
  • A caster implementing both CastsAttributes and CastsInboundAttributes reads as the raw column value (Laravel never calls its get()), directly and through a Castable.
  • A Castable whose castUsing() cannot be resolved reads as mixed; it no longer falls back to the class's own get() (Laravel uses castUsing() alone, even for a value object implementing both contracts).
  • A castUsing() return that is an intersection (BaseCast&CastsInboundAttributes, also inside class-string<...>) reads as mixed, since the inbound contract may be one of its parts.
  • Known open-world assumption (same as 4.x for a documented @return): a non-final caster returned by castUsing() is trusted as declared, even though a runtime subclass may also implement CastsInboundAttributes.
  • Casters with their own @return docblock, or one inherited from a base caster (@return Money over get(): object), are unchanged.
  • Added a fixture project with migrations (the Type harness has no schema) and one subprocess test (CasterReturnTypesEmissionTest) asserting that a native-only get(): Money caster reads as Money.

Checklist

  • Subprocess test with a native-only get(): Money caster

@alies-dev alies-dev self-assigned this Nov 13, 2026
@alies-dev
alies-dev requested a lite review from Copilot November 13, 2026 00:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@alies-dev

Copy link
Copy Markdown
Collaborator Author

/psalm-delta

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ Types: decreased · Performance: unchanged

Apps: all → 23

App Wall (s) Memory (MB) Issues Weighted (types) Taint Coverage (%)
✅ monica 25.3 $\color{gray}{\textsf{-2.2\%}}$ 1 695 $\color{gray}{\textsf{+0.1\%}}$ 5 840 12 725 6 97.06
✅ pixelfed 26.5 $\color{gray}{\textsf{-2.9\%}}$ 1 431 20 193 36 866 59 86.02
✅ coolify 37.1 $\color{gray}{\textsf{+2.0\%}}$ 1 542 $\color{gray}{\textsf{-0.1\%}}$ 19 748 33 975 15 88.88
✅ bookstack 14.6 $\color{gray}{\textsf{+1.1\%}}$ 1 203 $\color{gray}{\textsf{-0.1\%}}$ 3 285 6 144 22 96.67
✅ solidtime 17.9 $\color{gray}{\textsf{+0.3\%}}$ 1 317 $\color{gray}{\textsf{-0.1\%}}$ 1 332 2 402 0 97.96
✅ laravel-socialite 11.2 $\color{gray}{\textsf{-0.1\%}}$ 661 365 859 0 89.54
✅ laravel-permission 14.9 $\color{gray}{\textsf{-0.2\%}}$ 812 426 724 0 88.68
✅ laravel-excel 17.0 $\color{gray}{\textsf{-1.6\%}}$ 864 $\color{gray}{\textsf{-0.1\%}}$ 904 2 150 0 94.29
✅ bagisto 41.3 $\color{gray}{\textsf{+0.3\%}}$ 1 977 $\color{gray}{\textsf{+0.2\%}}$ 17 283 32 185 1 83.67
✅ filament 41.6 $\color{gray}{\textsf{-2.1\%}}$ 1 682 $\color{gray}{\textsf{-0.1\%}}$ 12 026 28 161 878 95.22
✅ unit3d 26.7 $\color{gray}{\textsf{+1.0\%}}$ 1 252 5 225 10 965 5 96.48
✅ vito 15.0 $\color{gray}{\textsf{-1.2\%}}$ 1 053 $\color{gray}{\textsf{-0.4\%}}$ 2 528 3 616 55 96.41
✅ laravel-backup 8.1 $\color{gray}{\textsf{-6.6\%}}$ 814 407 811 0 95.53
✅ corcel 13.5 $\color{gray}{\textsf{+0.4\%}}$ 747 $\color{gray}{\textsf{-0.1\%}}$ 403 1 024 0 87.67
✅ spatie-dashboard 15.1 $\color{gray}{\textsf{-0.5\%}}$ 822 190 333 0 89.42
✅ tastyigniter 13.1 $\color{gray}{\textsf{+3.6\%}}$ 932 38 71 0 100.00
✅ unopim 43.9 $\color{gray}{\textsf{+1.8\%}}$ 2 207 $\color{gray}{\textsf{+0.6\%}}$ 20 422 33 058 56 85.83
✅ m3u-editor 91.3 $\color{gray}{\textsf{+0.3\%}}$ 2 196 $\color{gray}{\textsf{-0.2\%}}$ 22 432 $\color{gray}{\textsf{+0}}$ $\color{green}{\textsf{-58}}$ $\color{orange}{\textsf{4 changed}}$ 38 195 $\color{green}{\textsf{-72}}$ 30 93.39 $\color{green}{\textsf{+0.02}}$
✅ agent-fleet 69.7 $\color{gray}{\textsf{+0.3\%}}$ 2 542 $\color{gray}{\textsf{-0.2\%}}$ 27 965 $\color{red}{\textsf{+2}}$ $\color{green}{\textsf{-34}}$ $\color{orange}{\textsf{7 changed}}$ 51 675 $\color{green}{\textsf{-32}}$ 131 93.84 $\color{green}{\textsf{+0.02}}$
✅ relaticle 62.8 3 813 $\color{gray}{\textsf{+0.2\%}}$ 2 548 4 843 14 98.09
✅ aureuserp 82.2 $\color{gray}{\textsf{+0.9\%}}$ 3 349 $\color{gray}{\textsf{+0.1\%}}$ 33 306 75 355 0 92.55
✅ kanka 71.4 $\color{gray}{\textsf{+0.4\%}}$ 2 449 $\color{gray}{\textsf{-0.2\%}}$ 17 264 34 054 7 94.01
✅ skuul 37.5 $\color{gray}{\textsf{-0.8\%}}$ 1 534 $\color{gray}{\textsf{-0.2\%}}$ 3 535 7 569 3 98.95
Total 797.6 $\color{gray}{\textsf{+0.1\%}}$ 36 893 217 665 $\color{red}{\textsf{+2}}$ $\color{green}{\textsf{-92}}$ $\color{orange}{\textsf{11 changed}}$ 417 760 $\color{green}{\textsf{-104}}$ 1 282 –
How to read this table
  • Wall: head seconds. A badge is colored only for a move of at least 3 s and 10 %, as shared runners are noisy: slower is red, faster is green, anything smaller is gray.
  • Memory: peak RSS of the largest process in Psalm's process tree, workers included. Red above +5 %, green below −5 %, gray between. A dash means a side has no figure.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base, and $\color{orange}{\textsf{N changed}}$ for issues at the same place and type with a new message (not counted in added or removed).
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1), order-dependent types excluded. Taint and dead-code issues are not weighed. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (--find-dead-code). More is red, fewer is green.
  • Coverage: inferred type coverage. A badge appears only for a move of at least 0.005 points, smaller moves are parallel-analysis jitter.
  • Total: sum over the apps compared on both sides (coverage is not summed). Badge thresholds and the heading words apply per app, not to the total, so the Total row can be colored differently from the heading.
  • Colors: $\color{red}{\textsf{red}}$ worse or more issues, $\color{green}{\textsf{green}}$ better or fewer issues, $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Icons: ✅ both sides produced a report · ❌ head produced none while base did · 💥 not comparable (base crashed, both crashed, or not run). A crash on both sides is not caused by this PR.
  • Performance (heading): regressed if a head run crashed or a wall or memory badge is red, else improved if one is green, else unchanged. Types (heading): direction of the total weighted change. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the PR.
✅ m3u-editor: issues +0 −58, 4 changed

Wall 91.0 → 91.3 s (+0.3 %) · Memory 2 200 → 2 196 MB (−0.2 %) · Coverage 93.37 → 93.39 %

Weighted 38 267 → 38 195 (−72, −0.19 %): L1 −45 · L2 −12 · L3 −1

Issue type Level base → head added removed changed
MixedMethodCall L1 1 825 → 1 797 0 28 0
RiskyTruthyFalsyComparison L2 1 390 → 1 378 0 12 0
MixedReturnStatement L1 446 → 437 0 9 0
MixedAssignment L1 3 052 → 3 048 0 4 0
MixedArgument L1 2 439 → 2 437 0 2 4
MixedArgumentTypeCoercion L1 453 → 452 0 1 0
MixedOperand L1 353 → 352 0 1 0
PossiblyNullReference L3 192 → 191 0 1 0

Removed (58)

  • app/Events/DvrRecordingStatusEvent.php:74 MixedArgument: Argument 1 of App\Events\DvrRecordingStatusEvent::__construct cannot be mixed|null, expecting null|string
  • app/Events/DvrRecordingStatusEvent.php:74 MixedMethodCall: Cannot determine the type of $recording->scheduled_start when calling method toIso8601String
  • app/Events/DvrRecordingStatusEvent.php:75 MixedArgument: Argument 1 of App\Events\DvrRecordingStatusEvent::__construct cannot be mixed|null, expecting null|string
  • app/Events/DvrRecordingStatusEvent.php:75 MixedMethodCall: Cannot determine the type of $recording->scheduled_end when calling method toIso8601String
  • app/Filament/CopilotTools/DvrOverviewTool.php:299 MixedAssignment: Unable to determine the type that $start is being assigned to
  • app/Filament/CopilotTools/DvrOverviewTool.php:299 MixedMethodCall: Cannot determine the type of $recording->scheduled_start when calling method format
  • app/Filament/CopilotTools/DvrOverviewTool.php:300 MixedAssignment: Unable to determine the type that $end is being assigned to
  • app/Filament/CopilotTools/DvrOverviewTool.php:300 MixedMethodCall: Cannot determine the type of $recording->scheduled_end when calling method format
  • app/Filament/GuestPanel/Resources/DvrRules/GuestDvrRuleResource.php:272 MixedReturnStatement: Could not infer a return type
  • app/Filament/GuestPanel/Resources/DvrRules/GuestDvrRuleResource.php:272 MixedReturnStatement: Possibly-mixed return value
  • … and 48 more

Changed (4)

  • app/Services/DvrPostProcessorService.php:562 MixedArgument: Argument 1 of abs cannot be mixed, expecting numeric → Argument 1 of Illuminate\Support\Carbon::diffInSeconds cannot be mixed, expecting DateTimeInterface|null|string
  • app/Services/DvrPostProcessorService.php:566 MixedArgument: Argument 1 of abs cannot be mixed, expecting numeric → Argument 1 of Illuminate\Support\Carbon::diffInSeconds cannot be mixed, expecting DateTimeInterface|null|string
  • app/Services/DvrVodIntegrationService.php:273 MixedArgument: Argument 1 of substr cannot be non-empty-mixed, expecting string → Argument 1 of substr cannot be non-empty-mixed|non-falsy-string, expecting string
  • app/Services/M3uProxyService.php:3871 MixedArgument: Argument 1 of abs cannot be mixed, expecting numeric → Argument 1 of Illuminate\Support\Carbon::diffInSeconds cannot be mixed, expecting DateTimeInterface|null|string
✅ agent-fleet: issues +2 −34, 7 changed

Wall 69.5 → 69.7 s (+0.3 %) · Memory 2 547 → 2 542 MB (−0.2 %) · Coverage 93.82 → 93.84 %

Weighted 51 707 → 51 675 (−32, −0.06 %): L1 −31 · L3 −3 · L4 +2

Issue type Level base → head added removed changed
MixedArgumentTypeCoercion L1 347 → 325 0 22 0
MixedArrayAssignment L1 84 → 79 0 5 0
MixedReturnTypeCoercion L1 436 → 432 0 4 0
PossiblyNullArgument L3 477 → 474 0 3 0
RiskyTruthyFalsyComparison L2 1 000 → 1 000 0 0 7
RedundantCast L4 622 → 624 2 0 0

Added (2)

  • app/Domain/Integration/Actions/HandleInboundWebhookAction.php:49 RedundantCast: Redundant cast to string
  • app/Livewire/Signals/SignalConnectorsPage.php:327 RedundantCast: Redundant cast to array

Removed (34)

  • app/Console/Commands/PollInputConnectors.php:99 MixedArrayAssignment: Cannot access array value on mixed variable $config['_team_id']
  • app/Console/Commands/PollInputConnectors.php:100 MixedArrayAssignment: Cannot access array value on mixed variable $config['connector_id']
  • app/Console/Commands/PollInputConnectors.php:101 MixedArgumentTypeCoercion: Argument 1 of App\Domain\Signal\Contracts\InputConnectorInterface::poll expects array<array-key, mixed>, but parent type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|array<'connector_id', never> pr…
  • app/Console/Commands/PollInputConnectors.php:107 MixedArgumentTypeCoercion: Argument 2 of App\Console\Commands\PollInputConnectors::getUpdatedConfig expects array<array-key, mixed>, but parent type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|array<'connector_id', never> p…
  • app/Domain/Approval/Jobs/FireApprovalWebhookJob.php:43 MixedArgumentTypeCoercion: Argument 3 of hash_hmac expects string, but parent type ''|(TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed) provided
  • app/Domain/Project/Listeners/NotifyAssistantOnProjectComplete.php:122 MixedArgumentTypeCoercion: Argument 1 of App\Domain\Telegram\Actions\SendTelegramReplyAction::execute expects string, but parent type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|null provided
  • app/Domain/Release/Crypto/Actions/SignReleaseAction.php:86 MixedArgumentTypeCoercion: Argument 1 of base64_decode expects string, but parent type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|null provided
  • app/Domain/Signal/Actions/RefreshOAuthTokenAction.php:65 MixedArgumentTypeCoercion: Argument 2 of App\Domain\Signal\Actions\RefreshOAuthTokenAction::refreshToken expects array<array-key, mixed>, but parent type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|array<never, neve…
  • app/Domain/Signal/Actions/RefreshOAuthTokenAction.php:68 MixedArgumentTypeCoercion: Argument 1 of array_merge expects array<array-key, mixed>, but parent type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|array<never, never> provided
  • app/Domain/Signal/Connectors/ApiPollingConnector.php:196 MixedReturnTypeCoercion: The declared return type 'array<array-key, mixed>|null' for App\Domain\Signal\Connectors\ApiPollingConnector::resolveCredentials is more specific than the inferred return type '(TGet:Illuminate\Contracts\Database\Eloq…
  • … and 24 more

Changed (7)

  • app/Domain/Integration/Actions/HandleInboundWebhookAction.php:45 RiskyTruthyFalsyComparison: Operand of type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|null contains type TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed, which can be falsy and truthy.… → Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • app/Domain/Signal/Models/ConnectorSignalSubscription.php:102 RiskyTruthyFalsyComparison: Operand of type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|null contains type TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed, which can be falsy and truthy. Thi… → Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • app/Domain/Signal/Models/SignalConnectorSetting.php:81 RiskyTruthyFalsyComparison: Operand of type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|null contains type TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed, which can be falsy and truthy. This can … → Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • app/Domain/Tool/Actions/ResolveAgentToolsAction.php:428 RiskyTruthyFalsyComparison: Operand of type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|null contains type TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed, which can be falsy and truthy. This can… → Operand of type array<array-key, mixed>|null contains type array<array-key, mixed>, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • app/Http/Controllers/PerTeamSignalWebhookController.php:174 RiskyTruthyFalsyComparison: Operand of type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|null contains type TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed, which can be falsy and truthy. This… → Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • app/Http/Controllers/SubscriptionWebhookController.php:60 RiskyTruthyFalsyComparison: Operand of type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|null contains type TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed, which can be falsy and truthy. This c… → Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.
  • app/Http/Controllers/TelegramWebhookController.php:37 RiskyTruthyFalsyComparison: Operand of type (TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed)|null contains type TGet:Illuminate\Contracts\Database\Eloquent\CastsAttributes as mixed, which can be falsy and truthy. This can c… → Operand of type null|string contains type string, which can be falsy and truthy. This can cause possibly unexpected behavior. Use strict comparison instead.

head b86db79 · base 647f1c0 (merge-base) · workflow run

Informational only — never fails the PR. Pick apps with /psalm-delta <group|app> ..., add --find-dead-code-style flags (/psalm-delta help lists both). Reproduce locally: bash bin/ci/delta.sh --apps "<group|app|--flag> ..." <pr-branch>.

@alies-dev

Copy link
Copy Markdown
Collaborator Author

/psalm-delta

@alies-dev

Copy link
Copy Markdown
Collaborator Author

/psalm-delta

@alies-dev alies-dev added the release:fix for PRs only (used by release-drafter) label Nov 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🐰 Bencher Report

Projectpsalm-plugin-laravel
Branchpr-2294
Testbedintel-v1
Click to view all benchmark results
BenchmarkLatencyBenchmark Result
seconds (s)
(Result Δ%)
Upper Boundary
seconds (s)
(Limit %)
Peak MemoryBenchmark Result
megabytes (MB) x 1e3
(Result Δ%)
Upper Boundary
megabytes (MB) x 1e3
(Limit %)
monica📈 view plot
🚷 view threshold
43.05 s
(-1.95%)Baseline: 43.91 s
47.80 s
(90.06%)
📈 view plot
🚷 view threshold
1.15 MB x 1e3
(0.00%)Baseline: 1.15 MB x 1e3
1.21 MB x 1e3
(95.24%)
🐰 View full continuous benchmarking report in Bencher

…ed docblocks

Expand the native get() signature in the caster's own context so self, static
and parent (including trait methods) name the caster instead of the model, and
use it only when the interface's TGet|null is the sole documentation of get().
@alies-dev

Copy link
Copy Markdown
Collaborator Author

/psalm-delta all

@alies-dev
alies-dev merged commit 2e42f45 into 4.x Nov 16, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix for PRs only (used by release-drafter)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Custom caster with a native-only get() return type resolves to TGet|null

2 participants