barryvdh / laravel-ide-helper

IDE Helper for Laravel
MIT License
14.17k stars 1.16k forks source link

New Laravel attributes are not correctly recognized when method is protected #1378

Closed bbprojectnet closed 7 months ago

bbprojectnet commented 2 years ago

Versions:

Description:

Only public and type casted get/set methods attributes are detected.

But, according to https://laravel.com/docs/9.x/eloquent-mutators mutator method should be protected.

Also, it would be nice if ide helper supported the generic attribute declaration, like that:

    /**
     * Name attribute.
     *
     * @return \Illuminate\Database\Eloquent\Casts\Attribute<string, never>
     */
    protected function name(): Attribute
    {
        return Attribute::make(
            get: fn () => 'foo',
        );
    }

if the get/set methods do not have a return type specified.

gajosadrian commented 1 year ago

Yes, it's a new missing feature in ide-helper... the code is located in: https://github.com/barryvdh/laravel-ide-helper/blob/dda200fd34d2b9151bd6d9ddecaeb421978d6b99/src/Console/ModelsCommand.php#L570 but currently I don't have time to fix it.

Derhelios66 commented 11 months ago

The problem should already be fixed with this commit https://github.com/barryvdh/laravel-ide-helper/commit/39885645b91b0266290fce03bbfd155a67bb2e27. But since then there was no release.

mfn commented 7 months ago

New release was made -> closing

marc31 commented 3 months ago

Hey, thanks for fixing this bug. I have one remaining question though. Suppose an attribute returns a collection of Post objects, how should I type hint it?

protected function filteredPosts(): Attribute
{
    return Attribute::make(
        /**
         * @return \Illuminate\Database\Eloquent\Collection<int, \App\Models\Post>
         */
        get: function (): {
            return $this->post->where('post_online');
        }
    );
}

I'd like to ensure the correct typing for this scenario. Thanks in advance for your assistance!

mfn commented 3 months ago

Looks correct to me; does it work or give you any trouble?

marc31 commented 3 months ago

it's being inferred as @mixed.

omaressaouaf commented 2 months ago

@mfn @Derhelios66 Just to confirm on @marc31 answer, the mixed type is the only inferred type for the new accessor/mutators even if we docblock the attribute with a generic type like @marc31 did