Skip to content

Commit 520ab7b

Browse files
committed
Replace Collection with GenericType
1 parent fb05a34 commit 520ab7b

File tree

10 files changed

+174
-191
lines changed

10 files changed

+174
-191
lines changed

src/TypeResolver.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@
5656
use phpDocumentor\Reflection\Types\Callable_;
5757
use phpDocumentor\Reflection\Types\CallableParameter;
5858
use phpDocumentor\Reflection\Types\ClassString;
59-
use phpDocumentor\Reflection\Types\Collection;
6059
use phpDocumentor\Reflection\Types\Compound;
6160
use phpDocumentor\Reflection\Types\Context;
6261
use phpDocumentor\Reflection\Types\Expression;
6362
use phpDocumentor\Reflection\Types\Float_;
63+
use phpDocumentor\Reflection\Types\GenericType;
6464
use phpDocumentor\Reflection\Types\Integer;
6565
use phpDocumentor\Reflection\Types\InterfaceString;
6666
use phpDocumentor\Reflection\Types\Intersection;
@@ -460,15 +460,14 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
460460
return new Self_(...$this->createTypesByTypeNodes($type->genericTypes, $context));
461461

462462
default:
463-
$collectionType = $this->createType($type->type, $context);
464-
if ($collectionType instanceof Object_ === false) {
465-
throw new RuntimeException(sprintf('%s is not a collection', (string) $collectionType));
463+
$mainType = $this->createType($type->type, $context);
464+
if ($mainType instanceof Object_ === false) {
465+
throw new RuntimeException(sprintf('%s is an unsupported generic', (string) $mainType));
466466
}
467467

468-
return new Collection(
469-
$collectionType->getFqsen(),
470-
...array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context))
471-
);
468+
$types = $this->createTypesByTypeNodes($type->genericTypes, $context);
469+
470+
return new GenericType($mainType->getFqsen(), $types);
472471
}
473472
}
474473

src/Types/AbstractList.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
use phpDocumentor\Reflection\Type;
1717

1818
/**
19-
* Represents a list of values. This is an abstract class for Array_ and Collection.
19+
* Represents a list of values. This is an abstract class for Array_ and List_.
2020
*
2121
* @psalm-immutable
2222
*/

src/Types/Collection.php

Lines changed: 0 additions & 69 deletions
This file was deleted.

src/Types/GenericType.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link http://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Reflection\Types;
15+
16+
use phpDocumentor\Reflection\Fqsen;
17+
use phpDocumentor\Reflection\Type;
18+
19+
/**
20+
* Value Object representing a type with generics.
21+
*
22+
* @psalm-immutable
23+
*/
24+
final class GenericType implements Type
25+
{
26+
/** @var Fqsen|null */
27+
private $fqsen;
28+
29+
/** @var Type[] */
30+
private $types;
31+
32+
/**
33+
* @param Type[] $types
34+
*/
35+
public function __construct(?Fqsen $fqsen, array $types)
36+
{
37+
$this->fqsen = $fqsen;
38+
$this->types = $types;
39+
}
40+
41+
public function getFqsen(): ?Fqsen
42+
{
43+
return $this->fqsen;
44+
}
45+
46+
/**
47+
* @return Type[]
48+
*/
49+
public function getTypes(): array
50+
{
51+
return $this->types;
52+
}
53+
54+
public function __toString(): string
55+
{
56+
$objectType = (string) ($this->fqsen ?? 'object');
57+
58+
return $objectType . '<' . implode(', ', $this->types) . '>';
59+
}
60+
}

