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

[11.x] feat: improve Factory generics, add generics to HasFactory #52005

Merged
merged 1 commit into from
Jul 8, 2024

Conversation

calebdw
Copy link
Contributor

@calebdw calebdw commented Jul 3, 2024

Hello!

This PR updates the Factory generics to make the types more accurate. I also added generics to the HasFactory trait, as well as allow the user to define a static $factory property in lieu of overriding the newFactory method which helps slim down models. Of course the method can still be overridden if desired.

// before
class User extends Authenticatable
{
    use HasFactory;

    protected function newFactory(): UserFactory
    {
        return UserFactory::new();
    }
}

// after
class User extends Authenticatable
{
    /** @use HasFactory<UserFactory> */
    use HasFactory;

    protected static string $factory = UserFactory::class;
}

Thanks!

@Jubeki
Copy link
Contributor

Jubeki commented Jul 3, 2024

Because I know, that PHP-CS-Fixer and Pint can have problems with formatting / ordering traits with comments in between, I wanted to ask, if the following is possible too:

/** @use HasFactory<UserFactory> */
class User extends Authenticatable
{
    use HasFactory;
}

Great work!!! ❤️

@calebdw
Copy link
Contributor Author

calebdw commented Jul 3, 2024

@Jubeki, no it looks like placing @use on the class level does not work:

 ------ -------------------------------------------------------------------------------------------------------------------------- 
  Line   Autoload.php                                                                                                              
 ------ -------------------------------------------------------------------------------------------------------------------------- 
  15     Class User uses generic trait Illuminate\Database\Eloquent\Factories\HasFactory but does not specify its types: TFactory  
         🪪  missingType.generics                                                                                                  
 ------ -------------------------------------------------------------------------------------------------------------------------- 

For what it's worth though, I've never had ordering issues with Pint (uses PHP-CS-Fixer) so that could be fixed.

@Jubeki
Copy link
Contributor

Jubeki commented Jul 4, 2024

@calebdw I checked again, it seems I remembered incorrectly. It was a spacing issue and not an ordering issue: laravel/pint#278
So should be no problem!

@taylorotwell taylorotwell merged commit 2f1f330 into laravel:11.x Jul 8, 2024
28 checks passed
@calebdw
Copy link
Contributor Author

calebdw commented Jul 8, 2024

Thanks @taylorotwell!

@calebdw calebdw deleted the hasfactory branch July 8, 2024 15:09
HDVinnie added a commit to HDInnovations/UNIT3D-Community-Edition that referenced this pull request Jul 19, 2024
- With the latest larastan and laravel changes such as laravel/framework#52005 we have a lot more errors. This commit generates a new baseline.
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.

3 participants