Skip to content

[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

Merged
merged 1 commit into from
Mar 14, 2025
Merged

Conversation

mtarld
Copy link
Contributor

@mtarld mtarld commented Feb 21, 2025

Q A
Branch? 7.3
Bug fix? no
New feature? yes
Deprecations? no
Issues
License MIT

Introduces the ArrayShapeType that holds the exact shape of an array. Also enables the StringTypeResolver to parse array{foo: int, bar?: string} and to create the appropriate type.

This PR needs #59824 to be merged first.

@mtarld mtarld force-pushed the feat/array-shape branch from e1d1d9b to af1231b Compare March 3, 2025 11:16
foreach ($value as $key => $itemValue) {
$valueType = $this->shape[$key]['type'] ?? false;
if (!$valueType) {
return false;
Copy link
Member

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.

@GromNaN
Copy link
Member

GromNaN commented Mar 14, 2025

Thank you Mathias.

@GromNaN GromNaN merged commit cf5270c into symfony:7.3 Mar 14, 2025
11 checks passed
@mtarld mtarld deleted the feat/array-shape branch March 14, 2025 09:22
fabpot added a commit that referenced this pull request Mar 26, 2025
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`
symfony-splitter pushed a commit to symfony/type-info that referenced this pull request Mar 26, 2025
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`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants