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

collector generics loose precision #8127

Closed
staabm opened this issue Oct 6, 2022 · 7 comments
Closed

collector generics loose precision #8127

staabm opened this issue Oct 6, 2022 · 7 comments

Comments

@staabm
Copy link
Contributor

staabm commented Oct 6, 2022

Bug report

it seems the generic type of a collector is loosing precision..?

Code snippet that reproduces the problem

collector

/**
 * @implements Collector<CallLike, array{string, TaintType::TYPE_*, string, int}>
 */
final class SinkCollector implements Collector
{
    public function getNodeType(): string
    {
        return CallLike::class;
    }

    public function processNode(Node $node, Scope $scope)
    {}
}

/**
 * @psalm-immutable
 */
final class TaintType
{
    public const TYPE_INPUT = 'input';
    public const TYPE_SQL = 'sql';
    public const TYPE_HTML = 'html';

    public const TYPES = [self::TYPE_INPUT, self::TYPE_SQL, self::TYPE_HTML];
}

rule

/**
 * @implements Rule<CollectedDataNode>
 */
final class TaintRule implements Rule
{
    public function getNodeType(): string
    {
        return CollectedDataNode::class;
    }

    public function processNode(Node $node, Scope $scope): array
    {
        $sinkCollectorData = $node->get(SinkCollector::class);
        dumpType($sinkCollectorData);
       // actual: Dumped type: array<string, array<int, array{string, string, string, int}>> 
      // expected Dumped type: array<string, array<int, array{string, TaintType::TYPE_*,, string, int}>> 
    }
}

https://phpstan.org/r/72a1ea44-59e0-41c2-af31-11eeb6723b37

@mergeable
Copy link

mergeable bot commented Oct 6, 2022

This bug report is missing a link to reproduction at phpstan.org/try.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

Please reproduce it on the playground by replicating all the involved classes and interfaces.

@staabm
Copy link
Contributor Author

staabm commented Oct 6, 2022

just updated the description and added a link

@ondrejmirtes
Copy link
Member

This is the infamous generics type generalization again.

@ondrejmirtes ondrejmirtes added this to the Generics milestone Oct 26, 2022
@devnix
Copy link

devnix commented Mar 20, 2023

Is there any known workaround to this issue?

@ondrejmirtes
Copy link
Member

Last night I came up with the idea that we mostly shouldn't generalize the generic type variables, except when they're in object generics, like Foo<int>.

Here's the resulting PR: phpstan/phpstan-src#2818

We can't do this for objects, because I want new Collection([1, 2, 3]) to become Collection<int>. This could be improved in the future thanks to this suggestion: #6732 (comment) (but it's pretty complex to implement so for now it has to wait).

The new behaviour now only applies to bleeding edge (https://phpstan.org/blog/what-is-bleeding-edge) so definitely enable it to get the taste of the future 👍

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 Jan 12, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants