plugin icon indicating copy to clipboard operation
plugin copied to clipboard

[Feature Request]: Enable HelperCode and Go-To features for custom classes

Open ventuinha opened this issue 2 years ago • 2 comments

Feature Description

First of all I'd like to thank you all for the great plugin. Me and my team we've been using Laravel Idea for some time it helped us upgrading our work quality quite a lot. I'm really grateful for that

The suggestion: Making other classes be capable of having a helpercode generated for Attributes and casts like in the Model class. The collection proxy helper classes would also be great regarding that.

How could this be useful? In my case, we have a plan of adapting the use of LaravelData with the same Attribute behavior from Models (adapting the existing HasAttributes trait from Eloquent). Maybe other features could make use of this but, this is currently the case I have to propose


I tried figuring out if that was possible using ide.json but for now, it seems to only work for completions. Also tried using PHPDoc with @mixin Model @see, @extend to see if I could get the helpercode but nope.

The workaround I'm currently working on is creating my own doc command using ide-helper, but Laravel Idea works way better on guessing the types and could expand the possibilities

If this suggestion isn't really interesting or isn't going to be applied, let me know so I can report to the team :c

ventuinha avatar Dec 08 '23 15:12 ventuinha

Hello. The first step in the Eloquent helper generation is field fetching. How to fetch fields for classes in your case?

adelf avatar Dec 11 '23 13:12 adelf

Hello adelf, sorry for the late reply.

For this case I believe the first step in this case could be fetching the properties of the class That should be equivalent to fetching the database columns of the model as fields


The example below for the sake of keeping it small I'll be keeping some declarations

The intended class

class UserData extends DataWithDynamicAttributes
{
    public Carbon $created_at;
    public Carbon $updated_at;
    
    public function __construct(
        public string $first_name,
        public string $last_name,
        public string $email,
    ){}

    public function fullName(): Attribute
    {
        return Attribute::make(get: fn(): string => $this->first_name . ' ' . $this->last_name);
    }
    
}

Should be capable of generating same fields as


new class extends Migration
{
    public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->string('first_name');
            $table->string('last_name');
            $table->string('email');
            $table->timestamps();
        });
    }
};

class User extends Model
{
    public function fullName(): Attribute
    {
        return Attribute::make(get: fn(): string => $this->first_name . ' ' . $this->last_name);
    }
}

How Laravel Idea should understand when or which classes it should be using such feature is up to you, however, PHPDoc or idea.json telling which classes it should be inheriting to enable this feature could be enough anyways

ventuinha avatar Dec 14 '23 13:12 ventuinha