laravel-ide-helper
laravel-ide-helper copied to clipboard
Wrong namespace for related models with different namespaces
Versions:
- ide-helper Version: 2.10.0
- Laravel Version: 8.49.2
- PHP Version: 8.0.8 (Laravel Sail: 1.8.4)
Description:
In my project the models are divided into multiple namespaces:
-
\App\Models
-
\App\Models\Pivots
-
\App\Models\Cashier
- ...
IDE Helper is generating docs as expected apart from relations.
Lets say we have the \App\Models\User
and the \App\Models\School
model. Also since a user can be training at different schools they are related M:N via the \App\Models\Pivots\SchoolUser
pivot model with relations school -> \App\Models\School
and user -> \App\Models\User
.
When parsing relations IDE helper seems to assume, that all share the same namespace. Therefore the namespace for the related model is omitted in the _ide_helper_models.php
. The \App\Models\Pivots\IdeHelperSchoolUser
mixin will have a property user -> User
and school -> School
. This is absolutely ok if they all are in the same namespace. Since in my case the namespaces are different my IDE cant find the referenced Models (\App\Models\Pivots\User
and \App\Models\Pivots\School
don't exist) and fails to properly hint types/properties/methods.
Steps To Reproduce:
- Setup fresh Laravel project with IDE Helper
- Create Models in the
\App\Models
namespace - Create Pivots for a N:M relationship within the
\App\Models\Pivots
namespace - Create relations on the pivot model to its related models
- Run
php artisan ide-helper:models
orsail artisan ide-helper:models
- Look at the helper mixin for the Pivot Model, the namespace for the related models is omitted though its needed in this case
Note: im talking about martial arts schools, obviously pupils would not be attending multiple schools. ;)
Suggestion: always use fully qualified namespaces, even if a short one might be sufficient. This might solve the problem already.
I've started to run into this more in one of my projects where I have been more aggressive with namespacing my models. I may try to find a fix for it.
I think this can be fixed by using force_fqn
in config/ide-helper.php
. I've enabled that and it fixed the current case of not referencing the namespaced class. It isn't clear what other impact this might have and curious why this isn't the default?
I think because people don't like the full namespace as it clutters the code more. But if it's an issue, you can create a PR to change the default.
@barryvdh It is unclear to me if it's an issue if there is an option to change it. It just wasn't immediately obvious to me that it was an option that existed at all until I dived into the ide helper code directly.
I think because people don't like the full namespace as it clutters the code more.
Do you think the "clutter" worry is an older/outdated concern? I wonder if the force_fqn
default maybe made more sense before there were more options to hide the generated code away from the models themselves? If you think this is likely, I'd be more inclined to send a PR to change the default. :)
Either way, if force_fqn
is not true
, could we find a way to let the user know? Perhaps a message like, "Detected models related to models in a different namespace. Please set force_fqn
to true
if your IDE is not able to resolve the related model classes on its own."
Happy to work on this from either or both directions. :)