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] Makes database factories generic #39169

Merged
merged 1 commit into from
Oct 12, 2021
Merged

Conversation

nunomaduro
Copy link
Member

@nunomaduro nunomaduro commented Oct 12, 2021

This pull request makes the public methods ( and not protected ) of the database factory class generic. In other words, it allows to get auto-completion like this:
Screenshot 2021-10-12 at 13 51 10

Note that, to get this flavor people need to add this annotation to the top of their existing database factory classes. New database factory classes, created from php artisan make:factory already include this annotation.

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Team>
 */
class TeamFactory extends Factory
{
    // ..
}

For people who opt for not using this annotation, the auto-completion still works, but on the base model class:
Screenshot 2021-10-12 at 13 55 17

PHPStan wise is still not perfect, as we need the \Illuminate\Database\Eloquent\Model|TModel union is needed to make PHPStorm understand that \Illuminate\Database\Eloquent\Model is the default generic type of TModel.

If this pull request gets merged, I will update the UserFactory class in our skeletons.

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