Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/Type/Php/ClassExistsFunctionTypeSpecifyingExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\FunctionTypeSpecifyingExtension;
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\ObjectType;
use function in_array;
use function ltrim;

Expand All @@ -41,7 +43,6 @@ public function isFunctionSupported(
public function specifyTypes(FunctionReflection $functionReflection, FuncCall $node, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes
{
$argType = $scope->getType($node->getArgs()[0]->value);
$classStringType = new ClassStringType();
if ($argType instanceof ConstantStringType) {
return $this->typeSpecifier->create(
new FuncCall(new FullyQualified('class_exists'), [
Expand All @@ -54,9 +55,14 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
);
}

$narrowedType = new ClassStringType();
if ($functionReflection->getName() === 'enum_exists') {
$narrowedType = new GenericClassStringType(new ObjectType('UnitEnum'));
}

return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
$classStringType,
$narrowedType,
$context,
false,
$scope,
Expand Down
1 change: 1 addition & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1372,6 +1372,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9963.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/str-shuffle.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9995.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/enum_exists.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9867.php');
}

Expand Down
28 changes: 28 additions & 0 deletions tests/PHPStan/Analyser/data/enum_exists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace EnumExists;

use function PHPStan\Testing\assertType;

function getEnumValue(string $enumFqcn, string $name): mixed {
if (enum_exists($enumFqcn)) {
assertType('class-string<UnitEnum>', $enumFqcn);
return (new \ReflectionEnum($enumFqcn))->getCase($name)->getValue();
}
assertType('string', $enumFqcn);

return null;
}

/**
* @param class-string $enumFqcn
*/
function getEnumValueFromClassString(string $enumFqcn, string $name): mixed {
if (enum_exists($enumFqcn)) {
assertType('class-string<UnitEnum>', $enumFqcn);
return (new \ReflectionEnum($enumFqcn))->getCase($name)->getValue();
}
assertType('class-string', $enumFqcn);

return null;
}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/data/generic-enum-class-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function testEnumExists(string $str)
{
assertType('string', $str);
if (enum_exists($str)) {
assertType('class-string', $str);
assertType('class-string<UnitEnum>', $str);
}
}