larastan / larastan

⚗️ Adds code analysis to Laravel improving developer productivity and code quality.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

@mixin not working correctly on laravel-mongodb

promatik opened this issue · comments

  • Larastan Version: 2.9.5
  • Laravel Version: 11.6.0

Description

After this update: mongodb/laravel-mongodb@fdfb5e5#diff-284e164f76aeb13f424d154f9208d91fdbb0112b0a50ad8b1dfa237176f03cf2 on mongodb/laravel-mongodb, all my models became Illuminate\Database\Eloquent\Model.

That update just added a /** @mixin Builder */ on src/Eloquent/Model.php (Mongo extension of the Eloquent Model).

I tried running phpstan alone (without larastan) and there was no errors, so I believe this issue is related with larastan.

After that, my models that used to be inferred correctly, are now inferred as Illuminate\Database\Eloquent\Model, throwing me errors like:

Access to an undefined property Illuminate\Database\Eloquent\Model::$share_code.

Thank you for your report!

Where does share_code property come from?

Oh, sorry, that's just an attribute on one of my models.

I have a Room model which has the $share_code attribute.
After mongo added the mixin to MongoModel (their src/Eloquent/Model.php), all my models that extended the Mongo Model (in this case, Room model) "became" Illuminate\Database\Eloquent\Model.

That's why I have dozens of errors like Access to an undefined property Illuminate\Database\Eloquent\Model::$share_code, on places like $room->share_code (where $room is a Room).

This Room is just an example, because I have several models which extend the MongoModel, and all are throwing the same errors after that mixin was added.

Hi,

This is expected. Eloquent builder is a generic class. It should've been specified the inner types. When not specified, the outer bound (in this case Model) is assumed. Also adding mixin to the Model is not recommended. Larastan has it's own extensions that resolves missing methods for builders. And mixin can interfere with that. Lastly this does not sound like a Larastan issue.

Hi @canvural, this issue was introduced in laravel-mongodb v4.3.

From what the maintainers says, adding that annotation is not an issue for phpstan (https://phpstan.org/r/10adb986-2c43-4b77-9a80-6f31d562441f) it only becomes an issue for larastan.

Please check the thread here; mongodb/laravel-mongodb#2923
So, isn't this an issue in Larastan?

Hi @canvural, this issue was introduced in laravel-mongodb v4.3.

From what the maintainers says, adding that annotation is not an issue for phpstan (phpstan.org/r/10adb986-2c43-4b77-9a80-6f31d562441f) it only becomes an issue for larastan.

Please check the thread here; mongodb/laravel-mongodb#2923 So, isn't this an issue in Larastan?

What I'm saying is that the generic type should be specified when you do @mixin Builder You can see the error here. This is the root cause of the issue. If you don't supply the inner type the Model is assumed and you get the undefined property errors.

An @mixin Builder<$this> or @mixin Builder<static> would work---but the generics only exist in the Larastan stubs which would not be a dependency of mongodb. The best thing to do would be to add the builder generics to the framework which would then make the generic mixin possible

You can also use stub files to fix wrong 3rd party PHPDocs.