plugin icon indicating copy to clipboard operation
plugin copied to clipboard

[Bug]: Guarded attribute inspectation on tests

Open jopicornell opened this issue 2 months ago • 7 comments

Bug description

This "bug" is closely related to #1261 but this time, for tests. We use factories to run feature tests, and this inspection shows up. Factories are a testing feature and, IMO should not contain this kind of inspections.

Plugin version

1.4.1.251

Operating system

MacOS

Steps to reproduce

Create a feature test and, in a test case, just call a factory create model within it:

$user = User::factory()->create(['email_verified_at' => now()]);

where email_verified_at is a guarded attribute

Relevant log output


jopicornell avatar Oct 30 '25 08:10 jopicornell

Factories already should be excluded:

Image

Internally, it checks if factory() method contains \Illuminate\Database\Eloquent\Factories\Factory type in method reference returning type. Maybe you are using different factories library?

gorbunov avatar Oct 31 '25 05:10 gorbunov

Thank you for the response!

I'm using an Eloquent Factory:

Image

And I'm using it in the test like this

Image

I have created a new Laravel project and I don't see this inspection, so it has to be something that I'm using in my project that the plugin doesn't like. It is a fairly new project, so it is weird. If you have any idea I can keep trying things.

I'll keep trying things and let you know if I come up with something.

jopicornell avatar Nov 03 '25 18:11 jopicornell

Image

Just to verify: clicking ->factory() method here brings you to correct factory class, right?

Can you maybe try clearing caches and regenerate helper files, just to check if this is maybe some old index messing things up?

gorbunov avatar Nov 07 '25 06:11 gorbunov

I am having the same problem, and can confirm that having cleared the PHPStorm caches (File > Invalidate Caches > All boxes checked) and regenerating helper code (Laravel > Generate Helper Code) I'm still seeing the warning about guarded attributes:

Image

My plugin version is 1.5.0.252 PHPStorm 2025.2.4

ghostal avatar Dec 02 '25 18:12 ghostal

@gorbunov Regarding the information shared about how the plugin makes the decision internally, could this change be the issue?

https://github.com/laravel/framework/commit/2f1f3301bdb4d6ac45f980702e2d2bc544e2d104#diff-c14e536174956829af10331e4333c434b9c4c2722f9e28080aa1e32f0e8ee75a

Specifically, the HasFactory trait now declares @return TFactory in its annotation for the factory() method, not @return \Illuminate\Database\Eloquent\Factories\Factory<static>?

ghostal avatar Dec 02 '25 18:12 ghostal

To remove any doubt, the issue in my case isn't being caused by the isVendor() call, which I have removed here:

Image

The IDE understands that the factory() method is returning the correct factory class:

Image

And EloquentProposalFactory is extending the standard Laravel factory:

Image

ghostal avatar Dec 02 '25 18:12 ghostal

@gorbunov Regarding the information shared about how the plugin makes the decision internally, could this change be the issue?

Specifically, the HasFactory trait now declares @return TFactory in its annotation for the factory() method, not @return \Illuminate\Database\Eloquent\Factories\Factory<static>?

Yes, that's possible, phpStorm itself isn't super-good yet in resolving generic types, so maybe we don't get correct type in checks. I'll try to reproduce on fresh Laravel project.

gorbunov avatar Dec 03 '25 06:12 gorbunov