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
59 changes: 25 additions & 34 deletions src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -434,16 +434,7 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
return new IntegerRange((string) $type->genericTypes[0], (string) $type->genericTypes[1]);

case 'iterable':
return new Iterable_(
...array_reverse(
array_map(
function (TypeNode $genericType) use ($context): Type {
return $this->createType($genericType, $context);
},
$type->genericTypes
)
)
);
return new Iterable_(...array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context)));

case 'key-of':
return new KeyOf($this->createType($type->genericTypes[0], $context));
Expand All @@ -452,18 +443,17 @@ function (TypeNode $genericType) use ($context): Type {
return new ValueOf($this->createType($type->genericTypes[0], $context));

case 'int-mask':
return new IntMask(
...array_map(
function (TypeNode $genericType) use ($context): Type {
return $this->createType($genericType, $context);
},
$type->genericTypes
)
);
return new IntMask(...$this->createTypesByTypeNodes($type->genericTypes, $context));

case 'int-mask-of':
return new IntMaskOf($this->createType($type->genericTypes[0], $context));

case 'static':
return new Static_(...$this->createTypesByTypeNodes($type->genericTypes, $context));

case 'self':
return new Self_(...$this->createTypesByTypeNodes($type->genericTypes, $context));

default:
$collectionType = $this->createType($type->type, $context);
if ($collectionType instanceof Object_ === false) {
Expand All @@ -472,14 +462,7 @@ function (TypeNode $genericType) use ($context): Type {

return new Collection(
$collectionType->getFqsen(),
...array_reverse(
array_map(
function (TypeNode $genericType) use ($context): Type {
return $this->createType($genericType, $context);
},
$type->genericTypes
)
)
...array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context))
);
}
}
Expand Down Expand Up @@ -645,14 +628,7 @@ private function resolveTypedObject(string $type, ?Context $context = null): Obj
/** @param TypeNode[] $typeNodes */
private function createArray(array $typeNodes, Context $context): Array_
{
$types = array_reverse(
array_map(
function (TypeNode $node) use ($context): Type {
return $this->createType($node, $context);
},
$typeNodes
)
);
$types = array_reverse($this->createTypesByTypeNodes($typeNodes, $context));

if (isset($types[1]) === false) {
return new Array_(...$types);
Expand Down Expand Up @@ -727,4 +703,19 @@ private function tryParseRemainingCompoundTypes(TokenIterator $tokenIterator, Co

return $type;
}

/**
* @param TypeNode[] $nodes
*
* @return Type[]
*/
private function createTypesByTypeNodes(array $nodes, Context $context): array
{
return array_map(
function (TypeNode $node) use ($context): Type {
return $this->createType($node, $context);
},
$nodes
);
}
}
22 changes: 22 additions & 0 deletions src/Types/Self_.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use phpDocumentor\Reflection\Type;

use function implode;

/**
* Value Object representing the 'self' type.
*
Expand All @@ -24,11 +26,31 @@
*/
final class Self_ implements Type
{
/** @var Type[] */
private $genericTypes;

public function __construct(Type ...$genericTypes)
{
$this->genericTypes = $genericTypes;
}

/**
* @return Type[]
*/
public function getGenericTypes(): array
{
return $this->genericTypes;
}

/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
public function __toString(): string
{
if ($this->genericTypes) {
return 'self<' . implode(', ', $this->genericTypes) . '>';
}

return 'self';
}
}
22 changes: 22 additions & 0 deletions src/Types/Static_.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

use phpDocumentor\Reflection\Type;

use function implode;

/**
* Value Object representing the 'static' type.
*
Expand All @@ -29,11 +31,31 @@
*/
final class Static_ implements Type
{
/** @var Type[] */
private $genericTypes;

public function __construct(Type ...$genericTypes)
{
$this->genericTypes = $genericTypes;
}

/**
* @return Type[]
*/
public function getGenericTypes(): array
{
return $this->genericTypes;
}

/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
public function __toString(): string
{
if ($this->genericTypes) {
return 'static<' . implode(', ', $this->genericTypes) . '>';
}

return 'static';
}
}
24 changes: 24 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,10 @@ public function typeProvider(): array
new Integer()
),
],
[
'static',
new Static_(),
],
[
'self',
new Self_(),
Expand Down Expand Up @@ -1109,6 +1113,26 @@ public function genericsProvider(): array
'int-mask-of<Foo::INT_*>',
new IntMaskOf(new ConstExpression(new Object_(new Fqsen('\\phpDocumentor\\Foo')), 'INT_*')),
],
[
'iterable<int, string>',
new Iterable_(new String_(), new Integer()),
],
[
'static<FirstClass, SecondClass, ThirdClass>',
new Static_(
new Object_(new Fqsen('\\phpDocumentor\\FirstClass')),
new Object_(new Fqsen('\\phpDocumentor\\SecondClass')),
new Object_(new Fqsen('\\phpDocumentor\\ThirdClass')),
),
],
[
'self<FirstClass, SecondClass, ThirdClass>',
new Self_(
new Object_(new Fqsen('\\phpDocumentor\\FirstClass')),
new Object_(new Fqsen('\\phpDocumentor\\SecondClass')),
new Object_(new Fqsen('\\phpDocumentor\\ThirdClass')),
),
],
];
}

