plugin icon indicating copy to clipboard operation
plugin copied to clipboard

[Bug]: It does not resolve multiple columns to eloquent model select() correctly

Open cyppe opened this issue 1 year ago • 1 comments

Bug description

Nothing major, but it does not resolve the format when you pass multple columns to select method.

CleanShot 2024-03-21 at 08 52 30@2x

I know I can send the columns as array, but the method accepts both.

CleanShot 2024-03-21 at 09 01 58@2x

Plugin version

2024.1

Operating system

MacOS

Steps to reproduce

No response

Relevant log output

No response

cyppe avatar Mar 21 '24 08:03 cyppe

Reproduced. As a workaround, I can recommend the Model::query()->select() variant. I'll try to find a way to fix this, but can't promise anything. It's a PhpStorm inspection.

adelf avatar Apr 12 '24 00:04 adelf

The actual method in Laravel's Query Builder \Illuminate\Database\Query\Builder::select() looks like this:

public function select($columns = ['*'])
{
    $this->columns = [];
    $this->bindings['select'] = [];

    $columns = is_array($columns) ? $columns : func_get_args();

    // ...
}

→ see framework/src/Illuminate/Database/Query/Builder.php at 11.x · laravel/framework

If one were to call this method directly, PhpStorm is "smart" enough to notice the call to func_get_args(). PhpStorm will not report a parameter mismatch for these functions (enabled by default in the inspection settings):

Image

You can see this behavior in action in the following example:

Possible Solution

When it comes to solving this issue in Laravel Idea, I guess there could be two options:

  1. Change the signature of the select() method in the generated _ide_helper_model_builders_[...].php file(s) to:
    /**
      * ...
      * @see \Illuminate\Database\Query\Builder::select
      * @method $this select(array|mixed $columns = ['*'], ...$arguments)
      * ...
      */
    
  2. Add an actual dummy implementation for the select() method. This method could either call func_get_args() itself or call parent::select($columns).

Personally, I am in favor of option 1, since it wouldn't require any actual method implementations.

FeBe95 avatar Jan 20 '25 12:01 FeBe95

Thank you. We will try option 1.

adelf avatar Jan 20 '25 12:01 adelf

Thanks for looking into this!

One additional note: The function func_get_args() is used a total of three times in Builder.php:

  1. select($columns = ['*'])
  2. addSelect($column)
  3. distinct()

If the suggested solution works as expected, it would be great to have it implemented for all three instances.

Further thoughts

If multiple @method entries for the same method is a valid thing to do, the following solution could be even better ("more correct"):

/**
  * @method $this select(array $columns = ['*'])
  * @method $this select(string|mixed $column, string|mixed ...$columns)
  */

FeBe95 avatar Jan 20 '25 13:01 FeBe95

Thanks for the fix, everything works as expected! I think this ticket can be closed.

FeBe95 avatar Feb 16 '25 18:02 FeBe95