Skip to content
Open
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: 5 additions & 0 deletions src/Reflection/InitializerExprTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2748,6 +2748,11 @@ public function getClassConstFetchTypeByReflection(Name|Expr $class, string $con
unset($this->currentlyResolvingClassConstant[$resolvingName]);
continue;
}
if ($reflectionConstant->isEnumCase()) {
$types[] = new EnumCaseObjectType($constantClassReflection->getName(), $constantName);
unset($this->currentlyResolvingClassConstant[$resolvingName]);
continue;
}
$reflectionConstantDeclaringClass = $reflectionConstant->getDeclaringClass();
$constantType = $this->getType($reflectionConstant->getValueExpression(), InitializerExprContext::fromClass($reflectionConstantDeclaringClass->getName(), $reflectionConstantDeclaringClass->getFileName() ?: null));
$nativeType = null;
Expand Down
52 changes: 52 additions & 0 deletions tests/PHPStan/Reflection/InitializerExprTypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,70 @@
namespace PHPStan\Reflection;

use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Scalar\LNumber;
use PhpParser\Node\Scalar\String_;
use PhpParser\ParserFactory;
use PHPStan\Analyser\ConstantResolver;
use PHPStan\BetterReflection\Reflection\Adapter\ReflectionClass;
use PHPStan\BetterReflection\Reflector\DefaultReflector;
use PHPStan\BetterReflection\SourceLocator\Ast\Locator;
use PHPStan\BetterReflection\SourceLocator\Type\StringSourceLocator;
use PHPStan\Php\PhpVersion;
use PHPStan\Reflection\ReflectionProvider\DirectReflectionProviderProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\Testing\PHPStanTestCase;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\OversizedArrayBuilder;
use PHPStan\Type\NeverType;
use PHPStan\Type\OperatorTypeSpecifyingExtensionRegistry;
use PHPStan\Type\Type;
use PHPStan\Type\UnaryOperatorTypeSpecifyingExtensionRegistry;
use PHPStan\Type\VerbosityLevel;
use PHPUnit\Framework\Attributes\DataProvider;

class InitializerExprTypeResolverTest extends PHPStanTestCase
{

public function testUnitEnumDefaultWithClassAdapter(): void
{
$container = self::getContainer();
// Do not resolve the runtime's RoundingMode polyfill on older PHP versions.
$reflector = new DefaultReflector(new StringSourceLocator(
'<?php enum RoundingMode { case HalfAwayFromZero; }',
new Locator((new ParserFactory())->createForNewestSupportedVersion()),
));
$classReflection = $container->getByType(ClassReflectionFactory::class)->create(
'RoundingMode',
new ReflectionClass($reflector->reflectClass('RoundingMode')),
null,
null,
null,
);

// PHP runtimes without native enum reflection use the generic class adapter.
$reflectionProvider = $this->createMock(ReflectionProvider::class);
$reflectionProvider->method('hasClass')->with('RoundingMode')->willReturn(true);
$reflectionProvider->method('getClass')->with('RoundingMode')->willReturn($classReflection);
$resolver = new InitializerExprTypeResolver(
$container->getByType(ConstantResolver::class),
new DirectReflectionProviderProvider($reflectionProvider),
new PhpVersion(80500),
$container->getByType(OperatorTypeSpecifyingExtensionRegistry::class),
$container->getByType(UnaryOperatorTypeSpecifyingExtensionRegistry::class),
new OversizedArrayBuilder(),
$container->getParameter('usePathConstantsAsConstantString'),
);

$type = $resolver->getType(
new ClassConstFetch(new FullyQualified('RoundingMode'), 'HalfAwayFromZero'),
InitializerExprContext::createEmpty(),
);

$this->assertSame('RoundingMode::HalfAwayFromZero', $type->describe(VerbosityLevel::precise()));
}

public static function dataExplicitNever(): iterable
{
yield [
Expand Down
Loading