3

I am using Laravel 10 + PHPStan.

When I use Resource with the Laravel code, then PHPStan sends me this error:

18     Access to an undefined property App\Http\Resources\LocationResource::$id.
19     Access to an undefined property App\Http\Resources\LocationResource::$name.
20     Access to an undefined property App\Http\Resources\LocationResource::$comment.
21     Access to an undefined property App\Http\Resources\LocationResource::$library_id.
22     Access to an undefined property App\Http\Resources\LocationResource::$created_at.

I know I can solve this issue with some annotations. But I do not want to use that. I prefer use the phpstan.neon file and declare inside which error phpstan must ignore. See the documentation here.

So my phpstan.neon file looks like that:

parameters:
paths:
    - app/
ignoreErrors:
    - '#Call to an undefined method [a-zA-Z0-9\\_::]#'
    - '#Access to an undefined property App\\Http\\Resources\\[a-zA-Z0-9]::#'

It is the second 'ignoreErrors' line which is not correct.

Which regex I must enter in this conf file to ignore these kinds of errors?

1
  • 2
    Because it is an API Resource, I would add @mixin to the class definition, usage is @mixin CLASS, doing so will help with $this->property, the IDE will understand it Commented Apr 9, 2023 at 18:35

2 Answers 2

6

Ignoring the errors is not a solution.

Add @mixin to the Resource class with the related model:

/**
 * @mixin \App\Models\Location
 */
class LocationResource extends JsonResource
{
//...
Sign up to request clarification or add additional context in comments.

Comments

4

I found this solution , add this line in the phpstan.neon file in the "ignoreErrors" section :

# to ignore these errors :  Access to an undefined property App\Http\Resources\LocationResource::$id.
        - '#Access to an undefined property App\\Http\\Resources\\[a-zA-Z0-9::a-zA-Z]#'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.