Larastan: Recommendation how to use DynamicCallOnStaticMethodsRule

Created on 11 Aug 2020  Â·  5Comments  Â·  Source: nunomaduro/larastan

  • Larastan Version: 0.6.2
  • --level used: 6

Description

I tried exploring to use DynamicCallOnStaticMethodsRule from https://github.com/phpstan/phpstan-strict-rules

Check that statically declared methods are called statically

The use-case is to prevent (accidentally? intentionally for sake of consistency?) calling something defined as static function… as $this->… when self:: should be used.

When I enabled it, I was greeted with thousands of errors because of all the static calls to \Illuminate\Database\Eloquent\Builder, \Illuminate\Database\Query\Builder and some from the Illuminate\Database\Eloquent\Relations namespace.

Some specific error examples:

  226    Dynamic call to static method
         Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Mod
         el>::orderBy().
…
  299    Dynamic call to static method
         Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Mod
         el>::forPage().
…
  73     Dynamic call to static method
         Illuminate\Database\Eloquent\Relations\HasMany<Illuminate\Database\El
         oquent\Model>::where().

List goes on, I've > 400 such errors 😅

So right now I've to use these ignoreErrors:

    - '/Dynamic call to static method Illuminate\\Database\\Eloquent\\Builder/'
    - '/Dynamic call to static method Illuminate\\Database\\Query\\Builder/'
    - '/Dynamic call to static method Illuminate\\Database\\Eloquent\\Relations\\/'

Is there anything Larastan can assist doing here? 🤔

Most helpful comment

🙈 Fully closing your eyes helps in the short-term :)

All 5 comments

🙈 Fully closing your eyes helps in the short-term :)

This is a known issue with that rule I think. #355 and #483 are the same types of issues. I still don't know exactly how to solve this.

I was thinking the issue comes from here maybe. So this has to change dynamically somehow.

Can you maybe test with changing to false here? Just to confirm again, it solves some of the issues.

Can you maybe test with changing to false

I can confirm (I guess), hard to say because now I get > 400 "Static call to instance method" errors; but yes, it flipped the side.

Interestingly, from the ignoreErrors I pasted, only two were reported as being unused

     Ignored error pattern /Dynamic call to static method
     Illuminate\\Database\\Query\\Builder/ was not matched in reported errors.
     Ignored error pattern /Dynamic call to static method
     Illuminate\\Database\\Eloquent\\Relations\\/ was not matched in reported
     errors.

The one ignoring Dynamic call to static method Illuminate\\Database\\Eloquent\\Builder is still encountered it seems.


I tried to search the issues for DynamicCallOnStaticMethodsRule. I know see the mistake I made, because although I installed the phpstan-struct-rules package, I only did this to enable this one rule, not everything.


Anyway, feel free to close this in favour of the existing ones. I don't think this issue brings news to the table…

Thank you Markus.

You may comment or open another issue Anytime!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

szepeviktor picture szepeviktor  Â·  4Comments

mariomka picture mariomka  Â·  4Comments

JeroenVanOort picture JeroenVanOort  Â·  3Comments

tranba picture tranba  Â·  4Comments

Braunson picture Braunson  Â·  4Comments