larastan: Parameter $column of method orWhere() errors when Illuminate\Database\Query\Expression is given

  • Larastan Version: 0.7.0
  • --level used: 5
  • PHPStan Version: 0.12.75

Description

When on level 5, the orWhere() method on Illuminate\Database\Eloquent\Builder<Illuminate\Database\Eloquent\Model>::orWhere() fails when a Illuminate\Database\Query\Expression is given, even though it’s in the DocBlock of aforementioned method.

/**
 * Add an "or where" clause to the query.
 *
 * @param  \Closure|array|string|\Illuminate\Database\Query\Expression  $column
 * @param  mixed  $operator
 * @param  mixed  $value
 * @return $this
 */
public function orWhere($column, $operator = null, $value = null)

Laravel code where the issue was found

$subQuery->orWhere(
    DB::raw(sprintf('lower(%s)', $column)),
    'like',
    '%' . strtolower($searchTerm) . '%'
);

Full error:

{
    "description": "Parameter #1 $column of method Illuminate\\Database\\Eloquent\\Builder<Illuminate\\Database\\Eloquent\\Model>::orWhere() expects array<int|string, mixed>|Closure|string, Illuminate\\Database\\Query\\Expression given.",
    "fingerprint": "3d188e1afc222590ee964c4330945aaa601e2139aaf0454b43c2e3ad64662aef",
    "location": {
        "path": "app/JsonApi/AbstractAdapter.php",
        "lines": {
            "begin": 46
        }
    }
}

Note: it says it expects an array that looks like int|string or mixed, that’s another error I think?

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 19 (11 by maintainers)

Most upvoted comments

@szepeviktor @canvural I’m happy to report that the error is gone when applying the changes mentioned in https://github.com/nunomaduro/larastan/issues/784#issuecomment-781229537.

On to the tests now.

And explicitly the int, mixed part.

array<model-property<TModelClass>|int, mixed> this type says it is an array where it’s keys can be either model-property<TModelClass> or int, and it’s value is mixed type.

That makes perfect sense. Thank you!

For now I’ve changed it to this and am currently manually testing it:

/**
 * Add an "or where" clause to the query.
 *
 * @param  \Closure|model-property<TModelClass>|array<model-property<TModelClass>|int, mixed>|\Illuminate\Database\Query\Expression  $column
 * @param  mixed  $operator
 * @param  mixed  $value
 * @return $this
 */
public function orWhere($column, $operator = null, $value = null);

@szepeviktor @canvural What about the array type that’s included in the stub but not in the original DocBlock? Is that expected?