Skip to content

Commit a402408

Browse files
committed
Improve the conversion of arrays to strings
1 parent 936bce1 commit a402408

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/Types/AbstractList.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515

1616
use phpDocumentor\Reflection\Type;
1717

18+
use function preg_match;
19+
use function substr;
20+
1821
/**
1922
* Represents a list of values. This is an abstract class for Array_ and List_.
2023
*
@@ -84,10 +87,13 @@ public function __toString(): string
8487
return 'array<' . $this->keyType . ',' . $this->valueType . '>';
8588
}
8689

87-
if ($this->valueType instanceof Compound) {
88-
return '(' . $this->valueType . ')[]';
90+
if (
91+
!preg_match('/[^\w\\\\]/', (string) $this->valueType) ||
92+
substr((string) $this->valueType, -2, 2) === '[]'
93+
) {
94+
return $this->valueType . '[]';
8995
}
9096

91-
return $this->valueType . '[]';
97+
return 'array<' . $this->valueType . '>';
9298
}
9399
}

tests/unit/TypeResolverTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public function testResolvingArrayExpressionObjectsTypes(): void
376376
$resolvedType = $fixture->resolve('(\stdClass|Reflection\DocBlock)[]', new Context('phpDocumentor'));
377377

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

381381
$valueType = $resolvedType->getValueType();
382382

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

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

410410
$valueType = $resolvedType->getValueType();
411411

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

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

442442
$parentArrayType = $resolvedType->getValueType();
443443
$this->assertInstanceOf(Array_::class, $parentArrayType);
@@ -483,7 +483,7 @@ public function testResolvingArrayExpressionOrCompoundTypes(): void
483483
$resolvedType = $fixture->resolve('\stdClass|(string|\stdClass)[]|bool', new Context(''));
484484

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

488488
$firstType = $resolvedType->get(0);
489489
$this->assertInstanceOf(Object_::class, $firstType);

tests/unit/Types/ArrayTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
namespace phpDocumentor\Reflection\Types;
1515

1616
use phpDocumentor\Reflection\Fqsen;
17+
use phpDocumentor\Reflection\PseudoTypes\ArrayShape;
18+
use phpDocumentor\Reflection\PseudoTypes\ArrayShapeItem;
19+
use phpDocumentor\Reflection\PseudoTypes\ObjectShape;
20+
use phpDocumentor\Reflection\PseudoTypes\ObjectShapeItem;
1721
use PHPUnit\Framework\TestCase;
1822

1923
final class ArrayTest extends TestCase
@@ -63,8 +67,27 @@ public function provideToStringData(): array
6367
'simple array' => [new Array_(), 'array'],
6468
'array of mixed' => [new Array_(new Mixed_()), 'mixed[]'],
6569
'array of single type' => [new Array_(new String_()), 'string[]'],
66-
'array of compound type' => [new Array_(new Compound([new Integer(), new String_()])), '(int|string)[]'],
70+
'multidimensional array' => [new Array_(new Array_(new String_())), 'string[][]'],
71+
'array of compound type' => [new Array_(new Compound([new Integer(), new String_()])), 'array<int|string>'],
6772
'array with key type' => [new Array_(new String_(), new Integer()), 'array<int,string>'],
73+
'array of array shapes' => [
74+
new Array_(
75+
new ArrayShape(
76+
new ArrayShapeItem('foo', new String_(), false),
77+
new ArrayShapeItem('bar', new Integer(), false)
78+
)
79+
),
80+
'array<array{foo: string, bar: int}>',
81+
],
82+
'array of object shapes' => [
83+
new Array_(
84+
new ObjectShape(
85+
new ObjectShapeItem('foo', new String_(), false),
86+
new ObjectShapeItem('bar', new Integer(), false)
87+
)
88+
),
89+
'array<object{foo: string, bar: int}>',
90+
],
6891
];
6992
}
7093
}

0 commit comments

Comments
 (0)