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

Narrow enum_exists() arg to UnitEnum #2764

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 13 additions & 1 deletion 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,6 +55,17 @@ public function specifyTypes(FunctionReflection $functionReflection, FuncCall $n
);
}

if ($functionReflection->getName() === 'enum_exists') {
return $this->typeSpecifier->create(
Copy link
Member

Choose a reason for hiding this comment

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

Please less code duplication here. The only thing different is the type argument.

$node->getArgs()[0]->value,
new GenericClassStringType(new ObjectType('UnitEnum')),
$context,
false,
$scope,
);
}

$classStringType = new ClassStringType();
return $this->typeSpecifier->create(
$node->getArgs()[0]->value,
$classStringType,
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);
}
}

Loading