Skip to content

Commit 2dce258

Browse files
committed
Fix the conversion of lists to strings
1 parent 69644ac commit 2dce258

11 files changed

Lines changed: 88 additions & 16 deletions

File tree

src/PseudoTypes/List_.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(?Type $valueType = null)
4141
*/
4242
public function __toString(): string
4343
{
44-
if ($this->valueType instanceof Mixed_) {
44+
if ($this->valueType === null) {
4545
return 'list';
4646
}
4747

src/PseudoTypes/NonEmptyArray.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __toString(): string
3939
return 'non-empty-array<' . $this->keyType . ',' . $this->valueType . '>';
4040
}
4141

42-
if ($this->valueType instanceof Mixed_) {
42+
if ($this->valueType === null) {
4343
return 'non-empty-array';
4444
}
4545

src/PseudoTypes/NonEmptyList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function __construct(?Type $valueType = null)
4141
*/
4242
public function __toString(): string
4343
{
44-
if ($this->valueType instanceof Mixed_) {
44+
if ($this->valueType === null) {
4545
return 'non-empty-list';
4646
}
4747

src/Types/AbstractList.php

Lines changed: 12 additions & 7 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,7 +68,7 @@ public function getKeyType(): Type
6368
*/
6469
public function getValueType(): Type
6570
{
66-
return $this->valueType;
71+
return $this->valueType ?? $this->defaultValueType;
6772
}
6873

6974
/**
@@ -75,7 +80,7 @@ public function __toString(): string
7580
return 'array<' . $this->keyType . ',' . $this->valueType . '>';
7681
}
7782

78-
if ($this->valueType instanceof Mixed_) {
83+
if ($this->valueType === null) {
7984
return 'array';
8085
}
8186

src/Types/Iterable_.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function __toString(): string
2929
return 'iterable<' . $this->keyType . ',' . $this->valueType . '>';
3030
}
3131

32-
if ($this->valueType instanceof Mixed_) {
32+
if ($this->valueType === null) {
3333
return 'iterable';
3434
}
3535

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

tests/unit/Types/ArrayTest.php

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,54 @@
1313

1414
namespace phpDocumentor\Reflection\Types;
1515

16+
use phpDocumentor\Reflection\Fqsen;
1617
use PHPUnit\Framework\TestCase;
1718

1819
/**
1920
* @coversDefaultClass \phpDocumentor\Reflection\Types\Array_
2021
*/
2122
class ArrayTest extends TestCase
2223
{
24+
/**
25+
* @covers ::getOriginalKeyType
26+
* @covers ::getOriginalValueType
27+
* @covers ::getKeyType
28+
* @covers ::getValueType
29+
*/
30+
public function testCreateWithoutParams(): void
31+
{
32+
$type = new Array_();
33+
34+
$this->assertNull($type->getOriginalKeyType());
35+
$this->assertNull($type->getOriginalValueType());
36+
$this->assertEquals(new Compound([new String_(), new Integer()]), $type->getKeyType());
37+
$this->assertEquals(new Mixed_(), $type->getValueType());
38+
}
39+
40+
/**
41+
* @covers ::getOriginalKeyType
42+
* @covers ::getOriginalValueType
43+
* @covers ::getKeyType
44+
* @covers ::getValueType
45+
*/
46+
public function testCreateWithParams(): void
47+
{
48+
$valueType = new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar'));
49+
$keyType = new Compound(
50+
[
51+
new String_(),
52+
new Integer(),
53+
]
54+
);
55+
56+
$type = new Array_($valueType, $keyType);
57+
58+
$this->assertSame($keyType, $type->getOriginalKeyType());
59+
$this->assertSame($valueType, $type->getOriginalValueType());
60+
$this->assertSame($keyType, $type->getKeyType());
61+
$this->assertSame($valueType, $type->getValueType());
62+
}
63+
2364
/**
2465
* @dataProvider provideArrays
2566
* @covers ::__toString
@@ -36,7 +77,7 @@ public function provideArrays(): array
3677
{
3778
return [
3879
'simple array' => [new Array_(), 'array'],
39-
'array of mixed' => [new Array_(new Mixed_()), 'array'],
80+
'array of mixed' => [new Array_(new Mixed_()), 'mixed[]'],
4081
'array of single type' => [new Array_(new String_()), 'string[]'],
4182
'array of compound type' => [new Array_(new Compound([new Integer(), new String_()])), '(int|string)[]'],
4283
'array with key type' => [new Array_(new String_(), new Integer()), 'array<int,string>'],

0 commit comments

Comments
 (0)