Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions src/PseudoTypes/Generic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\PseudoTypes;

use phpDocumentor\Reflection\Fqsen;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Object_;

use function implode;

/**
* Value Object representing a type with generics.
*
* @psalm-immutable
*/
final class Generic extends Object_
{
/** @var Type[] */
private $types;

/**
* @param Type[] $types
*/
public function __construct(?Fqsen $fqsen, array $types)
{
parent::__construct($fqsen);

$this->types = $types;
}

/**
* @return Type[]
*/
public function getTypes(): array
{
return $this->types;
}

public function __toString(): string
{
$objectType = (string) ($this->fqsen ?? 'object');

return $objectType . '<' . implode(', ', $this->types) . '>';
}
}
43 changes: 43 additions & 0 deletions src/PseudoTypes/GenericTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link http://phpdoc.org
*/

namespace phpDocumentor\Reflection\PseudoTypes;

use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Object_;

/**
* Value Object representing the wrapper over the generic.
*
* @psalm-immutable
*/
final class GenericTemplate implements Type
{
/** @var Object_ */
private $resolvedType;

public function __construct(Object_ $resolvedType)
{
$this->resolvedType = $resolvedType;
}

public function getResolvedType(): Object_
{
return $this->resolvedType;
}

public function __toString(): string
{
return (string) $this->resolvedType;
}
}
24 changes: 17 additions & 7 deletions src/TypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
use phpDocumentor\Reflection\PseudoTypes\ConstExpression;
use phpDocumentor\Reflection\PseudoTypes\False_;
use phpDocumentor\Reflection\PseudoTypes\FloatValue;
use phpDocumentor\Reflection\PseudoTypes\Generic;
use phpDocumentor\Reflection\PseudoTypes\GenericTemplate;
use phpDocumentor\Reflection\PseudoTypes\HtmlEscapedString;
use phpDocumentor\Reflection\PseudoTypes\IntegerRange;
use phpDocumentor\Reflection\PseudoTypes\IntegerValue;
Expand Down Expand Up @@ -56,7 +58,6 @@
use phpDocumentor\Reflection\Types\Callable_;
use phpDocumentor\Reflection\Types\CallableParameter;
use phpDocumentor\Reflection\Types\ClassString;
use phpDocumentor\Reflection\Types\Collection;
use phpDocumentor\Reflection\Types\Compound;
use phpDocumentor\Reflection\Types\Context;
use phpDocumentor\Reflection\Types\Expression;
Expand Down Expand Up @@ -460,15 +461,24 @@ private function createFromGeneric(GenericTypeNode $type, Context $context): Typ
return new Self_(...$this->createTypesByTypeNodes($type->genericTypes, $context));

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

return new Collection(
$collectionType->getFqsen(),
...array_reverse($this->createTypesByTypeNodes($type->genericTypes, $context))
$types = array_map(
function (TypeNode $node) use ($context): Type {
$innerType = $this->createType($node, $context);
if ($innerType instanceof Object_ && $innerType instanceof Generic === false) {
return new GenericTemplate($innerType);
}

return $innerType;
},
$type->genericTypes
);

return new Generic($mainType->getFqsen(), $types);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Types/AbstractList.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use phpDocumentor\Reflection\Type;

/**
* Represents a list of values. This is an abstract class for Array_ and Collection.
* Represents a list of values. This is an abstract class for Array_ and List_.
*
* @psalm-immutable
*/
Expand Down
69 changes: 0 additions & 69 deletions src/Types/Collection.php

This file was deleted.

4 changes: 2 additions & 2 deletions src/Types/Object_.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
*
* @psalm-immutable
*/
final class Object_ implements Type
class Object_ implements Type
{
/** @var Fqsen|null */
private $fqsen;
protected $fqsen;

/**
* Initializes this object with an optional FQSEN, if not provided this object is considered 'untyped'.
Expand Down
Loading