Skip to content

[9.x] Improve types on model factory methods#40902

Merged
nunomaduro merged 2 commits intolaravel:9.xfrom
axlon:patch-3
Feb 10, 2022
Merged

[9.x] Improve types on model factory methods#40902
nunomaduro merged 2 commits intolaravel:9.xfrom
axlon:patch-3

Conversation

@axlon
Copy link
Copy Markdown
Contributor

@axlon axlon commented Feb 9, 2022

This fixes the following PHPStan error found in the application boilerplate (tested at level 8):

 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  Line   database/factories/UserFactory.php                                                                                                                                                       
 ------ ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 
  36     Parameter #1 $state of method Illuminate\Database\Eloquent\Factories\Factory<App\Models\User>::state() expects array<string, mixed>|(callable(): array<string, mixed>), Closure(array):  
         array{email_verified_at: null} given.                                                                                                                                                    
 ------ -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

I also updated the types for create, createOne, createQuietly, make, makeOne, raw and new. I wanted to update lazy as well but its currently hardcoded as an array

@nunomaduro nunomaduro marked this pull request as draft February 10, 2022 11:32
@nunomaduro
Copy link
Copy Markdown
Member

Can you add a few tests on types folder? Or the existing tests already cover this?

@axlon
Copy link
Copy Markdown
Contributor Author

axlon commented Feb 10, 2022

@nunomaduro I added the additional type checks, I noticed that PHPStan will always report the parameters of callables that are parameters themselves as mixed, seems to be a bug: https://phpstan.org/r/7e566448-8b05-4b83-9082-f2169233c862. I added assertions for the $attributes parameter in callables anyway, but commented them out so its at least documented what it should be.

Either way this PR should solve a few cases where callables were previously not allowed, or were only allowed without parameters.

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.

2 participants