-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TypeInfo] Add ArrayShapeType
class
#59827
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
Conversation
e1bca5b
to
294b185
Compare
3660bb4
to
e1d1d9b
Compare
foreach ($value as $key => $itemValue) { | ||
$valueType = $this->shape[$key]['type'] ?? false; | ||
if (!$valueType) { | ||
return false; |
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.
For extra keys, the behavior is different between PHPStan (allow) and psalm (restrict). You implemented the strict version: false
is returned when an extra key is present. This is consistent with psalm and PHPStorm 👍🏻
There is a syntax using ...
to allow extra keys (demo); but this is not supported by PHPStorm and PHP_CodeSniffer.
/** @param array{foo: string, ...} $a */
function fun(array $a): int
This feature can wait for a subsequent PR.
Thank you Mathias. |
This PR was merged into the 7.3 branch. Discussion ---------- [TypeInfo] Add `ArrayShapeType::$sealed` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | | License | MIT Implement sealed syntax for array shape type, as suggested in #59827 (comment). Commits ------- fded1eb [TypeInfo] Add `ArrayShapeType::$extraKeyType` and `ArrayShapeType::$extraValueType`
This PR was merged into the 7.3 branch. Discussion ---------- [TypeInfo] Add `ArrayShapeType::$sealed` | Q | A | ------------- | --- | Branch? | 7.3 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | | License | MIT Implement sealed syntax for array shape type, as suggested in symfony/symfony#59827 (comment). Commits ------- fded1eb5030 [TypeInfo] Add `ArrayShapeType::$extraKeyType` and `ArrayShapeType::$extraValueType`
Introduces the
ArrayShapeType
that holds the exact shape of an array. Also enables theStringTypeResolver
to parsearray{foo: int, bar?: string}
and to create the appropriate type.This PR needs #59824 to be merged first.