Skip to content

Added support for class-string and class-string<Type> types #90

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

Merged
merged 2 commits into from
Feb 2, 2020
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
38 changes: 37 additions & 1 deletion src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use ArrayIterator;
use InvalidArgumentException;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\ClassString;
use phpDocumentor\Reflection\Types\Collection;
use phpDocumentor\Reflection\Types\Compound;
use phpDocumentor\Reflection\Types\Context;
Expand Down Expand Up @@ -66,6 +67,7 @@ final class TypeResolver
*/
private $keywords = [
'string' => Types\String_::class,
'class-string' => Types\ClassString::class,
'int' => Types\Integer::class,
'integer' => Types\Integer::class,
'bool' => Types\Boolean::class,
Expand Down Expand Up @@ -221,7 +223,11 @@ private function parseTypes(ArrayIterator $tokens, Context $context, int $parser

$classType = array_pop($types);
if ($classType !== null) {
$types[] = $this->resolveCollection($tokens, $classType, $context);
if ((string)$classType === 'class-string') {
$types[] = $this->resolveClassString($tokens, $context);
} else {
$types[] = $this->resolveCollection($tokens, $classType, $context);
}
}

$tokens->next();
Expand Down Expand Up @@ -386,6 +392,36 @@ private function resolveTypedObject(string $type, ?Context $context = null) : Ob
return new Object_($this->fqsenResolver->resolve($type, $context));
}

/**
* Resolves class string
*/
private function resolveClassString(ArrayIterator $tokens, Context $context) : Type
{
$tokens->next();

$classType = $this->parseTypes($tokens, $context, self::PARSER_IN_COLLECTION_EXPRESSION);

if (!$classType instanceof Object_ || $classType->getFqsen() === null) {
throw new RuntimeException(
$classType . ' is not a class string'
);
}

if ($tokens->current() !== '>') {
if (empty($tokens->current())) {
throw new RuntimeException(
'class-string: ">" is missing'
);
}

throw new RuntimeException(
'Unexpected character "' . $tokens->current() . '", ">" is missing'
);
}

return new ClassString($classType->getFqsen());
}

/**
* Resolves the collection values and keys
*
Expand Down
54 changes: 54 additions & 0 deletions src/Types/ClassString.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\Types;

use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Type;

/**
* Value Object representing the type 'string'.
*/
final class ClassString implements Type
{
/** @var Fqsen|null */
private $fqsen;

/**
* Initializes this representation of a class string with the given Fqsen.
*/
public function __construct(?Fqsen $fqsen = null)
{
$this->fqsen = $fqsen;
}

/**
* Returns the FQSEN associated with this object.
*/
public function getFqsen() : ?Fqsen
{
return $this->fqsen;
}

/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
public function __toString() : string
{
if ($this->fqsen === null) {
return 'class-string';
} else {
return 'class-string<' . (string)$this->fqsen . '>';
}
}
}
40 changes: 40 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Mockery as m;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Boolean;
use phpDocumentor\Reflection\Types\ClassString;
use phpDocumentor\Reflection\Types\Compound;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\Iterable_;
Expand Down Expand Up @@ -59,6 +60,30 @@ public function testResolvingKeywords(string $keyword, string $expectedClass) :
$this->assertInstanceOf($expectedClass, $resolvedType);
}

/**
* @uses \phpDocumentor\Reflection\Types\Context
* @uses \phpDocumentor\Reflection\Types\Object_
* @uses \phpDocumentor\Reflection\Types\String_
*
* @covers ::__construct
* @covers ::resolve
* @covers ::<private>
*
* @dataProvider provideClassStrings
*/
public function testResolvingClassStrings(string $classString, bool $throwsException) : void
{
$fixture = new TypeResolver();

if ($throwsException) {
$this->expectException('RuntimeException');
}

$resolvedType = $fixture->resolve($classString, new Context(''));

$this->assertInstanceOf(ClassString::class, $resolvedType);
}

/**
* @uses \phpDocumentor\Reflection\Types\Context
* @uses \phpDocumentor\Reflection\Types\Object_
Expand Down Expand Up @@ -634,6 +659,7 @@ public function provideKeywords() : array
{
return [
['string', Types\String_::class],
['class-string', Types\ClassString::class],
['int', Types\Integer::class],
['integer', Types\Integer::class],
['float', Types\Float_::class],
Expand All @@ -657,6 +683,20 @@ public function provideKeywords() : array
];
}

/**
* Returns a list of class string types and whether they throw an exception.
*
* @return (string|bool)[][]
*/
public function provideClassStrings() : array
{
return [
['class-string<\phpDocumentor\Reflection>', false],
['class-string<\phpDocumentor\Reflection\DocBlock>', false],
['class-string<string>', true],
];
}

/**
* Provides a list of FQSENs to test the resolution patterns with.
*
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/Types/ClassStringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\Types;

use phpDocumentor\Reflection\Fqsen;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \phpDocumentor\Reflection\Types\ClassString
*/
class ClassStringTest extends TestCase
{
/**
* @dataProvider provideClassStrings
* @covers ::__toString
*/
public function testClassStringStringifyCorrectly(ClassString $array, string $expectedString) : void
{
$this->assertSame($expectedString, (string) $array);
}

/**
* @return mixed[]
*/
public function provideClassStrings() : array
{
return [
'generic clss string' => [new ClassString(), 'class-string'],
'typed class string' => [new ClassString(new Fqsen('\Foo\Bar')), 'class-string<\Foo\Bar>'],
];
}
}