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

Added regression test #3062

Merged
merged 2 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1602,11 +1602,6 @@ parameters:
count: 4
path: src/Type/TypeCombinator.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\Enum\\\\EnumCaseObjectType is error\\-prone and deprecated\\. Use Type\\:\\:getEnumCases\\(\\) instead\\.$#"
count: 1
path: src/Type/TypeCombinator.php

-
message: "#^Doing instanceof PHPStan\\\\Type\\\\FloatType is error\\-prone and deprecated\\. Use Type\\:\\:isFloat\\(\\) instead\\.$#"
count: 1
Expand Down
32 changes: 22 additions & 10 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ class ObjectType implements TypeWithClassName, SubtractableType

private ?string $cachedDescription = null;

/** @var array<string, list<EnumCaseObjectType>> */
private static array $enumCases = [];

/** @api */
public function __construct(
private string $className,
Expand All @@ -114,6 +117,7 @@ public static function resetCaches(): void
self::$methods = [];
self::$properties = [];
self::$ancestors = [];
self::$enumCases = [];
}

private static function createFromReflection(ClassReflection $reflection): self
Expand Down Expand Up @@ -1215,23 +1219,31 @@ public function getEnumCases(): array
return [];
}

$subtracted = [];
if ($this->subtractedType !== null) {
foreach ($this->subtractedType->getEnumCases() as $enumCase) {
$subtracted[$enumCase->getEnumCaseName()] = true;
}
$cacheKey = $this->describeCache();
if (array_key_exists($cacheKey, self::$enumCases)) {
return self::$enumCases[$cacheKey];
}

$cases = [];
$className = $classReflection->getName();

$cases = [];
foreach ($classReflection->getEnumCases() as $enumCase) {
if (array_key_exists($enumCase->getName(), $subtracted)) {
continue;
}
$cases[] = new EnumCaseObjectType($className, $enumCase->getName(), $classReflection);
}

return $cases;
if ($this->subtractedType !== null) {
foreach ($cases as $i => $case) {
$caseName = $case->getEnumCaseName();
foreach ($this->subtractedType->getEnumCases() as $subtracedCase) {
if ($caseName === $subtracedCase->getEnumCaseName()) {
unset($cases[$i]);
continue 2;
}
}
}
}

return self::$enumCases[$cacheKey] = array_values($cases);
}

public function isCallable(): TrinaryLogic
Expand Down
4 changes: 2 additions & 2 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use PHPStan\Type\Constant\ConstantFloatType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\Enum\EnumCaseObjectType;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateArrayType;
use PHPStan\Type\Generic\TemplateBenevolentUnionType;
Expand Down Expand Up @@ -201,7 +200,8 @@ public static function union(Type ...$types): Type
if ($types[$i] instanceof StringType && !$types[$i] instanceof ClassStringType) {
$hasGenericScalarTypes[ConstantStringType::class] = true;
}
if ($types[$i] instanceof EnumCaseObjectType) {
$enumCases = $types[$i]->getEnumCases();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverts #2985

if (count($enumCases) === 1) {
$enumCaseTypes[$types[$i]->describe(VerbosityLevel::cache())] = $types[$i];

unset($types[$i]);
Expand Down
10 changes: 10 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,16 @@ public function testBug10772(): void
$this->assertNoErrors($errors);
}

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

$errors = $this->runAnalyse(__DIR__ . '/data/bug-10979.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
Loading
Loading