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

PHPStan level 2 #253

Merged
merged 19 commits into from
Aug 28, 2024
Merged

PHPStan level 2 #253

merged 19 commits into from
Aug 28, 2024

Conversation

SanderMuller
Copy link
Contributor

No description provided.

@SanderMuller SanderMuller changed the title phpstan-level-2 PHPStan level 2 Aug 21, 2024
phpstan.neon.dist Outdated Show resolved Hide resolved
phpstan.neon.dist Outdated Show resolved Hide resolved
phpstan.neon.dist Outdated Show resolved Hide resolved
phpstan.neon.dist Outdated Show resolved Hide resolved
@SanderMuller SanderMuller marked this pull request as ready for review August 21, 2024 19:10
phpstan.neon.dist Outdated Show resolved Hide resolved
@staudenmeir
Copy link
Owner

@SanderMuller Do you have any idea how to solve these errors?

I tried type-hinting the ancestorPost relationship with @return \Staudenmeir\EloquentHasManyDeep\HasOneDeep<Post>, but PHPStan still thinks that the result is a collection.

Bildschirmfoto 2024-08-25 um 23 24 17

@SanderMuller
Copy link
Contributor Author

@SanderMuller Do you have any idea how to solve these errors?

I tried type-hinting the ancestorPost relationship with @return \Staudenmeir\EloquentHasManyDeep\HasOneDeep<Post>, but PHPStan still thinks that the result is a collection.

Bildschirmfoto 2024-08-25 um 23 24 17

We could either add ignore statements, baseline them or add @var docblocks at each usage. I guess PHPStan can just infer that it's a collection of models based on the Staudenmeir\EloquentHasManyDeep\HasOneDeep relation which extends HasManyDeep which extends HasManyThrough, so it's based on a one on many relation. You could refactor HasOneDeep to extend HasOneThrough and use trais for the common logic instead of inheritance.

Let me know which direction you want to take, then I can take a look

@SanderMuller
Copy link
Contributor Author

@staudenmeir I've resolved it for now via 1bee685

Keep in mind that users of the package(s) might encounter the same challenge if the cause isn't solved

@staudenmeir
Copy link
Owner

Thanks @SanderMuller.

I guess PHPStan can just infer that it's a collection of models based on the Staudenmeir\EloquentHasManyDeep\HasOneDeep relation which extends HasManyDeep which extends HasManyThrough, so it's based on a one on many relation.

It's unfortunate that @extends \Illuminate\Database\Eloquent\Relations\HasOneThrough<TRelatedModel> in the HasOneDeep relation doesn't override that.

You could refactor HasOneDeep to extend HasOneThrough and use trais for the common logic instead of inheritance.

I want to avoid that because it would be a breaking change. Laravel recently refactored the HasOneThrough relation in a similar way and it broke code in my packages.

@staudenmeir staudenmeir merged commit 7b1dc99 into staudenmeir:main Aug 28, 2024
31 checks passed
@staudenmeir
Copy link
Owner

Thanks for this great contribution! I released a new version.

@SanderMuller
Copy link
Contributor Author

Thanks for this great contribution! I released a new version.

🎉 thank you for the time and effort you put into this

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