Skip to content

Commit

Permalink
Basic support for interface-string and trait-string
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 16, 2021
1 parent b170a4f commit d0ef5d4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/PhpDoc/TypeNodeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ private function resolveIdentifierTypeNode(IdentifierTypeNode $typeNode, NameSco
return new IntersectionType([new StringType(), new AccessoryLiteralStringType()]);

case 'class-string':
case 'interface-string':
case 'trait-string':
return new ClassStringType();

case 'callable-string':
Expand Down Expand Up @@ -480,7 +482,7 @@ private function resolveGenericTypeNode(GenericTypeNode $typeNode, NameScope $na
if (count($genericTypes) === 2) { // iterable<KeyType, ValueType>
return new IterableType($genericTypes[0], $genericTypes[1]);
}
} elseif ($mainTypeName === 'class-string') {
} elseif (in_array($mainTypeName, ['class-string', 'interface-string'], true)) {
if (count($genericTypes) === 1) {
$genericType = $genericTypes[0];
if ((new ObjectWithoutClassType())->isSuperTypeOf($genericType)->yes() || $genericType instanceof MixedType) {
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 @@ -506,6 +506,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/range-numeric-string.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/missing-closure-native-return-typehint.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-4741.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/more-type-strings.php');
}

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

namespace MoreTypeStrings;

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
*/
public function doFoo(
string $interfaceString,
string $traitString,
string $genericInterfaceString,
string $genericTraitString
): void
{
assertType('class-string', $interfaceString);
assertType('class-string', $traitString);
assertType('class-string<MoreTypeStrings\Foo>', $genericInterfaceString);
assertType('string', $genericTraitString);
}

}

0 comments on commit d0ef5d4

Please sign in to comment.