Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: isEnum class-string narrowing #4474

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

Draft
SamMousa wants to merge 4 commits into phpstan:2.1.x
base: 2.1.x
Choose a base branch
Loading
from SamMousa:feat-improve-reflectionstub
Draft
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
11 changes: 11 additions & 0 deletions stubs/ReflectionClass.stub
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,15 @@ class ReflectionClass
public function getAttributes(?string $name = null, int $flags = 0)
{
}

/**
* @phpstan-assert-if-true class-string<\UnitEnum> $this->name
* @phpstan-assert-if-true class-string<\UnitEnum> $this->getName()
* @phpstan-assert-if-true ReflectionClass<UnitEnum> $this
Copy link
Author

@SamMousa SamMousa Oct 27, 2025
edited
Loading

Choose a reason for hiding this comment

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

This assertion does not work currently. Is there anyway to narrow the type T for a generic?

I've tried using @phsptan-assert-if-true UnitEnum T, but that doesn't work.

* @return (T is \UnitEnum ? true: false)
*/
public function isEnum(): bool
{
}

}
10 changes: 10 additions & 0 deletions stubs/ReflectionClassWithLazyObjects.stub
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,14 @@ class ReflectionClass
public function isUninitializedLazyObject(object $object): bool
{
}

/**
* @phpstan-assert-if-true class-string<\UnitEnum> $this->name
* @phpstan-assert-if-true class-string<\UnitEnum> $this->getName()
* @phpstan-assert-if-true ReflectionClass<UnitEnum> $this
* @return (T is \UnitEnum ? true: false)
*/
public function isEnum(): bool
{
}
}
2 changes: 0 additions & 2 deletions tests/PHPStan/Analyser/data/enum-reflection-php81.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace EnumReflection81;

use ReflectionEnum;
use ReflectionEnumBackedCase;
use ReflectionEnumUnitCase;
use function PHPStan\Testing\assertType;

enum Foo: int
Expand Down
38 changes: 38 additions & 0 deletions tests/PHPStan/Analyser/nsrt/reflectionclass-isEnum.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php // lint >= 8.1

namespace ReflectionClassIsEnum;

use ReflectionClass;
use function PHPStan\Testing\assertType;

/**
* @param class-string $class
*/
function testNarrowClassAfterIsEnum(string $class): void {
$r = new ReflectionClass($class);
if ($r->isEnum()) {
assertType('class-string<UnitEnum>', $r->name);
assertType('class-string<UnitEnum>', $r->getName());

assertType('UnitEnum', $r->newInstance());


// Todo:
//assertType('ReflectionClass<UnitEnum>', $r);

}



}

function testTemplateAssertions(): void {
$enumR = new ReflectionClass(Foo::class);
assertType('ReflectionClass<ReflectionClassIsEnum\\Foo>', $enumR);
assertType('true', $enumR->isEnum());
}


enum Foo {
case Bar;
}
Loading

AltStyle によって変換されたページ (->オリジナル) /