Expand Down
60 changes: 60 additions & 0 deletions tests/unit/Types/SelfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Reflection\Types;

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

/**
* @coversDefaultClass \phpDocumentor\Reflection\Types\Self_
*/
class SelfTest extends TestCase
{
/**
* @covers ::getGenericTypes
*/
public function testCreate(): void
{
$genericTypes = [
new Object_(new Fqsen('\\phpDocumentor\\FirstClass')),
new Object_(new Fqsen('\\phpDocumentor\\SecondClass')),
new Object_(new Fqsen('\\phpDocumentor\\ThirdClass')),
];

$type = new Self_(...$genericTypes);

$this->assertSame($genericTypes, $type->getGenericTypes());
}

/**
* @dataProvider provideToStringData
* @covers ::__toString
*/
public function testToString(string $expectedResult, Self_ $type): void
{
$this->assertSame($expectedResult, (string) $type);
}

/**
* @return array<string, array{string, OffsetAccess}>
*/
public static function provideToStringData(): array
{
return [
'basic' => [
'self',
new Self_(),
],
'with generic' => [
'self<\\phpDocumentor\\FirstClass, \\phpDocumentor\\SecondClass, \\phpDocumentor\\ThirdClass>',
new Self_(
new Object_(new Fqsen('\\phpDocumentor\\FirstClass')),
new Object_(new Fqsen('\\phpDocumentor\\SecondClass')),
new Object_(new Fqsen('\\phpDocumentor\\ThirdClass')),
),
],
];
}
}
60 changes: 60 additions & 0 deletions tests/unit/Types/StaticTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Reflection\Types;

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

/**
* @coversDefaultClass \phpDocumentor\Reflection\Types\Static_
*/
class StaticTest extends TestCase
{
/**
* @covers ::getGenericTypes
*/
public function testCreate(): void
{
$genericTypes = [
new Object_(new Fqsen('\\phpDocumentor\\FirstClass')),
new Object_(new Fqsen('\\phpDocumentor\\SecondClass')),
new Object_(new Fqsen('\\phpDocumentor\\ThirdClass')),
];

$type = new Static_(...$genericTypes);

$this->assertSame($genericTypes, $type->getGenericTypes());
}

/**
* @dataProvider provideToStringData
* @covers ::__toString
*/
public function testToString(string $expectedResult, Static_ $type): void
{
$this->assertSame($expectedResult, (string) $type);
}

/**
* @return array<string, array{string, Static_}>
*/
public static function provideToStringData(): array
{
return [
'basic' => [
'static',
new Static_(),
],
'with generic' => [
'static<\\phpDocumentor\\FirstClass, \\phpDocumentor\\SecondClass, \\phpDocumentor\\ThirdClass>',
new Static_(
new Object_(new Fqsen('\\phpDocumentor\\FirstClass')),
new Object_(new Fqsen('\\phpDocumentor\\SecondClass')),
new Object_(new Fqsen('\\phpDocumentor\\ThirdClass')),
),
],
];
}
}