Skip to content

Do not report generic covariant issue without strict check#5437

Open
VincentLanglet wants to merge 1 commit intophpstan:2.1.xfrom
VincentLanglet:genericStrictCheck
Open

Do not report generic covariant issue without strict check#5437
VincentLanglet wants to merge 1 commit intophpstan:2.1.xfrom
VincentLanglet:genericStrictCheck

Conversation

@VincentLanglet
Copy link
Copy Markdown
Contributor

No description provided.

$type = TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
if ($type instanceof GenericObjectType) {
$type = $type->changeVariances(
array_fill(0, count($type->getVariances()), TemplateTypeVariance::createBivariant()),
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What if we changed it to covariant only if it's invariant? Also we might need to do it only if isSuperType is no. So closer to the end of the method?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes I updated to only change invariant to covariant.

But I think I can do it every time and it's easier this way to me. It avoids having to do this strategy in every possible cases (constant array, iterable, etc).

Since invariant checks equals and covariant isSuperTypeOf, I can't think of a false positif to always change covariant to invariant.

@VincentLanglet VincentLanglet marked this pull request as ready for review April 9, 2026 21:47
@phpstan-bot
Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants