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

Fix false issue when passing enum case to generic class #2930

Open
wants to merge 1 commit into
base: 1.10.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/Type/Generic/TemplateTypeVariance.php
Expand Up @@ -7,6 +7,7 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\AcceptsResult;
use PHPStan\Type\BenevolentUnionType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\MixedType;
use PHPStan\Type\NeverType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -157,6 +158,12 @@ public function isValidVarianceWithReason(?TemplateType $templateType, Type $a,
return AcceptsResult::createYes();
}

if ($b->isEnum()->yes()) {
if ($b->generalize(GeneralizePrecision::lessSpecific())->equals($a)) {
return AcceptsResult::createYes();
}
}

if ($this->invariant()) {
$result = $a->equals($b);
$reasons = [];
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php
Expand Up @@ -2525,6 +2525,18 @@ public function testBug5869(): void
$this->analyse([__DIR__ . '/data/bug-5869.php'], []);
}

public function testBug10484(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->checkThisOnly = false;
$this->checkNullables = true;
$this->checkUnionTypes = true;
$this->analyse([__DIR__ . '/data/bug-10484.php'], []);
}

public function testGenericsEmptyArray(): void
{
$this->checkThisOnly = false;
Expand Down
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-10484.php
@@ -0,0 +1,21 @@
<?php

namespace Bug10484;

enum Foo {
case Bar;
}

class Test {
/**
* @param \Ds\Set<Foo> $foos
*/
public function bar(\Ds\Set $foos): void
{
}

public function test(): void
{
$this->bar(new \Ds\Set([Foo::Bar]));
}
}