Skip to content

Commit fb05a34

Browse files
authored
Fix the conversion of lists to strings (#237)
* Fix the conversion of lists to strings * Fix Psalm errors * Fix CS * Fix CS error
1 parent 69644ac commit fb05a34

File tree

12 files changed

+100
-30
lines changed

12 files changed

+100
-30
lines changed

src/PseudoTypes/List_.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use phpDocumentor\Reflection\Type;
1818
use phpDocumentor\Reflection\Types\Array_;
1919
use phpDocumentor\Reflection\Types\Integer;
20-
use phpDocumentor\Reflection\Types\Mixed_;
2120

2221
/**
2322
* Value Object representing the type 'list'.
@@ -41,7 +40,7 @@ public function __construct(?Type $valueType = null)
4140
*/
4241
public function __toString(): string
4342
{
44-
if ($this->valueType instanceof Mixed_) {
43+
if ($this->valueType === null) {
4544
return 'list';
4645
}
4746

src/PseudoTypes/NonEmptyArray.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use phpDocumentor\Reflection\PseudoType;
1717
use phpDocumentor\Reflection\Type;
1818
use phpDocumentor\Reflection\Types\Array_;
19-
use phpDocumentor\Reflection\Types\Mixed_;
2019

2120
/**
2221
* Value Object representing the type 'non-empty-array'.
@@ -35,12 +34,12 @@ public function underlyingType(): Type
3534
*/
3635
public function __toString(): string
3736
{
38-
if ($this->keyType) {
39-
return 'non-empty-array<' . $this->keyType . ',' . $this->valueType . '>';
37+
if ($this->valueType === null) {
38+
return 'non-empty-array';
4039
}
4140

42-
if ($this->valueType instanceof Mixed_) {
43-
return 'non-empty-array';
41+
if ($this->keyType) {
42+
return 'non-empty-array<' . $this->keyType . ',' . $this->valueType . '>';
4443
}
4544

4645
return 'non-empty-array<' . $this->valueType . '>';

src/PseudoTypes/NonEmptyList.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
use phpDocumentor\Reflection\Type;
1818
use phpDocumentor\Reflection\Types\Array_;
1919
use phpDocumentor\Reflection\Types\Integer;
20-
use phpDocumentor\Reflection\Types\Mixed_;
2120

2221
/**
2322
* Value Object representing the type 'non-empty-list'.
@@ -41,7 +40,7 @@ public function __construct(?Type $valueType = null)
4140
*/
4241
public function __toString(): string
4342
{
44-
if ($this->valueType instanceof Mixed_) {
43+
if ($this->valueType === null) {
4544
return 'non-empty-list';
4645
}
4746

src/Types/AbstractList.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323
abstract class AbstractList implements Type
2424
{
25-
/** @var Type */
25+
/** @var Type|null */
2626
protected $valueType;
2727

2828
/** @var Type|null */
@@ -31,15 +31,15 @@ abstract class AbstractList implements Type
3131
/** @var Type */
3232
protected $defaultKeyType;
3333

34+
/** @var Type */
35+
protected $defaultValueType;
36+
3437
/**
3538
* Initializes this representation of an array with the given Type.
3639
*/
3740
public function __construct(?Type $valueType = null, ?Type $keyType = null)
3841
{
39-
if ($valueType === null) {
40-
$valueType = new Mixed_();
41-
}
42-
42+
$this->defaultValueType = new Mixed_();
4343
$this->valueType = $valueType;
4444
$this->defaultKeyType = new Compound([new String_(), new Integer()]);
4545
$this->keyType = $keyType;
@@ -50,6 +50,11 @@ public function getOriginalKeyType(): ?Type
5050
return $this->keyType;
5151
}
5252

53+
public function getOriginalValueType(): ?Type
54+
{
55+
return $this->valueType;
56+
}
57+
5358
/**
5459
* Returns the type for the keys of this array.
5560
*/
@@ -63,20 +68,20 @@ public function getKeyType(): Type
6368
*/
6469
public function getValueType(): Type
6570
{
66-
return $this->valueType;
71+
return $this->valueType ?? $this->defaultValueType;
6772
}
6873

6974
/**
7075
* Returns a rendered output of the Type as it would be used in a DocBlock.
7176
*/
7277
public function __toString(): string
7378
{
74-
if ($this->keyType) {
75-
return 'array<' . $this->keyType . ',' . $this->valueType . '>';
79+
if ($this->valueType === null) {
80+
return 'array';
7681
}
7782

78-
if ($this->valueType instanceof Mixed_) {
79-
return 'array';
83+
if ($this->keyType) {
84+
return 'array<' . $this->keyType . ',' . $this->valueType . '>';
8085
}
8186

8287
if ($this->valueType instanceof Compound) {

src/Types/Collection.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ public function getFqsen(): ?Fqsen
5858
public function __toString(): string
5959
{
6060
$objectType = (string) ($this->fqsen ?? 'object');
61+
$valueType = $this->getValueType();
6162

6263
if ($this->keyType === null) {
63-
return $objectType . '<' . $this->valueType . '>';
64+
return $objectType . '<' . $valueType . '>';
6465
}
6566

66-
return $objectType . '<' . $this->keyType . ',' . $this->valueType . '>';
67+
return $objectType . '<' . $this->keyType . ',' . $valueType . '>';
6768
}
6869
}

src/Types/Iterable_.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ final class Iterable_ extends AbstractList
2525
*/
2626
public function __toString(): string
2727
{
28-
if ($this->keyType) {
29-
return 'iterable<' . $this->keyType . ',' . $this->valueType . '>';
28+
if ($this->valueType === null) {
29+
return 'iterable';
3030
}
3131

32-
if ($this->valueType instanceof Mixed_) {
33-
return 'iterable';
32+
if ($this->keyType) {
33+
return 'iterable<' . $this->keyType . ',' . $this->valueType . '>';
3434
}
3535

3636
return 'iterable<' . $this->valueType . '>';

tests/unit/PseudoTypes/ListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function provideArrays(): array
4040
{
4141
return [
4242
'simple list' => [new List_(), 'list'],
43-
'list of mixed' => [new List_(new Mixed_()), 'list'],
43+
'list of mixed' => [new List_(new Mixed_()), 'list<mixed>'],
4444
'list of single type' => [new List_(new String_()), 'list<string>'],
4545
'list of compound type' => [new List_(new Compound([new Integer(), new String_()])), 'list<int|string>'],
4646
];

tests/unit/PseudoTypes/NonEmptyArrayTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function provideArrays(): array
4040
{
4141
return [
4242
'simple non-empty-array' => [new NonEmptyArray(), 'non-empty-array'],
43-
'non-empty-array of mixed' => [new NonEmptyArray(new Mixed_()), 'non-empty-array'],
43+
'non-empty-array of mixed' => [new NonEmptyArray(new Mixed_()), 'non-empty-array<mixed>'],
4444
'non-empty-array of single type' => [new NonEmptyArray(new String_()), 'non-empty-array<string>'],
4545
'non-empty-array of compound type' =>
4646
[

tests/unit/PseudoTypes/NonEmptyListTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function provideArrays(): array
4040
{
4141
return [
4242
'simple non-empty-list' => [new NonEmptyList(), 'non-empty-list'],
43-
'non-empty-list of mixed' => [new NonEmptyList(new Mixed_()), 'non-empty-list'],
43+
'non-empty-list of mixed' => [new NonEmptyList(new Mixed_()), 'non-empty-list<mixed>'],
4444
'non-empty-list of single type' => [new NonEmptyList(new String_()), 'non-empty-list<string>'],
4545
'non-empty-list of compound type' =>
4646
[new NonEmptyList(new Compound([new Integer(), new String_()])), 'non-empty-list<int|string>'],

tests/unit/TypeResolverTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -982,12 +982,22 @@ public function typeProvider(): array
982982
),
983983
]),
984984
],
985+
[
986+
'array',
987+
new Array_(),
988+
],
985989
[
986990
'string[]',
987991
new Array_(
988992
new String_()
989993
),
990994
],
995+
[
996+
'mixed[]',
997+
new Array_(
998+
new Mixed_()
999+
),
1000+
],
9911001
[
9921002
'$this',
9931003
new This(),
@@ -1178,6 +1188,22 @@ public function genericsProvider(): array
11781188
new Object_(new Fqsen('\\phpDocumentor\\ThirdClass')),
11791189
),
11801190
],
1191+
[
1192+
'array<mixed>',
1193+
new Array_(new Mixed_()),
1194+
],
1195+
[
1196+
'iterable<mixed>',
1197+
new Iterable_(new Mixed_()),
1198+
],
1199+
[
1200+
'non-empty-array<mixed>',
1201+
new NonEmptyArray(new Mixed_()),
1202+
],
1203+
[
1204+
'non-empty-list<mixed>',
1205+
new NonEmptyList(new Mixed_()),
1206+
],
11811207
];
11821208
}
11831209

0 commit comments

Comments
 (0)