barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.25k stars 1.17k forks source link

factory method not recognized when using ide-helper:models -M #1589

Open lk77 opened 2 months ago

lk77 commented 2 months ago

Versions:

Question:

When i'm using ide-helper:models -M, the HasFactory::factory() method is not recognized properly, it works fine without the -M, somehow, using a mixin cause this line :

 * @method static \Database\Factories\MyFactory factory($count = null, $state = [])

to not be recognized by phpstorm, which cause "polymorphic calls" warnings, because phpstorm expect a generic Factory instance, and not the real factory for the model,

do you have any idea why that is ?

thanks.

khaled-sadek commented 1 month ago

@lk77 Could you let me know if you tried to define your factory implementation? like the following :

/** @use HasFactory<YourModelFactory> */
use HasFactory;

note: don't forget to replace YourModelFactory with the real factory class for this model.

lk77 commented 1 month ago

I tried that solution, and it does not work, the only solution i have is to copy paste the "@method static" line from _ide_helper_models.php to the actual model

and it seems it's the same with all static methods like query(), they do not work with the -M option

i guess there is some sort of "precedence" calculation between ide-helper annotations, and laravel annotations, or type inference of some sort

edit :

i removed all annotations from laravel manually in vendor, just to test, and now i can't ctrl+click anymore, but it still does not find the annotations in _ide_helper_models.php

madebycaliper commented 3 weeks ago

@khaled-sadek I'm having the same problem and the suggested fix didn't work. I tried the comment hint with the full path to the factory class and just the class alone (e.g. WorkSiteFactory and Database\Factories\WorkSiteFactory).

I just refactored to use mixins and really prefer it, but this makes writing tests incredibly annoying. Might switch back if there's not fix for this. Thanks!

lk77 commented 2 weeks ago

@madebycaliper

i have something like that :

/**
 * @method static \Database\Factories\User\UserFactory factory($count = null, $state = [])
 *
 * @mixin IdeHelperUser
 */
class User extends Authenticatable