tests/unit/CollectionResolverTest.php

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
use phpDocumentor\Reflection\PseudoTypes\List_;
1717
use phpDocumentor\Reflection\PseudoTypes\NonEmptyList;
1818
use phpDocumentor\Reflection\Types\Array_;
19-
use phpDocumentor\Reflection\Types\Collection;
2019
use phpDocumentor\Reflection\Types\Compound;
2120
use phpDocumentor\Reflection\Types\Context;
2221
use phpDocumentor\Reflection\Types\Float_;
22+
use phpDocumentor\Reflection\Types\GenericType;
2323
use phpDocumentor\Reflection\Types\Integer;
2424
use phpDocumentor\Reflection\Types\Nullable;
2525
use phpDocumentor\Reflection\Types\Object_;
@@ -36,7 +36,7 @@ class CollectionResolverTest extends TestCase
3636
/**
3737
* @uses \phpDocumentor\Reflection\Types\Context
3838
* @uses \phpDocumentor\Reflection\Types\Compound
39-
* @uses \phpDocumentor\Reflection\Types\Collection
39+
* @uses \phpDocumentor\Reflection\Types\GenericType
4040
* @uses \phpDocumentor\Reflection\Types\String_
4141
*
4242
* @covers ::resolve
@@ -49,23 +49,16 @@ public function testResolvingCollection(): void
4949

5050
$resolvedType = $fixture->resolve('ArrayObject<string>', new Context(''));
5151

52-
$this->assertInstanceOf(Collection::class, $resolvedType);
52+
$this->assertInstanceOf(GenericType::class, $resolvedType);
5353
$this->assertSame('\\ArrayObject<string>', (string) $resolvedType);
54-
55-
$this->assertEquals('\\ArrayObject', (string) $resolvedType->getFqsen());
56-
57-
$valueType = $resolvedType->getValueType();
58-
59-
$keyType = $resolvedType->getKeyType();
60-
61-
$this->assertInstanceOf(String_::class, $valueType);
62-
$this->assertInstanceOf(Compound::class, $keyType);
54+
$this->assertSame('\\ArrayObject', (string) $resolvedType->getFqsen());
55+
$this->assertEquals([new String_()], $resolvedType->getTypes());
6356
}
6457

6558
/**
6659
* @uses \phpDocumentor\Reflection\Types\Context
6760
* @uses \phpDocumentor\Reflection\Types\Compound
68-
* @uses \phpDocumentor\Reflection\Types\Collection
61+
* @uses \phpDocumentor\Reflection\Types\GenericType
6962
* @uses \phpDocumentor\Reflection\Types\String_
7063
*
7164
* @covers ::__construct
@@ -78,25 +71,22 @@ public function testResolvingCollectionWithKeyType(): void
7871

7972
$resolvedType = $fixture->resolve('ArrayObject<string[],Iterator>', new Context(''));
8073

81-
$this->assertInstanceOf(Collection::class, $resolvedType);
82-
$this->assertSame('\\ArrayObject<string[],\\Iterator>', (string) $resolvedType);
74+
$this->assertInstanceOf(GenericType::class, $resolvedType);
75+
$this->assertSame('\\ArrayObject<string[], \\Iterator>', (string) $resolvedType);
76+
$this->assertSame('\\ArrayObject', (string) $resolvedType->getFqsen());
8377

84-
$this->assertEquals('\\ArrayObject', (string) $resolvedType->getFqsen());
78+
$types = $resolvedType->getTypes();
8579

86-
$valueType = $resolvedType->getValueType();
87-
88-
$keyType = $resolvedType->getKeyType();
89-
90-
$this->assertInstanceOf(Object_::class, $valueType);
91-
$this->assertEquals('\\Iterator', (string) $valueType->getFqsen());
92-
$this->assertInstanceOf(Array_::class, $keyType);
93-
$this->assertInstanceOf(String_::class, $keyType->getValueType());
80+
$this->assertArrayHasKey(0, $types);
81+
$this->assertEquals(new Array_(new String_()), $types[0]);
82+
$this->assertArrayHasKey(1, $types);
83+
$this->assertInstanceOf(Object_::class, $types[1]);
84+
$this->assertSame('\\Iterator', (string) $types[1]->getFqsen());
9485
}
9586

9687
/**
9788
* @uses \phpDocumentor\Reflection\Types\Context
9889
* @uses \phpDocumentor\Reflection\Types\Compound
99-
* @uses \phpDocumentor\Reflection\Types\Collection
10090
* @uses \phpDocumentor\Reflection\Types\String_
10191
*
10292
* @covers ::__construct
@@ -123,7 +113,6 @@ public function testResolvingArrayCollection(): void
123113
/**
124114
* @uses \phpDocumentor\Reflection\Types\Context
125115
* @uses \phpDocumentor\Reflection\Types\Compound
126-
* @uses \phpDocumentor\Reflection\Types\Collection
127116
* @uses \phpDocumentor\Reflection\Types\String_
128117
*
129118
* @covers ::__construct
@@ -150,7 +139,6 @@ public function testResolvingArrayCollectionWithKey(): void
150139
/**
151140
* @uses \phpDocumentor\Reflection\Types\Context
152141
* @uses \phpDocumentor\Reflection\Types\Compound
153-
* @uses \phpDocumentor\Reflection\Types\Collection
154142
* @uses \phpDocumentor\Reflection\Types\String_
155143
156144
* @covers ::__construct
@@ -177,7 +165,7 @@ public function testResolvingArrayCollectionWithKeyAndWhitespace(): void
177165
/**
178166
* @uses \phpDocumentor\Reflection\Types\Context
179167
* @uses \phpDocumentor\Reflection\Types\Compound
180-
* @uses \phpDocumentor\Reflection\Types\Collection
168+
* @uses \phpDocumentor\Reflection\Types\GenericType
181169
* @uses \phpDocumentor\Reflection\Types\String_
182170
*
183171
* @covers ::__construct
@@ -190,25 +178,22 @@ public function testResolvingCollectionOfCollection(): void
190178

191179
$resolvedType = $fixture->resolve('ArrayObject<string|integer|double,ArrayObject<DateTime>>', new Context(''));
192180

193-
$this->assertInstanceOf(Collection::class, $resolvedType);
194-
$this->assertSame('\\ArrayObject<string|int|float,\\ArrayObject<\\DateTime>>', (string) $resolvedType);
181+
$this->assertInstanceOf(GenericType::class, $resolvedType);
182+
$this->assertSame('\\ArrayObject<string|int|float, \\ArrayObject<\\DateTime>>', (string) $resolvedType);
183+
$this->assertSame('\\ArrayObject', (string) $resolvedType->getFqsen());
195184

196-
$this->assertEquals('\\ArrayObject', (string) $resolvedType->getFqsen());
185+
$types = $resolvedType->getTypes();
197186

198-
$valueType = $resolvedType->getValueType();
199-
$this->assertInstanceOf(Collection::class, $valueType);
200-
$collectionValueType = $valueType->getValueType();
187+
$this->assertArrayHasKey(0, $types);
188+
$this->assertEquals(new Compound([new String_(), new Integer(), new Float_()]), $types[0]);
201189

202-
$this->assertInstanceOf(Object_::class, $valueType->getValueType());
203-
$this->assertEquals('\\ArrayObject', (string) $valueType->getFqsen());
204-
$this->assertInstanceOf(Object_::class, $collectionValueType);
205-
$this->assertEquals('\\DateTime', (string) $collectionValueType->getFqsen());
190+
$this->assertArrayHasKey(0, $types);
191+
$this->assertInstanceOf(GenericType::class, $types[1]);
192+
$this->assertSame('\\ArrayObject', (string) $types[1]->getFqsen());
206193

207-
$keyType = $resolvedType->getKeyType();
208-
$this->assertInstanceOf(Compound::class, $keyType);
209-
$this->assertInstanceOf(String_::class, $keyType->get(0));
210-
$this->assertInstanceOf(Integer::class, $keyType->get(1));
211-
$this->assertInstanceOf(Float_::class, $keyType->get(2));
194+
$nestedGenericTypes = $types[1]->getTypes();
195+
$this->assertArrayHasKey(0, $nestedGenericTypes);
196+
$this->assertEquals(new Object_(new Fqsen('\\DateTime')), $nestedGenericTypes[0]);
212197
}
213198

214199
/**
@@ -278,15 +263,14 @@ public function testMissingEndCollection(): void
278263
public function testBadCollectionClass(): void
279264
{
280265
$this->expectException(RuntimeException::class);
281-
$this->expectExceptionMessage('string is not a collection');
266+
$this->expectExceptionMessage('string is an unsupported generic');
282267
$fixture = new TypeResolver();
283268
$fixture->resolve('string<integer>', new Context(''));
284269
}
285270

286271
/**
287272
* @uses \phpDocumentor\Reflection\Types\Context
288273
* @uses \phpDocumentor\Reflection\Types\Compound
289-
* @uses \phpDocumentor\Reflection\Types\Collection
290274
* @uses \phpDocumentor\Reflection\Types\String_
291275
*
292276
* @covers ::__construct

tests/unit/IntegerRangeResolverTest.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ class IntegerRangeResolverTest extends TestCase
2727
/**
2828
* @uses \phpDocumentor\Reflection\Types\Context
2929
* @uses \phpDocumentor\Reflection\Types\Compound
30-
* @uses \phpDocumentor\Reflection\Types\Collection
3130
* @uses \phpDocumentor\Reflection\Types\String_
3231
*
3332
* @covers ::__construct
@@ -53,7 +52,6 @@ public function testResolvingIntRange(): void
5352
/**
5453
* @uses \phpDocumentor\Reflection\Types\Context
5554
* @uses \phpDocumentor\Reflection\Types\Compound
56-
* @uses \phpDocumentor\Reflection\Types\Collection
5755
* @uses \phpDocumentor\Reflection\Types\String_
5856
*
5957
* @covers ::__construct
@@ -79,7 +77,6 @@ public function testResolvingIntRangeWithKeywords(): void
7977
/**
8078
* @uses \phpDocumentor\Reflection\Types\Context
8179
* @uses \phpDocumentor\Reflection\Types\Compound
82-
* @uses \phpDocumentor\Reflection\Types\Collection
8380
* @uses \phpDocumentor\Reflection\Types\String_
8481
*
8582
* @covers ::__construct
@@ -98,7 +95,6 @@ public function testResolvingIntRangeErrorMissingMaxValue(): void
9895
/**
9996
* @uses \phpDocumentor\Reflection\Types\Context
10097
* @uses \phpDocumentor\Reflection\Types\Compound
101-
* @uses \phpDocumentor\Reflection\Types\Collection
10298
* @uses \phpDocumentor\Reflection\Types\String_
10399
*
104100
* @covers ::__construct
@@ -117,7 +113,6 @@ public function testResolvingIntRangeErrorMisingMinValue(): void
117113
/**
118114
* @uses \phpDocumentor\Reflection\Types\Context
119115
* @uses \phpDocumentor\Reflection\Types\Compound
120-
* @uses \phpDocumentor\Reflection\Types\Collection
121116
* @uses \phpDocumentor\Reflection\Types\String_
122117
*
123118
* @covers ::__construct
@@ -136,7 +131,6 @@ public function testResolvingIntRangeErrorMisingComma(): void
136131
/**
137132
* @uses \phpDocumentor\Reflection\Types\Context
138133
* @uses \phpDocumentor\Reflection\Types\Compound
139-
* @uses \phpDocumentor\Reflection\Types\Collection
140134
* @uses \phpDocumentor\Reflection\Types\String_
141135
*
142136
* @covers ::__construct

tests/unit/NumericResolverTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ class NumericResolverTest extends TestCase
2828
/**
2929
* @uses \phpDocumentor\Reflection\Types\Context
3030
* @uses \phpDocumentor\Reflection\Types\Compound
31-
* @uses \phpDocumentor\Reflection\Types\Collection
3231
* @uses \phpDocumentor\Reflection\Types\String_
3332
*
3433
* @covers ::__construct

0 commit comments

Comments
 (0)