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

Incorrect property annotation on models with factories #1664

Open
scajal opened this issue Jan 23, 2025 · 2 comments
Open

Incorrect property annotation on models with factories #1664

scajal opened this issue Jan 23, 2025 · 2 comments
Labels

Comments

@scajal
Copy link

scajal commented Jan 23, 2025

Versions:

  • ide-helper Version: 3.5.4
  • Laravel Version: 11.39.0
  • PHP Version: 8.3

Description:

When generating Eloquent annotations on a model that uses a factory, the tool generates an incorrect $use_factory property with the following annotation:
@property-read \App\Models\TFactory|null $use_factory

Then, when running a static analysis tool (like Larastan), it fails:
PHPDoc tag @property-read for property App\Models\User::$use_factory contains unknown class App\Models\User\TFactory.

The model is defined as follows:

class User extends Model
{
    /** @use HasFactory<\Database\Factories\UserFactory>*/
    use HasFactory;

    ...
}

Steps To Reproduce:

  • Create a model with a factory
  • Apply the HasFactory trait to the model, and the corresponding @use
  • Generate the model's annotations
  • Run a static analysis tool (e.g.: Larastan)
@scajal scajal added the bug label Jan 23, 2025
@scajal scajal changed the title Wrong property anotation on models with factories Incorrect property annotation on models with factories Jan 23, 2025
@LauJosefsen
Copy link
Contributor

LauJosefsen commented Jan 24, 2025

It looks like it is due to Laravel 11.39 adding the getUseFactoryAttribute method to the factory trait.

laravel/framework@ffa1a12#diff-c14e536174956829af10331e4333c434b9c4c2722f9e28080aa1e32f0e8ee75aR46

The return type should be

    /**
     * Get the factory from the UseFactory class attribute.
     *
     * @return TFactory|null
     */

With TFactory being either defined as follows

    /** @use HasFactory<\Database\Factories\MyModelFactory> */
    use HasFactory;

or simply \Illuminate\Database\Eloquent\Factories\Factory.
or through the new #UseFactory attribute in Laravel 11.39

By stepping through the models code, it seems the getReturnTypeFromDocBlock method fails to find the type parsing the docblock, and defaults to the current namespace.

Would it be possible to add this method to the blacklisted methods in getPropertiesFromMethods untill we potentially can evaluate the generics?

I think the generics parsing is broken as it expects the @template tag to be present on the method docblock, but it is infact present in the traits docblock instead.

@chack1172
Copy link
Contributor

@LauJosefsen we don't need to evalutate the generic. This method should be skipped and not added as an attribute, it is a protected static method used internally by HasFactory, it is not an attribute.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants