Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Feb 10, 2022
Merged

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

merged 2 commits into from
Feb 10, 2022

Conversation

axlon
Copy link
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
Member

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

@axlon
Copy link
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