bmewburn / vscode-intelephense

PHP intellisense for Visual Studio Code
https://intelephense.com
Other
1.56k stars 92 forks source link

Undefined method 'onEachSide'. intelephense(P1013) #2912

Closed HugoSuarezJr closed 1 week ago

HugoSuarezJr commented 1 week ago

Describe the bug Intelephense says undefined method 'onEachSide' for the Illuminate\Database\Eloquent\Collection::onEachSide method.

To Reproduce $projects = Project::query()->paginate(10)->onEachSide(1);

Expected behavior There should be no Intelephense error as this is a defined method and the code works just fine but it displays as if it were wrong in VScode.

Screenshots Screenshot 2024-06-25 at 5 42 34 PM

Platform and version VSCode and PHP Intelephense v1.10.4

bmewburn commented 1 week ago

I think the problem is that paginate returns \Illuminate\Contracts\Pagination\LengthAwarePaginator which does not have this method declared. (You can hover on paginate to check this). https://github.com/laravel/framework/blob/11.x/src/Illuminate/Contracts/Pagination/LengthAwarePaginator.php

You could assign the result on paginate to a variable and narrow the type like so:


/** @var \Illuminate\Pagination\AbstractPaginator $paginator */
$paginator = $query->paginate(10);

//or alternatively

if ($paginator instanceof \Illuminate\Pagination\AbstractPaginator) {
  $projects = $paginator->onEachSide(1);
}

Or you could fool the extension by adding the onEachSide method to the \Illuminate\Contracts\Pagination\LengthAwarePaginator interface in a (non-executable) stub file. For example

intelephense_helper.php

namespace Illuminate\Contracts\Pagination;

interface LengthAwarePaginator {
  function onEachSide($param) {};
}