plugin icon indicating copy to clipboard operation
plugin copied to clipboard

[Feature Request]: Auto-complete model properties on custom methods

Open MatanYadaev opened this issue 3 years ago • 2 comments

Feature Description

This feature could be handy in situations that you don't use Laravel's method for creations or updates.

An example:

trait Notifiable
{
  public function notifications(): MorphMany
  {
    return $this->morphMany(Notification::class, 'owner')->orderByDesc('id');
  }

  /**
   * @param array<model-property<Notification>, mixed> $data
   * @return Notification
   */
  public function notify(array $data): Notification
  {
    return $this->notifications()->create($data);
  }
}
$user = User::firstOrFail();


$user->notifications()->create([
  // Here Laravel Idea auto-completes notification's properties
  'message' => 'example',
]);

$user->notify([
  // Here Laravel Idea doesn't auto-complete notification's properties
  'message' => 'example',
]);

As you can see in the example, I'm already using Larastan's custom PHPDoc type model-property. When I pass an array with a key that doesn't exist in the Notification model to the notify method - PHPStan alerts me about it:

Property 'message_content' does not exist in App\Models\Notification model.

I suppose that Laravel Idea could use the same (or similar) concept. Just suggesting.

Thanks. This feature could be very powerful.

MatanYadaev avatar Nov 25 '21 12:11 MatanYadaev

It will be hard to implement by reading phpDoc... but it can be solved by little instruction in the ide.json file. Is it ok for you?

adelf avatar Nov 26 '21 15:11 adelf

@adelf Sure, it will be better than nothing. Amazing, thanks!

MatanYadaev avatar Nov 27 '21 13:11 MatanYadaev