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
3 changes: 1 addition & 2 deletions src/PseudoTypes/List_.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Integer;
use phpDocumentor\Reflection\Types\Mixed_;

/**
* Value Object representing the type 'list'.
Expand All @@ -41,7 +40,7 @@ public function __construct(?Type $valueType = null)
*/
public function __toString(): string
{
if ($this->valueType instanceof Mixed_) {
if ($this->valueType === null) {
return 'list';
}

Expand Down
9 changes: 4 additions & 5 deletions src/PseudoTypes/NonEmptyArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use phpDocumentor\Reflection\PseudoType;
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Mixed_;

/**
* Value Object representing the type 'non-empty-array'.
Expand All @@ -35,12 +34,12 @@ public function underlyingType(): Type
*/
public function __toString(): string
{
if ($this->keyType) {
return 'non-empty-array<' . $this->keyType . ',' . $this->valueType . '>';
if ($this->valueType === null) {
return 'non-empty-array';
}

if ($this->valueType instanceof Mixed_) {
return 'non-empty-array';
if ($this->keyType) {
return 'non-empty-array<' . $this->keyType . ',' . $this->valueType . '>';
}

return 'non-empty-array<' . $this->valueType . '>';
Expand Down
3 changes: 1 addition & 2 deletions src/PseudoTypes/NonEmptyList.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use phpDocumentor\Reflection\Type;
use phpDocumentor\Reflection\Types\Array_;
use phpDocumentor\Reflection\Types\Integer;
use phpDocumentor\Reflection\Types\Mixed_;

/**
* Value Object representing the type 'non-empty-list'.
Expand All @@ -41,7 +40,7 @@ public function __construct(?Type $valueType = null)
*/
public function __toString(): string
{
if ($this->valueType instanceof Mixed_) {
if ($this->valueType === null) {
return 'non-empty-list';
}

Expand Down
25 changes: 15 additions & 10 deletions src/Types/AbstractList.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
abstract class AbstractList implements Type
{
/** @var Type */
/** @var Type|null */
protected $valueType;

/** @var Type|null */
Expand All @@ -31,15 +31,15 @@ abstract class AbstractList implements Type
/** @var Type */
protected $defaultKeyType;

/** @var Type */
protected $defaultValueType;

/**
* Initializes this representation of an array with the given Type.
*/
public function __construct(?Type $valueType = null, ?Type $keyType = null)
{
if ($valueType === null) {
$valueType = new Mixed_();
}

$this->defaultValueType = new Mixed_();
$this->valueType = $valueType;
$this->defaultKeyType = new Compound([new String_(), new Integer()]);
$this->keyType = $keyType;
Expand All @@ -50,6 +50,11 @@ public function getOriginalKeyType(): ?Type
return $this->keyType;
}

public function getOriginalValueType(): ?Type
{
return $this->valueType;
}

/**
* Returns the type for the keys of this array.
*/
Expand All @@ -63,20 +68,20 @@ public function getKeyType(): Type
*/
public function getValueType(): Type
{
return $this->valueType;
return $this->valueType ?? $this->defaultValueType;
}

/**
* Returns a rendered output of the Type as it would be used in a DocBlock.
*/
public function __toString(): string
{
if ($this->keyType) {
return 'array<' . $this->keyType . ',' . $this->valueType . '>';
if ($this->valueType === null) {
return 'array';
}

if ($this->valueType instanceof Mixed_) {
return 'array';
if ($this->keyType) {
return 'array<' . $this->keyType . ',' . $this->valueType . '>';
}

if ($this->valueType instanceof Compound) {
Expand Down
5 changes: 3 additions & 2 deletions src/Types/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ public function getFqsen(): ?Fqsen
public function __toString(): string
{
$objectType = (string) ($this->fqsen ?? 'object');
$valueType = $this->getValueType();

if ($this->keyType === null) {
return $objectType . '<' . $this->valueType . '>';
return $objectType . '<' . $valueType . '>';
}

return $objectType . '<' . $this->keyType . ',' . $this->valueType . '>';
return $objectType . '<' . $this->keyType . ',' . $valueType . '>';
}
}
8 changes: 4 additions & 4 deletions src/Types/Iterable_.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ final class Iterable_ extends AbstractList
*/
public function __toString(): string
{
if ($this->keyType) {
return 'iterable<' . $this->keyType . ',' . $this->valueType . '>';
if ($this->valueType === null) {
return 'iterable';
}

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

return 'iterable<' . $this->valueType . '>';
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/PseudoTypes/ListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function provideArrays(): array
{
return [
'simple list' => [new List_(), 'list'],
'list of mixed' => [new List_(new Mixed_()), 'list'],
'list of mixed' => [new List_(new Mixed_()), 'list<mixed>'],
'list of single type' => [new List_(new String_()), 'list<string>'],
'list of compound type' => [new List_(new Compound([new Integer(), new String_()])), 'list<int|string>'],
];
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/PseudoTypes/NonEmptyArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function provideArrays(): array
{
return [
'simple non-empty-array' => [new NonEmptyArray(), 'non-empty-array'],
'non-empty-array of mixed' => [new NonEmptyArray(new Mixed_()), 'non-empty-array'],
'non-empty-array of mixed' => [new NonEmptyArray(new Mixed_()), 'non-empty-array<mixed>'],
'non-empty-array of single type' => [new NonEmptyArray(new String_()), 'non-empty-array<string>'],
'non-empty-array of compound type' =>
[
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/PseudoTypes/NonEmptyListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function provideArrays(): array
{
return [
'simple non-empty-list' => [new NonEmptyList(), 'non-empty-list'],
'non-empty-list of mixed' => [new NonEmptyList(new Mixed_()), 'non-empty-list'],
'non-empty-list of mixed' => [new NonEmptyList(new Mixed_()), 'non-empty-list<mixed>'],
'non-empty-list of single type' => [new NonEmptyList(new String_()), 'non-empty-list<string>'],
'non-empty-list of compound type' =>
[new NonEmptyList(new Compound([new Integer(), new String_()])), 'non-empty-list<int|string>'],
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -982,12 +982,22 @@ public function typeProvider(): array
),
]),
],
[
'array',
new Array_(),
],
[
'string[]',
new Array_(
new String_()
),
],
[
'mixed[]',
new Array_(
new Mixed_()
),
],
[
'$this',
new This(),
Expand Down Expand Up @@ -1178,6 +1188,22 @@ public function genericsProvider(): array
new Object_(new Fqsen('\\phpDocumentor\\ThirdClass')),
),
],
[
'array<mixed>',
new Array_(new Mixed_()),
],
[
'iterable<mixed>',
new Iterable_(new Mixed_()),
],
[
'non-empty-array<mixed>',
new NonEmptyArray(new Mixed_()),
],
[
'non-empty-list<mixed>',
new NonEmptyList(new Mixed_()),
],
];
}

Expand Down
43 changes: 42 additions & 1 deletion tests/unit/Types/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,54 @@

namespace phpDocumentor\Reflection\Types;

use phpDocumentor\Reflection\Fqsen;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \phpDocumentor\Reflection\Types\Array_
*/
class ArrayTest extends TestCase
{
/**
* @covers ::getOriginalKeyType
* @covers ::getOriginalValueType
* @covers ::getKeyType
* @covers ::getValueType
*/
public function testCreateWithoutParams(): void
{
$type = new Array_();

$this->assertNull($type->getOriginalKeyType());
$this->assertNull($type->getOriginalValueType());
$this->assertEquals(new Compound([new String_(), new Integer()]), $type->getKeyType());
$this->assertEquals(new Mixed_(), $type->getValueType());
}

/**
* @covers ::getOriginalKeyType
* @covers ::getOriginalValueType
* @covers ::getKeyType
* @covers ::getValueType
*/
public function testCreateWithParams(): void
{
$valueType = new Object_(new Fqsen('\\phpDocumentor\\Foo\\Bar'));
$keyType = new Compound(
[
new String_(),
new Integer(),
]
);

$type = new Array_($valueType, $keyType);

$this->assertSame($keyType, $type->getOriginalKeyType());
$this->assertSame($valueType, $type->getOriginalValueType());
$this->assertSame($keyType, $type->getKeyType());
$this->assertSame($valueType, $type->getValueType());
}

/**
* @dataProvider provideArrays
* @covers ::__toString
Expand All @@ -36,7 +77,7 @@ public function provideArrays(): array
{
return [
'simple array' => [new Array_(), 'array'],
'array of mixed' => [new Array_(new Mixed_()), 'array'],
'array of mixed' => [new Array_(new Mixed_()), 'mixed[]'],
'array of single type' => [new Array_(new String_()), 'string[]'],
'array of compound type' => [new Array_(new Compound([new Integer(), new String_()])), '(int|string)[]'],
'array with key type' => [new Array_(new String_(), new Integer()), 'array<int,string>'],
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/Types/IterableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function provideIterables(): array
{
return [
'simple iterable' => [new Iterable_(), 'iterable'],
'iterable of mixed' => [new Iterable_(new Mixed_()), 'iterable'],
'iterable of mixed' => [new Iterable_(new Mixed_()), 'iterable<mixed>'],
'iterable of single type' => [new Iterable_(new String_()), 'iterable<string>'],
'iterable of compound type' => [
new Iterable_(new Compound([new Integer(), new String_()])),
Expand Down