Skip to content

Commit

Permalink
Fix false issue when passing enum case to generic class
Browse files Browse the repository at this point in the history
Fixes #10484
  • Loading branch information
Emil Masiakowski authored and EmilMassey committed Apr 16, 2024
1 parent 7961f7a commit 2216e84
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
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]));
}
}

0 comments on commit 2216e84

Please sign in to comment.