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

[9.x] Add PHPStan-specific tags to improve analysis on model factories #41286

Closed
wants to merge 1 commit into from
Closed

[9.x] Add PHPStan-specific tags to improve analysis on model factories #41286

wants to merge 1 commit into from

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