Skip to content

Commit 9049757

Browse files
authored
Replace Collection with GenericType (#238)
* Replace `Collection` with `GenericType` * Add tests * Fix CS * CR Fix * Add `GenericTemplate`
1 parent fb05a34 commit 9049757

File tree

13 files changed

+270
-192
lines changed

13 files changed

+270
-192
lines changed

src/PseudoTypes/Generic.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\PseudoTypes;
15+
16+
use phpDocumentor\Reflection\Fqsen;
17+
use phpDocumentor\Reflection\Type;
18+
use phpDocumentor\Reflection\Types\Object_;
19+
20+
use function implode;
21+
22+
/**
23+
* Value Object representing a type with generics.
24+
*
25+
* @psalm-immutable
26+
*/
27+
final class Generic extends Object_
28+
{
29+
/** @var Type[] */
30+
private $types;
31+
32+
/**
33+
* @param Type[] $types
34+
*/
35+
public function __construct(?Fqsen $fqsen, array $types)
36+
{
37+
parent::__construct($fqsen);
38+
39+
$this->types = $types;
40+
}
41+
42+
/**
43+
* @return Type[]
44+
*/
45+
public function getTypes(): array
46+
{
47+
return $this->types;
48+
}
49+
50+
public function __toString(): string
51+
{
52+
$objectType = (string) ($this->fqsen ?? 'object');
53+
54+
return $objectType . '<' . implode(', ', $this->types) . '>';
55+
}
56+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\PseudoTypes;
15+
16+
use phpDocumentor\Reflection\Type;
17+
use phpDocumentor\Reflection\Types\Object_;
18+
19+
/**
20+
* Value Object representing the wrapper over the generic.
21+
*
22+
* @psalm-immutable
23+
*/
24+
final class GenericTemplate implements Type
25+
{
26+
/** @var Object_ */
27+
private $resolvedType;
28+
29+
public function __construct(Object_ $resolvedType)
30+
{
31+
$this->resolvedType = $resolvedType;
32+
}
33+
34+
public function getResolvedType(): Object_
35+
{
36+
return $this->resolvedType;
37+
}
38+
39+
public function __toString(): string
40+
{
41+
return (string) $this->resolvedType;
42+
}
43+
}

src/TypeResolver.php

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use phpDocumentor\Reflection\PseudoTypes\ConstExpression;
2424
use phpDocumentor\Reflection\PseudoTypes\False_;
2525
use phpDocumentor\Reflection\PseudoTypes\FloatValue;
26+
use phpDocumentor\Reflection\PseudoTypes\Generic;
27+
use phpDocumentor\Reflection\PseudoTypes\GenericTemplate;
2628
use phpDocumentor\Reflection\PseudoTypes\HtmlEscapedString;
2729
use phpDocumentor\Reflection\PseudoTypes\IntegerRange;
2830
use phpDocumentor\Reflection\PseudoTypes\IntegerValue;
@@ -56,7 +58,6 @@
5658
use phpDocumentor\Reflection\Types\Callable_;
5759
use phpDocumentor\Reflection\Types\CallableParameter;
5860
use phpDocumentor\Reflection\Types\ClassString;
59-
use phpDocumentor\Reflection\Types\Collection;
6061
use phpDocumentor\Reflection\Types\Compound;
6162
use phpDocumentor\Reflection\Types\Context;
6263
use phpDocumentor\Reflection\Types\Expression;
@@ -460,15 +461,24 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
460461
return new Self_(...$this->createTypesByTypeNodes($type->genericTypes, $context));
461462

462463
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));
464+
$mainType = $this->createType($type->type, $context);
465+
if ($mainType instanceof Object_ === false) {
466+
throw new RuntimeException(sprintf('%s is an unsupported generic', (string) $mainType));
466467
}
467468

468-
return new Collection(
469-
$collectionType->getFqsen(),
470-
...array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context))
469+
$types = array_map(
470+
function (TypeNode $node) use ($context): Type {
471+
$innerType = $this->createType($node, $context);
472+
if ($innerType instanceof Object_ && $innerType instanceof Generic === false) {
473+
return new GenericTemplate($innerType);
474+
}
475+
476+
return $innerType;
477+
},
478+
$type->genericTypes
471479
);
480+
481+
return new Generic($mainType->getFqsen(), $types);
472482
}
473483
}
474484

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/Object_.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
*
2929
* @psalm-immutable
3030
*/
31-
final class Object_ implements Type
31+
class Object_ implements Type
3232
{
3333
/** @var Fqsen|null */
34-
private $fqsen;
34+
protected $fqsen;
3535

3636
/**
3737
* Initializes this object with an optional FQSEN, if not provided this object is considered 'untyped'.

0 commit comments

Comments
 (0)