Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[TypeInfo] Fix template key-type for array#62738

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
DjordyKoert wants to merge1 commit intosymfony:7.3
base:7.3
Choose a base branch
Loading
fromDjordyKoert:fix-type-info-generic

Conversation

@DjordyKoert
Copy link
Contributor

QA
Branch?7.3
Bug fix?yes
New feature?no
Deprecations?no
IssuesFix #...
LicenseMIT

#62388 broke type detection for template keys for array:

/** * @template TKey of array-key * @template TValue */class Collection {}

Above example currently throws an exceptionSymfony\Component\TypeInfo\Exception\InvalidArgumentException: "TKey" is not a valid array key type. even thoughTKey is a type ofarray-key (int|string).

This PR fixes this by getting the wrapped type when dealing with a template type when checking if a valid array key type is provided.

Copy link

CopilotAI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Pull request overview

This PR fixes a bug where template keys constrained toarray-key (which isint|string) incorrectly threw anInvalidArgumentException when used as array key types. The fix unwraps template types to access their bounds before validation.

Key Changes:

  • Added unwrapping logic for template types in array key validation to check the bound type rather than the template wrapper
  • Added comprehensive test cases covering template keys witharray-key bounds
  • Updated theDummyCollection fixture with template annotations forTKey andTValue

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

FileDescription
src/Symfony/Component/TypeInfo/Type/CollectionType.phpAdded logic to unwrapTemplateType and validate its bound when checking array key types
src/Symfony/Component/TypeInfo/Tests/TypeResolver/StringTypeResolverTest.phpAdded test cases for arrays and lists with template types, plus a negative test for invalid template key bounds
src/Symfony/Component/TypeInfo/Tests/Fixtures/DummyCollection.phpAdded template annotations forTKey (constrained toarray-key) andTValue to support new test cases

💡Add Copilot custom instructions for smarter, more guided reviews.Learn how to get started.

Comment on lines 53 to 65
$keyType =$this->getCollectionKeyType();

if ($keyTypeinstanceof TemplateType) {
$keyType =$keyType->getWrappedType();
}

$keyTypes =$keyTypeinstanceof UnionType ?$keyType->getTypes() : [$keyType];

foreach ($keyTypesas$type) {
if (!$typeinstanceof BuiltinType || !\in_array($type->getTypeIdentifier(), [TypeIdentifier::INT, TypeIdentifier::STRING],true)) {
thrownewInvalidArgumentException(\sprintf('"%s" is not a valid array key type.', (string)$keyType));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

We must be able to handle

  • Union of scalars
  • Union of template types, such asTFoo|TBar.
  • Template type that is a union
  • Template type that is scalar

To handle every case, maybe we can do the following:

$keyType =$this->getCollectionKeyType();foreach ($keyType->traverse(false)as$t) {// to traverse composite but not wrapped (because nullable, collection, etc are not valid)if ($tinstanceof UnionType) {continue;    }if (($tinstanceof BuiltinType ||$tinstanceof TemplateType) &&$t->isIdentifiedBy(TypeIdentifier::INT, TypeIdentifier::STRING) {continue;    }// others types, such as intersection, nullable types, etc are not validthrownewInvalidArgumentException(\sprintf('"%s" is not a valid array key type.', (string)$keyType));}

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

Copilot code reviewCopilotCopilot left review comments

@mtarldmtarldmtarld requested changes

Assignees

No one assigned

Projects

None yet

Milestone

7.3

Development

Successfully merging this pull request may close these issues.

3 participants

@DjordyKoert@mtarld@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp