Skip to content

Commit cbc0615

Browse files
committed
Move the logic of AbstractList::__toString() to Array_::__toString()
1 parent cae9aaa commit cbc0615

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

src/Types/AbstractList.php

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

1616
use phpDocumentor\Reflection\Type;
1717

18-
use function preg_match;
19-
use function substr;
20-
2118
/**
2219
* Represents a list of values. This is an abstract class for Array_ and List_.
2320
*
@@ -48,6 +45,11 @@ public function __construct(?Type $valueType = null, ?Type $keyType = null)
4845
$this->keyType = $keyType;
4946
}
5047

48+
/**
49+
* Returns a rendered output of the Type as it would be used in a DocBlock.
50+
*/
51+
abstract public function __toString(): string;
52+
5153
public function getOriginalKeyType(): ?Type
5254
{
5355
return $this->keyType;
@@ -73,26 +75,4 @@ public function getValueType(): Type
7375
{
7476
return $this->valueType ?? $this->defaultValueType;
7577
}
76-
77-
/**
78-
* Returns a rendered output of the Type as it would be used in a DocBlock.
79-
*/
80-
public function __toString(): string
81-
{
82-
if ($this->valueType === null) {
83-
return 'array';
84-
}
85-
86-
$valueTypeString = (string) $this->valueType;
87-
88-
if ($this->keyType) {
89-
return 'array<' . $this->keyType . ', ' . $valueTypeString . '>';
90-
}
91-
92-
if (!preg_match('/[^\w\\\\]/', $valueTypeString) || substr($valueTypeString, -2, 2) === '[]') {
93-
return $valueTypeString . '[]';
94-
}
95-
96-
return 'array<' . $valueTypeString . '>';
97-
}
9878
}

src/Types/Array_.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace phpDocumentor\Reflection\Types;
1515

16+
use function preg_match;
17+
use function substr;
18+
1619
/**
1720
* Represents an array type as described in the PSR-5, the PHPDoc Standard.
1821
*
@@ -26,4 +29,22 @@
2629
*/
2730
class Array_ extends AbstractList
2831
{
32+
public function __toString(): string
33+
{
34+
if ($this->valueType === null) {
35+
return 'array';
36+
}
37+
38+
$valueTypeString = (string) $this->valueType;
39+
40+
if ($this->keyType) {
41+
return 'array<' . $this->keyType . ', ' . $valueTypeString . '>';
42+
}
43+
44+
if (!preg_match('/[^\w\\\\]/', $valueTypeString) || substr($valueTypeString, -2, 2) === '[]') {
45+
return $valueTypeString . '[]';
46+
}
47+
48+
return 'array<' . $valueTypeString . '>';
49+
}
2950
}

tests/unit/Types/ContextFactoryTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,5 +262,10 @@ public function assertNamespaceAliasesFrom(Context $context): void
262262
class Foo extends AbstractList
263263
{
264264
// dummy class
265+
266+
public function __toString(): string
267+
{
268+
return '';
269+
}
265270
}
266271
}

0 commit comments

Comments
 (0)