BenSampo / laravel-enum

Simple, extensible and powerful enumeration implementation for Laravel.
https://sampo.co.uk/blog/using-enums-in-laravel
MIT License
2k stars 164 forks source link

Model annotation generation with laravel-ide-helper broken #265

Closed dennis-koster closed 2 years ago

dennis-koster commented 2 years ago

Hi,

In the documentation it is stated that model annotations can be generated with the laravel-ide-helper package. However, this does not seem to work (anymore) in my case. Given the following example:

<?php

declare(strict_types=1);

namespace App\Models;

use App\Enums\TestEnum;
use Illuminate\Database\Eloquent\Model;

class TestModel extends Model
{
    /**
     * @var string[]
     */
    protected $fillable = [
        'testValue',
    ];

    /**
     * @var array<string, string>
     */
    protected $casts = [
        'testValue' => TestEnum::class
    ];
}
<?php

declare(strict_types=1);

namespace App\Enums;

use BenSampo\Enum\Enum;

/**
 * @method static static FOO()
 * @method static static BAR()
 */
class TestEnum extends Enum
{
    protected const FOO = 'Foo';
    protected const BAR = 'Bar';
}

After running php artisan ide-helper:model --reset --write the following annotation is generated:

/**
 * App\Models\TestModel
 *
 * @property mixed $testValue
 * @method static \Illuminate\Database\Eloquent\Builder|TestModel doesntHaveByNonDependentSubquery($relationMethod, ...$constraints)
 * @method static \Illuminate\Database\Eloquent\Builder|TestModel hasByNonDependentSubquery($relationMethod, ...$constraints)
 * @method static \Illuminate\Database\Eloquent\Builder|TestModel newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|TestModel newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|TestModel orDoesntHaveByNonDependentSubquery($relationMethod, ...$constraints)
 * @method static \Illuminate\Database\Eloquent\Builder|TestModel orHasByNonDependentSubquery($relationMethod, ...$constraints)
 * @method static \Illuminate\Database\Eloquent\Builder|TestModel query()
 * @mixin \Eloquent
 */

Expected behaviour

I would expect the annotation to be:

@property TestEnum $testValue

Actual behaviour

The mixed type is used for the property type:

@property mixed $testValue

Version

laravel/framework: v8.78.1 bensampo/laravel-enum: v3.4.2 barryvdh/laravel-ide-helper: v2.12.3

spawnia commented 2 years ago

Looks like an issue with laravel-ide-helper: https://github.com/barryvdh/laravel-ide-helper/issues/1312. The Enum class implements the interface Illuminate\Contracts\Database\Eloquent\Castable and thus should be recognized correctly. I don't think there is anything we can do to improve this, so I am closing the issue.

Some general advice: In my experience the generated PHPDocs are imperfect in too many ways, I typically generate them once and then manually curate them as I make changes to the model.