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

ObjectType: fix isEnum #3915

Merged
merged 5 commits into from
Apr 7, 2025
Merged
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
21 changes: 20 additions & 1 deletion src/Type/ObjectType.php
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
use ArrayObject;
use Closure;
use Countable;
use DateTimeInterface;
use Iterator;
use IteratorAggregate;
use PHPStan\Analyser\OutOfClassScope;
@@ -44,6 +45,8 @@
use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
use PHPStan\Type\Traits\NonGenericTypeTrait;
use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
use Stringable;
use Throwable;
use Traversable;
use function array_key_exists;
use function array_map;
@@ -730,7 +733,23 @@ public function isEnum(): TrinaryLogic
return TrinaryLogic::createMaybe();
}

return TrinaryLogic::createFromBoolean($classReflection->isEnum());
if (
$classReflection->isEnum()
|| $classReflection->is('UnitEnum')
) {
return TrinaryLogic::createYes();
}

if (
$classReflection->isInterface()
&& !$classReflection->is(Stringable::class) // enums cannot have __toString
&& !$classReflection->is(Throwable::class) // enums cannot extend Exception/Error
&& !$classReflection->is(DateTimeInterface::class) // userland classes cannot extend DateTimeInterface
) {
return TrinaryLogic::createMaybe();
}

return TrinaryLogic::createNo();
}

public function canAccessProperties(): TrinaryLogic
29 changes: 29 additions & 0 deletions tests/PHPStan/Type/ObjectTypeTest.php
Original file line number Diff line number Diff line change
@@ -70,6 +70,35 @@ public function testIsIterable(ObjectType $type, TrinaryLogic $expectedResult):
);
}

/**
* @return iterable<array{0: ObjectType, 1: TrinaryLogic}>
*/
public function dataIsEnum(): iterable
{
if (PHP_VERSION_ID >= 80000) {
yield [new ObjectType('UnitEnum'), TrinaryLogic::createYes()];
yield [new ObjectType('BackedEnum'), TrinaryLogic::createYes()];
}
yield [new ObjectType('Unknown'), TrinaryLogic::createMaybe()];
yield [new ObjectType('Countable'), TrinaryLogic::createMaybe()];
yield [new ObjectType('Stringable'), TrinaryLogic::createNo()];
yield [new ObjectType('Throwable'), TrinaryLogic::createNo()];
yield [new ObjectType('DateTime'), TrinaryLogic::createNo()];
}

/**
* @dataProvider dataIsEnum
*/
public function testIsEnum(ObjectType $type, TrinaryLogic $expectedResult): void
{
$actualResult = $type->isEnum();
$this->assertSame(
$expectedResult->describe(),
$actualResult->describe(),
sprintf('%s -> isEnum()', $type->describe(VerbosityLevel::precise())),
);
}

public function dataIsCallable(): array
{
return [