Skip to content

Conversation

@devfrey
Copy link
Contributor

@devfrey devfrey commented Mar 1, 2022

Nuno made model factories generic in this PR. To preserve compatibility with PhpStorm the return types were defined as a union of \Illuminate\Database\Eloquent\Model|TModel. PHPStan normalises this into \Illuminate\Database\Eloquent\Model (because TModel is a subtype of Model), resulting in a bunch of unknown property/method errors when running PHPStan on tests that use model factories.

This PR adds specific tags for PHPStan that overrule the standard PHPDoc tags, to preserve the specific model types provided by the generics. PhpStorm's understanding of return types is unaffected (because it ignores these PHPStan-specific tags), but PHPStan gains a lot of benefits from these specific tags as can be seen in the updated tests.

@nunomaduro
Copy link
Member

In the future, we will be replacing \Illuminate\Database\Eloquent\Model by TModel. But we need to wait for IDEs to follow up. Only PHPStorm seems to be following.

And, we don't plan to add specific phpstan annotations for now.

@nunomaduro nunomaduro closed this Mar 1, 2022
@devfrey
Copy link
Contributor Author

devfrey commented Mar 1, 2022

Thanks for your reply @nunomaduro.

As an alternative solution I'd like to suggest to remove Model from the unions completely. You mention wanting to wait for IDE support, but Eloquent collections don't have this 'fallback' either (example here) and I couldn't find any complaints. In its current form the generic annotations are useless to PHPStan – which I think is a missed opportunity.

I'd be happy to send an updated PR.

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