Skip to content

Comments

[12.x] Improve types of Arr helper#58518

Merged
taylorotwell merged 23 commits intolaravel:12.xfrom
shaedrich:improve-array-types
Feb 1, 2026
Merged

[12.x] Improve types of Arr helper#58518
taylorotwell merged 23 commits intolaravel:12.xfrom
shaedrich:improve-array-types

Conversation

@shaedrich
Copy link
Contributor

Benefits

✅ Improved static analysis
✅ Improved type inference
✅ Increased type coverage

Based on

Highlights

  • true and false literal types instead of bool where possible
  • Narrowed whether strings or arrays are null upon return where possible
  • Use of generics to infer the return type where possible
  • Narrowing of callables where possible
  • Definition of array shapes where possible

@shaedrich shaedrich marked this pull request as draft January 27, 2026 22:22
@shaedrich shaedrich marked this pull request as ready for review February 1, 2026 03:19
@taylorotwell taylorotwell merged commit 1f00ef9 into laravel:12.x Feb 1, 2026
70 checks passed
@shaedrich shaedrich deleted the improve-array-types branch February 1, 2026 16:47
@calebdw
Copy link
Contributor

calebdw commented Feb 4, 2026

@shaedrich, the ::wrap() generics are incorrect and causes unresolvable type warnings in phpstan when the result is used

I would say that all of the time, Arr::wrap() will be called with a union type which the conditional returns are unable to handle (you're going to have to use an actual phpstan extension for more accurate types, maybe open up a Larastan issue); for example, this:

/** @var float[]|float|null $foo */
assertType('array{}|array<float>', Arr::wrap($foo));

fails with the following (as you can see, the float is not wrapped and the array is double wrapped):

 ------ ----------------------------------------------------------------------------- 
  Line   Support/Arr.php                                                              
 ------ ----------------------------------------------------------------------------- 
  184    Expected type array{}|array<float>, actual: array<array<float>|float>|float  
 ------ ----------------------------------------------------------------------------- 

I"ll submit PR to revert this

@shaedrich
Copy link
Contributor Author

Ah, darn! Thanks for reporting 👍🏻

@calebdw
Copy link
Contributor

calebdw commented Feb 4, 2026

I'm working on a fix that's actually pretty close without needing an extension 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants