Skip to content

New format computed property accessor not recognised #1315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pindab0ter opened this issue Feb 10, 2022 · 12 comments
Open

New format computed property accessor not recognised #1315

pindab0ter opened this issue Feb 10, 2022 · 12 comments
Labels

Comments

@pindab0ter
Copy link
Contributor

pindab0ter commented Feb 10, 2022

Versions:

  • ide-helper Version: 2.12.2
  • Laravel Version: 9.0.0
  • PHP Version: 8.1.2

Description:

PR #1289 added support for the new attribute mutator/accessor format, but calculated properties/attributes aren't recognised yet.

Steps To Reproduce:

This example calculated property results in * @property-read string $full_name:

public function getFullNameAttribute(): string
{
    return "$this->first_name $this->last_name";
}

The following example is not recognised and does not add the same @property.

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

It should also result in (only) $fullName instead of $full_name.

The latter should also be protected instead of public of course, but that's a separate issue (#1293).

@pindab0ter pindab0ter added the bug label Feb 10, 2022
@mfn
Copy link
Collaborator

mfn commented Feb 10, 2022

We're open to PRs \o/

@sawirricardo
Copy link

are there any progress/PR on this?

@john-inkesta
Copy link

If you make the function public, and provide a return type like so: fn(): string, it works.

@pindab0ter
Copy link
Contributor Author

That's true, but then it's no longer an accessor/mutator, which is the topic of this issue.

@john-inkesta
Copy link

john-inkesta commented Aug 22, 2023

What I mean is, if you do this:

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

It will generate a full_name property for you.

That's all you want, isn't it?

@sawirricardo
Copy link

does it matter though using public/protected?

@john-inkesta
Copy link

does it matter though using public/protected?

The Laravel docs makes them protected. But ide-helper only picks them up when they are public.

@mfn
Copy link
Collaborator

mfn commented Aug 23, 2023

The Laravel docs makes them protected. But ide-helper only picks them up when they are public.

The PR was merged #1339 but there has been no release after that.

You can try the dev-master version I guess.

@pindab0ter
Copy link
Contributor Author

What I mean is, if you do this:

public function fullName(): Attribute

{

    return Attribute::get(fn(): string => "$this->first_name $this->last_name");

}

It will generate a full_name property for you.

That's all you want, isn't it?

True. That's what I do nowadays. That said, Laravel supports accessors without typehinting whereas IDE Helper doesn't.

Hence my PR #1411.

@TzviPM
Copy link

TzviPM commented Oct 17, 2023

Any idea when this will be released on packagist? I'd prefer not to directly require dev-master, but this does work great for our use case. @barryvdh

taylorotwell pushed a commit to laravel/jetstream that referenced this issue Nov 3, 2023
We need to typehint mutator function return to let IDE Helper package know the type of the attribute.

barryvdh/laravel-ide-helper#1315 (comment)

Then PHPStan will not throw error on undefined profile_photo_url property.
@pindab0ter
Copy link
Contributor Author

I have given up on waiting for this and have stopped using this package in favour of the Laravel IDEA plugin.

@barryvdh
Copy link
Owner

barryvdh commented Feb 5, 2024

A new version was released an hour ago; https://github.com/barryvdh/laravel-ide-helper/releases/tag/v2.14.0

growno1127 added a commit to growno1127/jetstream that referenced this issue Mar 15, 2024
We need to typehint mutator function return to let IDE Helper package know the type of the attribute.

barryvdh/laravel-ide-helper#1315 (comment)

Then PHPStan will not throw error on undefined profile_photo_url property.
panich76 added a commit to panich76/jetstream that referenced this issue Mar 5, 2025
We need to typehint mutator function return to let IDE Helper package know the type of the attribute.

barryvdh/laravel-ide-helper#1315 (comment)

Then PHPStan will not throw error on undefined profile_photo_url property.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants