Skip to content
Open
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
25 changes: 5 additions & 20 deletions src/Types/AbstractList.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public function __construct(?Type $valueType = null, ?Type $keyType = null)
$this->keyType = $keyType;
}

/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
abstract public function __toString(): string;

public function getOriginalKeyType(): ?Type
{
return $this->keyType;
Expand All @@ -70,24 +75,4 @@ public function getValueType(): Type
{
return $this->valueType ?? $this->defaultValueType;
}

/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
public function __toString(): string
{
if ($this->valueType === null) {
return 'array';
}

if ($this->keyType) {
return 'array<' . $this->keyType . ', ' . $this->valueType . '>';
}

if ($this->valueType instanceof Compound) {
return '(' . $this->valueType . ')[]';
}

return $this->valueType . '[]';
}
}
21 changes: 21 additions & 0 deletions src/Types/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace phpDocumentor\Reflection\Types;

use function preg_match;
use function substr;

/**
* Represents an array type as described in the PSR-5, the PHPDoc Standard.
*
Expand All @@ -26,4 +29,22 @@
*/
class Array_ extends AbstractList
{
public function __toString(): string
{
if ($this->valueType === null) {
return 'array';
}

$valueTypeString = (string) $this->valueType;

if ($this->keyType) {
return 'array<' . $this->keyType . ', ' . $valueTypeString . '>';
}

if (!preg_match('/[^\w\\\\]/', $valueTypeString) || substr($valueTypeString, -2, 2) === '[]') {
return $valueTypeString . '[]';
}

return 'array<' . $valueTypeString . '>';
}
}
8 changes: 4 additions & 4 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public function testResolvingArrayExpressionObjectsTypes(): void
$resolvedType = $fixture->resolve('(\stdClass|Reflection\DocBlock)[]', new Context('phpDocumentor'));

$this->assertInstanceOf(Array_::class, $resolvedType);
$this->assertSame('(\stdClass|\phpDocumentor\Reflection\DocBlock)[]', (string) $resolvedType);
$this->assertSame('array<\stdClass|\phpDocumentor\Reflection\DocBlock>', (string) $resolvedType);

$valueType = $resolvedType->getValueType();

Expand Down Expand Up @@ -408,7 +408,7 @@ public function testResolvingArrayExpressionSimpleTypes(): void
$resolvedType = $fixture->resolve('(string|\stdClass|boolean)[]', new Context(''));

$this->assertInstanceOf(Array_::class, $resolvedType);
$this->assertSame('(string|\stdClass|bool)[]', (string) $resolvedType);
$this->assertSame('array<string|\stdClass|bool>', (string) $resolvedType);

$valueType = $resolvedType->getValueType();

Expand Down Expand Up @@ -440,7 +440,7 @@ public function testResolvingArrayOfArrayExpressionTypes(): void
$resolvedType = $fixture->resolve('(string|\stdClass)[][]', new Context(''));

$this->assertInstanceOf(Array_::class, $resolvedType);
$this->assertSame('(string|\stdClass)[][]', (string) $resolvedType);
$this->assertSame('array<array<string|\stdClass>>', (string) $resolvedType);

$parentArrayType = $resolvedType->getValueType();
$this->assertInstanceOf(Array_::class, $parentArrayType);
Expand Down Expand Up @@ -486,7 +486,7 @@ public function testResolvingArrayExpressionOrCompoundTypes(): void
$resolvedType = $fixture->resolve('\stdClass|(string|\stdClass)[]|bool', new Context(''));

$this->assertInstanceOf(Compound::class, $resolvedType);
$this->assertSame('\stdClass|(string|\stdClass)[]|bool', (string) $resolvedType);
$this->assertSame('\stdClass|array<string|\stdClass>|bool', (string) $resolvedType);

$firstType = $resolvedType->get(0);
$this->assertInstanceOf(Object_::class, $firstType);
Expand Down
25 changes: 24 additions & 1 deletion tests/unit/Types/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
namespace phpDocumentor\Reflection\Types;

use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\PseudoTypes\ArrayShape;
use phpDocumentor\Reflection\PseudoTypes\ArrayShapeItem;
use phpDocumentor\Reflection\PseudoTypes\ObjectShape;
use phpDocumentor\Reflection\PseudoTypes\ObjectShapeItem;
use PHPUnit\Framework\TestCase;

final class ArrayTest extends TestCase
Expand Down Expand Up @@ -63,8 +67,27 @@ public function provideToStringData(): array
'simple array' => [new Array_(), 'array'],
'array of mixed' => [new Array_(new Mixed_()), 'mixed[]'],
'array of single type' => [new Array_(new String_()), 'string[]'],
'array of compound type' => [new Array_(new Compound([new Integer(), new String_()])), '(int|string)[]'],
'multidimensional array' => [new Array_(new Array_(new String_())), 'string[][]'],
'array of compound type' => [new Array_(new Compound([new Integer(), new String_()])), 'array<int|string>'],
'array with key type' => [new Array_(new String_(), new Integer()), 'array<int, string>'],
'array of array shapes' => [
new Array_(
new ArrayShape(
new ArrayShapeItem('foo', new String_(), false),
new ArrayShapeItem('bar', new Integer(), false)
)
),
'array<array{foo: string, bar: int}>',
],
'array of object shapes' => [
new Array_(
new ObjectShape(
new ObjectShapeItem('foo', new String_(), false),
new ObjectShapeItem('bar', new Integer(), false)
)
),
'array<object{foo: string, bar: int}>',
],
];
}
}
5 changes: 5 additions & 0 deletions tests/unit/Types/ContextFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,5 +262,10 @@ public function assertNamespaceAliasesFrom(Context $context): void
class Foo extends AbstractList
{
// dummy class

public function __toString(): string
{
return '';
}
}
}