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 error on laravel scope methods #2104

Closed
dpetrovaliev opened this issue Nov 11, 2024 · 2 comments
Closed

Phpstan error on laravel scope methods #2104

dpetrovaliev opened this issue Nov 11, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@dpetrovaliev
Copy link

  • Larastan Version: 2.9.10
  • Laravel Version: 10.48.22

Description

I got this error when running ./vendor/bin/phpstan analyse:

 ------ -------------------------------------------------------------------------------- 
  Line   Models/Event.php                                                                
 ------ -------------------------------------------------------------------------------- 
  74     Call to an undefined method Illuminate\Database\Eloquent\Builder::matchDate().  
 ------ -------------------------------------------------------------------------------- 

It looks like the analysis doesn't take in mind the scope naming convention in Laravel.

Laravel code where the issue was found

...
    public function scopeMatchDate(Builder $query, Carbon $date): Builder|Event
    {
        return $query->....;
    }


    public function scopeActive(Builder $query): Builder|Event
    {
        return $query->matchDate(now())
            ->where(function ($query) {
                ...
                })->orWhere(function ($q) {
                    ...
                });
            });
    }
    
    ...
@dpetrovaliev dpetrovaliev added the bug Something isn't working label Nov 11, 2024
@calebdw
Copy link
Contributor

calebdw commented Nov 11, 2024

You need to add the proper generics to your methods.

Also, these scopes do not return Event so they should not be included in the return:

    /**
     * @param Builder<static> $query
     * @return Builder<static>
     */
    public function scopeMatchDate(Builder $query, Carbon $date): Builder
    {
        return $query;
    }

    /**
     * @param Builder<static> $query
     * @return Builder<static>
     */
    public function scopeActive(Builder $query): Builder
    {
        return $query->matchDate(now())
            ->where(function ($query) {
                // ...
                })->orWhere(function ($q) {
                    // ...
                });
            });
    }

@canvural
Copy link
Collaborator

canvural commented Nov 11, 2024

Hi,

Larastan doesn't know which model the query builder operates on. So you need to write your scopeActive method like this:

/** @param Builder<Event> $query */
public function scopeActive(Builder $query): Builder

Same goes for scopeMatchDate method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants