How to ignore "access to an undefined property" using PHPStan and Laravel
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Static Analysis: How to Silence "Undefined Property" Errors in PHPStan with Laravel
As developers working within the robust ecosystem of Laravel, we often rely on static analysis tools like PHPStan to ensure code quality and catch potential runtime errors before deployment. While these tools are incredibly valuable, frameworks introduce complexities—especially when dealing with dynamic data structures like Laravel's Eloquent Resources—that can sometimes generate false positives.
One common frustration arises when using PHPStan alongside Laravel components. Specifically, when accessing properties on Illuminate\Http\Resources objects, PHPStan often flags "Access to an undefined property," even though the code is logically sound within the framework context.
This post will dive into how to effectively manage these framework-specific warnings using PHPStan's configuration file (phpstan.neon) rather than cluttering your application with unnecessary PHPDoc annotations. We will determine the precise regex needed to ignore these common property access errors, ensuring a cleaner analysis process for any Laravel project.
The Context: Why Does This Happen?
The error you are encountering—Access to an undefined property App\Http\Resources\LocationResource::$id—occurs because PHPStan performs strict static analysis based purely on the declared types within your code. When dealing with Laravel Resources, these classes often rely on dynamic data passed during the request cycle. PHPStan sees a method call or property access that it cannot statically verify exists on the specific instance at that exact point in execution, leading to warnings.
While adding @var annotations is a valid solution for explicit type hinting, you rightly prefer managing this suppression centrally via phpstan.neon. This approach keeps your codebase cleaner and separates framework-specific noise from core business logic.
We aim to use the configuration file to suppress errors that are known to be safe within the Laravel context. For instance, when working with Eloquent models or Resources, we are dealing with properties (like id, name, created_at) that are guaranteed to exist on the underlying model object, even if PHPStan struggles to track them through the Resource abstraction layer immediately.
The Solution: Crafting the Perfect Regex for ignoreErrors
To successfully ignore these specific property access errors without ignoring unrelated bugs, we need a precise regular expression. Simply ignoring all undefined properties will introduce massive noise. We need a pattern that specifically targets accesses on classes that look like Laravel Resources.
The general format of the error you are seeing is:Access to an undefined property [ClassName]::[propertyName]
Based on your example, where the class namespace structure is predictable (e.g., App\Http\Resources\...), we can construct a highly specific pattern for PHPStan's ignoreErrors directive in your phpstan.neon file.
The Correct Pattern
To ignore errors of the form "Access to an undefined property [ClassName]::[propertyName]", you should use a regex that captures any access attempt on a property following a class name:
parameters:
paths:
- app/
ignoreErrors:
- '#Access to an undefined property .*::.*#'
Explanation of the Regex:
#: Starts the regular expression pattern.Access to an undefined property: Matches the literal starting text of the error message..*: This is the crucial part. It matches any character (.) zero or more times (*). This allows the regex to match any class path (likeApp\Http\Resources\LocationResource) and any property name that follows the double colon separator (::).
By using .*::.*, we instruct PHPStan to ignore any error message that starts with "Access to an undefined property" followed by a class reference and a dot-separated property name. This is broad enough to cover the specific case you described without being overly specific (which can break if Laravel changes its namespace structure).
Implementation Example in phpstan.neon
Here is how your final configuration should look, ensuring that only the framework-related noise is suppressed:
parameters:
paths:
- app/
ignoreErrors:
# Ignore all access errors specifically related to undefined properties on classes,
# which often occurs with Laravel Resources and Eloquent models.
- '#Access to an undefined property .*::.*#'
This configuration tells PHPStan: "When you encounter an error message detailing the access of an undefined property on a class (followed by whatever is on the right side of the ::), disregard it." This keeps your static analysis focused on actual logical errors rather than framework-specific noise.
Conclusion
Managing static analysis in large, framework-heavy applications like those built with Laravel requires a balance between strictness and pragmatism. By understanding how PHPStan parses error messages, we can leverage the ignoreErrors directive in phpstan.neon to surgically suppress known, safe warnings related to framework abstractions, such as accessing properties on Laravel Resources.
This approach allows you to maintain high code quality while streamlining your development workflow. As you continue building robust applications, remember that effective static analysis is a powerful tool for catching errors early, ensuring the reliability and scalability of your projects, much like adhering to best practices outlined by organizations like the Laravel Company.