-
-
Notifications
You must be signed in to change notification settings - Fork 33
Add PHPStan dot-notation return-type extension and harden typing/fixtures #179
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
rg -n -C4 '`@property`(-read)?|class (User|City)' README.md build/docs/base.mdRepository: voku/Arrayy
Length of output: 14366
🌐 Web query:
PHPStan undefined property@property-readexample💡 Result:
The
@property-readPHPDoc tag is used in PHPStan to declare "magic" properties that are available for reading but not writing, typically when a class implements the get magic method [1][2][3]. If you are encountering an "Access to an undefined property" error despite using@property-read, it is usually due to one of the following reasons: 1. Improper Placement: The@property-readtag must be placed in the PHPDoc block immediately preceding the class or trait definition [2][3]. It cannot be used to declare "abstract properties" on interfaces or classes in a way that enforces implementation by subclasses [4][5]. 2. PHP 8.2+ Dynamic Property Restrictions: Since PHP 8.2, native support for dynamic properties has been restricted. When you type-hint an interface that uses@property-read, PHPStan may report an "Access to an undefined property" error because PHP interfaces do not natively support property declarations [6][4]. To resolve this, you can use the@phpstan-require-extendstag on the interface to reference a base class that supports dynamic properties [6][4]. 3. Missing Type Information: PHPStan requires a type definition for@property-readtags (e.g., /@property-readstring $name */) [7]. Omitting the type will cause a parsing error [7]. Recommended Solutions: - Verify Placement: Ensure the tag is above the class/trait [1][2]. - Use@phpstan-require-extends: If using interfaces in PHP 8.2+, add@phpstan-require-extends[6][4]. - Use Proper Syntax: Ensure the tag follows the format@property-read[Type] [name] [1][2]. - Alternatives: For complex scenarios, consider using a custom class reflection extension or configuring universalObjectCratesClasses in your phpstan.neon file [6][8][9]. If the code is static, refactoring to use a standard getter method (e.g., getFieldName) is the most robust approach [5].Citations:
@propertyphpstan/phpstan#10302🌐 Web query:
"Access to an undefined property" PHPStan array-shape without@property-read``💡 Result:
The "Access to an undefined property" error when working with PHPStan object-shapes (often referred to as array-shapes in PHPStan notation) typically occurs when you attempt to access a property that is not explicitly defined in the type definition [1][2]. If you are encountering this error even for optional properties (e.g.,
object{prop?: string}), ensure you are using the latest version of PHPStan [3][4]. Recent updates (particularly in the 2.1.x development branch) have significantly improved how PHPStan handles optional properties in object shapes to reduce false positives [3][5][4]. If the error persists or is intentional, you have several ways to address it: 1. Use Null Coalescing Operator: If the property is optional, accessing it directly may trigger an error. Using the null coalescing operator (e.g.,$object->prop ?? 'default') is the idiomatic way to handle potentially missing properties safely [6][4]. 2. Suppress the Error: If you are certain the property will exist at runtime, you can ignore the specific error using its identifier,property.notFound[2]. This can be done inline: //@phpstan-ignoreproperty.notFound $value = $object->prop; Or globally in your phpstan.neon configuration: parameters: ignoreErrors: - identifier: property.notFound 3. Narrow the Type: Ensure that the variable is correctly typed as the object-shape before access [1]. If the variable is typed as a broaderobjectormixed, PHPStan cannot guarantee the property exists [1]. 4. Check Configuration: Ensure that strict dynamic property checking rules are not set too aggressively if your codebase relies on dynamic access, though this is less common for fixed object-shapes [6]. For most scenarios involving optional object-shape properties, upgrading your PHPStan version is the most effective solution, as the maintainers have addressed several issues where valid optional property accesses were incorrectly flagged as undefined [3][5][4].Citations:
Declare the magic properties used by the PHPStan examples.
The examples in
README.mdandbuild/docs/base.mdread$user->profileand$user->profile->name, but the shownUserandCityclasses declare only@template Tarray shapes and no@property/@property-readtags. Add matching declarations to the demonstrated models, or state that property-style access is runtime-only in this context.📍 Affects 2 files
README.md#L125-L133(this comment)build/docs/base.md#L124-L132🤖 Prompt for AI Agents