Skip to content

Commit

Permalink
Fix generalization of EnumCaseObjectType
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 28, 2022
1 parent 73f14db commit 9e42896
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Type/Enum/EnumCaseObjectType.php
Expand Up @@ -10,6 +10,7 @@
use PHPStan\TrinaryLogic;
use PHPStan\Type\CompoundType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\GeneralizePrecision;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use PHPStan\Type\VerbosityLevel;
Expand Down Expand Up @@ -126,6 +127,11 @@ public function getProperty(string $propertyName, ClassMemberAccessAnswerer $sco
return parent::getProperty($propertyName, $scope);
}

public function generalize(GeneralizePrecision $precision): Type
{
return new parent($this->getClassName(), null, $this->getClassReflection());
}

/**
* @param mixed[] $properties
*/
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Expand Up @@ -728,6 +728,11 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6696.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Methods/data/filter-iterator-child-class.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/smaller-than-benevolent.php');

if (PHP_VERSION_ID >= 80100) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6695.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6698.php');
}

Expand Down
58 changes: 58 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6695.php
@@ -0,0 +1,58 @@
<?php // lint >= 8.1

namespace Bug6695;

use function PHPStan\Testing\assertType;

enum Foo: int
{
case BAR = 1;
case BAZ = 2;

public function toCollection(): void
{
assertType('Bug6695\Collection<int, Bug6695\Foo>', $this->collect(self::cases()));
}

/**
* Create a collection from the given value.
*
* @template TKey of array-key
* @template TValue
*
* @param iterable<TKey, TValue> $value
* @return Collection<TKey, TValue>
*/
function collect($value): Collection
{
return new Collection($value);
}

}

/**
* @template TKey of array-key
* @template TValue
*
*/
class Collection
{

/**
* The items contained in the collection.
*
* @var iterable<TKey, TValue>
*/
protected $items = [];

/**
* Create a new collection.
*
* @param iterable<TKey, TValue> $items
* @return void
*/
public function __construct($items = [])
{
$this->items = $items;
}
}

0 comments on commit 9e42896

Please sign in to comment.