Larastan: Result of void scope is used

Created on 5 Feb 2020  路  3Comments  路  Source: nunomaduro/larastan

  • Larastan Version: 0.5.1
  • --level used: 5

Description

After upgrading I receive this error:

| Line | app/Models/Concerns/HasUuid.php (in context of class App\Models\Media) |
|------|------------------------------------------------------------------------|
| 30 | Cannot call method exists() on void. |
| 30 | Result of static method App\Models\Media::byUuid() (void) is used. |

Laravel code where the issue was found

The scope is the following:

    public function scopeByUuid(Builder $query, $token): void
    {
        $query->whereIn($this->getUuidName(), Arr::wrap($token));
    }

and it's called like: static::byUuid($uuid)->exists(). void scopes will still return the already in use query builder instance. So we've chosen to flag all scopes that only add something to the query as void and use real return types only for scopes that aggregate some real values.

Most helpful comment

Fixed in #450

I'd still consider this an edge case, but it wasn't hard to support. :+1:

All 3 comments

Hi,

From the docs:

Scopes should always return a query builder instance:

So, I think Larastan is right in this case.

That's true but it's a should rule and Laravel supports it:
https://github.com/illuminate/database/blob/4da3cb0023899b5f96ad4e8e49d75e1c761f3a1d/Eloquent/Builder.php#L980

And it's common practice to use this as difference between these scope types:
https://timacdonald.me/query-scopes-meet-action-scopes/

So it would be great if the scope auto detection/completion also supports the void return type and uses the query builder instead.

Fixed in #450

I'd still consider this an edge case, but it wasn't hard to support. :+1:

Was this page helpful?
0 / 5 - 0 ratings