Skip to content
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

checkBenevolentUnionTypes + @template TKey of array-key = false positive #9766

Closed
LastDragon-ru opened this issue Aug 16, 2023 · 3 comments
Closed
Labels
Milestone

Comments

@LastDragon-ru
Copy link

Bug report

I'm trying to enable checkBenevolentUnionTypes (as recomended in #9750), but seems I found something stange :)

<?php declare(strict_types = 1);

/**
 * @template TKey of array-key
 * @template TItem
 */
class PHPStanBug {
    /**
     * @param iterable<TKey, TItem> $items
     */
    public function __construct(
        private iterable $items,
    ) {
        // empty
    }

    /**
     * @return iterable<TKey, TItem>
     */
    protected function getItems(): iterable {
        return $this->items;
    }
}

PHPStan will report that:

 21     Method PHPStanBug::getItems() should return iterable<TKey of (int|string), TItem> but returns iterable<TKey of (int|string), TItem>.
         💡 Type int is not always the same as TKey. It breaks the contract for some argument types, typically subtypes.
         💡 Type string is not always the same as TKey. It breaks the contract for some argument types, typically subtypes.

With @template TKey of string|int there are no errors.

Code snippet that reproduces the problem

https://phpstan.org/r/3a30aea2-ea44-4546-8774-3a9589abe496

Expected output

Probably no error?

Did PHPStan help you today? Did it make you happy in any way?

No response

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Aug 16, 2023
@ondrejmirtes
Copy link
Member

This is a really easy fix. RuleLevelHelper needs to transform TemplateBenevolentUnionType into TemplateUnionType in these two places:

Right now TemplateBenevolentUnionType is transformed into UnionType, resulting in these errors. /cc @VincentLanglet

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#2587

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants