Skip to content
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
6 changes: 4 additions & 2 deletions src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,11 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
case 'class-string':
case 'interface-string':
case 'trait-string':
case 'enum-string':
return new ClassStringType();

case 'enum-string':
return new GenericClassStringType(new ObjectType('UnitEnum'));

case 'callable-string':
return new IntersectionType([new StringType(), new CallableType()]);

Expand Down Expand Up @@ -696,7 +698,7 @@ static function (string $variance): TemplateTypeVariance {
if (count($genericTypes) === 2) { // iterable<KeyType, ValueType>
return new IterableType($genericTypes[0], $genericTypes[1]);
}
} elseif (in_array($mainTypeName, ['class-string', 'interface-string'], true)) {
} elseif (in_array($mainTypeName, ['class-string', 'interface-string', 'enum-string'], true)) {
Copy link
Contributor Author

@zonuexe zonuexe Feb 5, 2025

Choose a reason for hiding this comment

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

Instead of adding this elseif clause, another idea is to add another clause.

-		} elseif (in_array($mainTypeName, ['class-string', 'interface-string'], true)) {
+		} elseif ($mainTypeName === 'enum-string') {
 			if (count($genericTypes) === 1) {
 				$genericType = $genericTypes[0];
-				if ($genericType->isObject()->yes() || $genericType instanceof MixedType) {
+				if ($genericType->isEnum()->yes() || $genericType instanceof MixedType) {
 					return new GenericClassStringType($genericType);
 				}
			}

However, Psalm simply equates the three.

Copy link
Member

Choose a reason for hiding this comment

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

We need to do TypeCombinator::intersect($genericType, new ObjectType(UnitEnum::class)). You can then test it with enum-string<object> and also enum-string<SomeInterface>.

if (count($genericTypes) === 1) {
$genericType = $genericTypes[0];
if ($genericType->isObject()->yes() || $genericType instanceof MixedType) {
Expand Down
40 changes: 40 additions & 0 deletions tests/PHPStan/Analyser/nsrt/more-type-strings-php8.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php // lint >= 8.0

namespace MoreTypeStringsPhp8;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @param interface-string $interfaceString
* @param trait-string $traitString
* @param interface-string<Foo> $genericInterfaceString
* @param trait-string<Foo> $genericTraitString
* @param enum-string<Bar> $genericEnumString
*/
public function doFoo(
string $interfaceString,
string $traitString,
string $genericInterfaceString,
string $genericTraitString,
string $genericEnumString,
): void
{
assertType('class-string', $interfaceString);
assertType('class-string', $traitString);
assertType('class-string<MoreTypeStringsPhp8\Foo>', $genericInterfaceString);
assertType('string', $genericTraitString);
assertType('class-string<MoreTypeStringsPhp8\Bar>', $genericEnumString);
}

}

enum Bar
{

case A;
case B;

}
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/more-types.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function doFoo(
assertType('array&callable(): mixed', $callableArray);
assertType('resource', $closedResource);
assertType('resource', $openResource);
assertType('class-string', $enumString);
assertType('class-string<UnitEnum>', $enumString);
assertType('literal-string&non-empty-string', $nonEmptyLiteralString);
assertType('float|int<min, -1>|int<1, max>|non-falsy-string|true', $nonEmptyScalar);
assertType("0|0.0|''|'0'|false", $emptyScalar);
Expand Down
Loading