Skip to content

Commit

Permalink
Merge branch refs/heads/1.10.x into 1.11.x
Browse files Browse the repository at this point in the history
  • Loading branch information
phpstan-bot committed Nov 25, 2023
2 parents 4da6f27 + de2b200 commit 3c7860c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
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 @@ -1373,6 +1373,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);
}
}

0 comments on commit 3c7860c

Please sign in to comment.