Skip to content

Commit 57c7fca

Browse files
authored
Merge pull request #167 from voku/copilot/update-readme-custom-phpstan-extension
Document PHPStan `meta()` return-type extension usage
2 parents adda744 + 3dae7c2 commit 57c7fca

2 files changed

Lines changed: 24 additions & 0 deletions

File tree

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,18 @@ $arrayy->Lars->lastname; // 'Müller'
120120

121121
The library offers type checking for phpdoc array-shape annotations, legacy `@property` phpdoc-class-comments, and native declared properties. Prefer the array-shape form because it can reuse the `Arrayy` template for IDE autocompletion and static-analysis support. `meta()` is also understood by PHPStan, so `meta()`-derived keys such as `$userMeta->city` and `$cityMeta->name` keep precise literal-string information during static analysis. When you want PHPStan to check reads precisely, prefer array-like access with literal keys (for example `$user['lastName']`) or narrowed `meta()` keys on these array-shape-based models. Do not combine array-shape annotations and `@property` tags on the same model.
122122

123+
If you use PHPStan and call `YourArrayySubclass::meta()`, you can register the custom return-type extension from `src/PHPStan/MetaDynamicStaticMethodReturnTypeExtension.php`. Use it when you want PHPStan to understand that `meta()` returns an object shape whose properties are the exact array keys collected from your array-shape annotations, `@property` tags, or native declared properties. That keeps expressions such as `$userMeta->id` typed as the literal string `'id'`, helps nested lookups like `$user[$userMeta->city][$cityMeta->name]`, and lets PHPStan report invalid meta-property access such as `$userMeta->ghost`.
124+
125+
The repository's own `phpstan.neon` registers the extension like this; copy the same service definition into your project's PHPStan config because this repository file is not shipped in the Composer package:
126+
127+
```neon
128+
services:
129+
-
130+
class: Arrayy\PHPStan\MetaDynamicStaticMethodReturnTypeExtension
131+
tags:
132+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
133+
```
134+
123135
```php
124136
/**
125137
* @template T of array{id: int, firstName: int|string, lastName: string, city?: City|null}

build/docs/base.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,18 @@ $arrayy->Lars->lastname; // 'Müller'
119119

120120
The library offers type checking for phpdoc array-shape annotations, legacy `@property` phpdoc-class-comments, and native declared properties. Prefer the array-shape form because it can reuse the `Arrayy` template for IDE autocompletion and static-analysis support. `meta()` is also understood by PHPStan, so `meta()`-derived keys such as `$userMeta->city` and `$cityMeta->name` keep precise literal-string information during static analysis. When you want PHPStan to check reads precisely, prefer array-like access with literal keys (for example `$user['lastName']`) or narrowed `meta()` keys on these array-shape-based models. Do not combine array-shape annotations and `@property` tags on the same model.
121121

122+
If you use PHPStan and call `YourArrayySubclass::meta()`, you can register the custom return-type extension from `src/PHPStan/MetaDynamicStaticMethodReturnTypeExtension.php`. Use it when you want PHPStan to understand that `meta()` returns an object shape whose properties are the exact array keys collected from your array-shape annotations, `@property` tags, or native declared properties. That keeps expressions such as `$userMeta->id` typed as the literal string `'id'`, helps nested lookups like `$user[$userMeta->city][$cityMeta->name]`, and lets PHPStan report invalid meta-property access such as `$userMeta->ghost`.
123+
124+
The repository's own `phpstan.neon` registers the extension like this; copy the same service definition into your project's PHPStan config because this repository file is not shipped in the Composer package:
125+
126+
```neon
127+
services:
128+
-
129+
class: Arrayy\PHPStan\MetaDynamicStaticMethodReturnTypeExtension
130+
tags:
131+
- phpstan.broker.dynamicStaticMethodReturnTypeExtension
132+
```
133+
122134
```php
123135
/**
124136
* @template T of array{id: int, firstName: int|string, lastName: string, city?: City|null}

0 commit comments

Comments
 (0)