Skip to content
Merged
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
11 changes: 10 additions & 1 deletion 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 @@ -704,6 +706,13 @@ static function (string $variance): TemplateTypeVariance {
}
}

return new ErrorType();
} elseif ($mainTypeName === 'enum-string') {
if (count($genericTypes) === 1) {
$genericType = $genericTypes[0];
return new GenericClassStringType(TypeCombinator::intersect($genericType, new ObjectType('UnitEnum')));
}

return new ErrorType();
} elseif ($mainTypeName === 'int') {
if (count($genericTypes) === 2) { // int<min, max>, int<1, 3>
Expand Down
48 changes: 48 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,48 @@
<?php // lint >= 8.1

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
* @param enum-string<BuzInterface> $genericInterfaceEnumString
*/
public function doFoo(
string $interfaceString,
string $traitString,
string $genericInterfaceString,
string $genericTraitString,
string $genericEnumString,
string $genericInterfaceEnumString,
): void
{
assertType('class-string', $interfaceString);
assertType('class-string', $traitString);
assertType('class-string<MoreTypeStringsPhp8\Foo>', $genericInterfaceString);
assertType('string', $genericTraitString);
assertType('class-string<MoreTypeStringsPhp8\Bar>', $genericEnumString);
assertType('class-string<MoreTypeStringsPhp8\BuzInterface&UnitEnum>', $genericInterfaceEnumString);
}

}

enum Bar
{

case A;
case B;

}

interface BuzInterface
{

}
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