Skip to content

Commit e555aca

Browse files
laurentjjaapio
authored andcommitted
Address review comments
1 parent 13ebf89 commit e555aca

File tree

5 files changed

+128
-98
lines changed

5 files changed

+128
-98
lines changed

src/TypeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* For the full copyright and license information, please view the LICENSE
66
* file that was distributed with this source code.
77
*
8-
* @copyright 2010-2015 Mike van Riel<[email protected]>
8+
* @copyright 2010-2017 Mike van Riel<[email protected]>
99
* @license http://www.opensource.org/licenses/mit-license.php MIT
1010
* @link http://phpdoc.org
1111
*/

src/Types/AbstractList.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* This file is part of phpDocumentor.
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*
8+
* @copyright 2010-2015 Mike van Riel<[email protected]>
9+
* @license http://www.opensource.org/licenses/mit-license.php MIT
10+
* @link http://phpdoc.org
11+
*/
12+
13+
namespace phpDocumentor\Reflection\Types;
14+
15+
use phpDocumentor\Reflection\Type;
16+
17+
/**
18+
* Represents a list of values. This is an abstract class for Array_ and Collection.
19+
*
20+
*/
21+
abstract class AbstractList implements Type
22+
{
23+
/** @var Type */
24+
protected $valueType;
25+
26+
/** @var Type|null */
27+
protected $keyType;
28+
29+
/** @var Type */
30+
protected $defaultKeyType;
31+
32+
/**
33+
* Initializes this representation of an array with the given Type.
34+
*
35+
* @param Type $valueType
36+
* @param Type $keyType
37+
*/
38+
public function __construct(Type $valueType = null, Type $keyType = null)
39+
{
40+
if ($valueType === null) {
41+
$valueType = new Mixed_();
42+
}
43+
44+
$this->valueType = $valueType;
45+
$this->defaultKeyType = new Compound([ new String_(), new Integer() ]);
46+
$this->keyType = $keyType;
47+
48+
}
49+
50+
/**
51+
* Returns the type for the keys of this array.
52+
*
53+
* @return Type
54+
*/
55+
public function getKeyType()
56+
{
57+
if ($this->keyType === null) {
58+
return $this->defaultKeyType;
59+
}
60+
return $this->keyType;
61+
}
62+
63+
/**
64+
* Returns the value for the keys of this array.
65+
*
66+
* @return Type
67+
*/
68+
public function getValueType()
69+
{
70+
return $this->valueType;
71+
}
72+
73+
/**
74+
* Returns a rendered output of the Type as it would be used in a DocBlock.
75+
*
76+
* @return string
77+
*/
78+
public function __toString()
79+
{
80+
if ($this->keyType) {
81+
return 'array<'.$this->keyType.','.$this->valueType.'>';
82+
}
83+
84+
if ($this->valueType instanceof Mixed_) {
85+
return 'array';
86+
}
87+
88+
if ($this->valueType instanceof Compound) {
89+
return '(' . $this->valueType . ')[]';
90+
}
91+
92+
return $this->valueType . '[]';
93+
}
94+
}

src/Types/Array_.php

Lines changed: 1 addition & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212

1313
namespace phpDocumentor\Reflection\Types;
1414

15-
use phpDocumentor\Reflection\Type;
16-
1715
/**
1816
* Represents an array type as described in the PSR-5, the PHPDoc Standard.
1917
*
@@ -23,77 +21,6 @@
2321
* 2. Types (`string[]`), where the value type is provided by preceding an opening and closing square bracket with a
2422
* type name.
2523
*/
26-
class Array_ implements Type
24+
final class Array_ extends AbstractList
2725
{
28-
/** @var Type */
29-
protected $valueType;
30-
31-
/** @var Type|null */
32-
protected $keyType;
33-
34-
/** @var Type */
35-
protected $defaultKeyType;
36-
37-
/**
38-
* Initializes this representation of an array with the given Type.
39-
*
40-
* @param Type $valueType
41-
* @param Type $keyType
42-
*/
43-
public function __construct(Type $valueType = null, Type $keyType = null)
44-
{
45-
if ($valueType === null) {
46-
$valueType = new Mixed_();
47-
}
48-
49-
$this->valueType = $valueType;
50-
$this->defaultKeyType = new Compound([ new String_(), new Integer() ]);
51-
$this->keyType = $keyType;
52-
53-
}
54-
55-
/**
56-
* Returns the type for the keys of this array.
57-
*
58-
* @return Type
59-
*/
60-
public function getKeyType()
61-
{
62-
if ($this->keyType === null) {
63-
return $this->defaultKeyType;
64-
}
65-
return $this->keyType;
66-
}
67-
68-
/**
69-
* Returns the value for the keys of this array.
70-
*
71-
* @return Type
72-
*/
73-
public function getValueType()
74-
{
75-
return $this->valueType;
76-
}
77-
78-
/**
79-
* Returns a rendered output of the Type as it would be used in a DocBlock.
80-
*
81-
* @return string
82-
*/
83-
public function __toString()
84-
{
85-
if ($this->keyType) {
86-
return 'array<'.$this->keyType.','.$this->valueType.'>';
87-
}
88-
89-
if ($this->valueType instanceof Mixed_) {
90-
return 'array';
91-
}
92-
93-
if ($this->valueType instanceof Compound) {
94-
return '(' . $this->valueType . ')[]';
95-
}
96-
97-
return $this->valueType . '[]';
98-
}
9926
}

src/Types/Collection.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* - ACollectionObject can be 'array' or an object that can act as an array
2727
* - aValueType and aKeyType can be any type expression
2828
*/
29-
class Collection extends Array_
29+
final class Collection extends AbstractList
3030
{
3131

3232
/** @var Fqsen */
@@ -43,7 +43,6 @@ public function __construct(Fqsen $fqsen, Type $valueType, Type $keyType = null)
4343
parent::__construct($valueType, $keyType);
4444

4545
$this->fqsen = $fqsen;
46-
4746
}
4847

4948
/**
@@ -66,6 +65,7 @@ public function __toString()
6665
if ($this->keyType === null) {
6766
return $this->fqsen.'<'.$this->valueType . '>';
6867
}
68+
6969
return $this->fqsen.'<'.$this->keyType . ',' . $this->valueType . '>';
7070
}
7171
}

tests/unit/CollectionResolverTest.php

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@
1212

1313
namespace phpDocumentor\Reflection;
1414

15-
use Mockery as m;
1615
use phpDocumentor\Reflection\Types\Array_;
1716
use phpDocumentor\Reflection\Types\Collection;
1817
use phpDocumentor\Reflection\Types\Compound;
1918
use phpDocumentor\Reflection\Types\Context;
20-
use phpDocumentor\Reflection\Types\Iterable_;
21-
use phpDocumentor\Reflection\Types\Nullable;
2219
use phpDocumentor\Reflection\Types\Object_;
23-
use phpDocumentor\Reflection\Types\Boolean;
24-
use Mockery\MockInterface;
25-
use phpDocumentor\Reflection\Types\String_;
2620

2721
/**
22+
* @covers ::<private>
2823
* @coversDefaultClass phpDocumentor\Reflection\TypeResolver
2924
*/
3025
class CollectionResolverTest extends \PHPUnit_Framework_TestCase
@@ -33,7 +28,6 @@ class CollectionResolverTest extends \PHPUnit_Framework_TestCase
3328
*
3429
* @covers ::__construct
3530
* @covers ::resolve
36-
* @covers ::<private>
3731
*
3832
* @uses \phpDocumentor\Reflection\Types\Context
3933
* @uses \phpDocumentor\Reflection\Types\Compound
@@ -65,7 +59,6 @@ public function testResolvingCollection() {
6559
*
6660
* @covers ::__construct
6761
* @covers ::resolve
68-
* @covers ::<private>
6962
*
7063
* @uses \phpDocumentor\Reflection\Types\Context
7164
* @uses \phpDocumentor\Reflection\Types\Compound
@@ -99,7 +92,6 @@ public function testResolvingCollectionWithKeyType() {
9992
*
10093
* @covers ::__construct
10194
* @covers ::resolve
102-
* @covers ::<private>
10395
*
10496
* @uses \phpDocumentor\Reflection\Types\Context
10597
* @uses \phpDocumentor\Reflection\Types\Compound
@@ -129,7 +121,6 @@ public function testResolvingArrayCollection() {
129121
*
130122
* @covers ::__construct
131123
* @covers ::resolve
132-
* @covers ::<private>
133124
*
134125
* @uses \phpDocumentor\Reflection\Types\Context
135126
* @uses \phpDocumentor\Reflection\Types\Compound
@@ -159,7 +150,6 @@ public function testResolvingArrayCollectionWithKey() {
159150
*
160151
* @covers ::__construct
161152
* @covers ::resolve
162-
* @covers ::<private>
163153
*
164154
* @uses \phpDocumentor\Reflection\Types\Context
165155
* @uses \phpDocumentor\Reflection\Types\Compound
@@ -192,61 +182,80 @@ public function testResolvingCollectionOfCollection() {
192182
$this->assertInstanceOf(Types\Float_::class, $keyType->get(2));
193183
}
194184

195-
196185
/**
197186
* @covers ::__construct
198187
* @covers ::resolve
199-
* @covers ::<private>
200188
* @expectedException \RuntimeException
201189
* @expectedExceptionMessage An array can have only integers or strings as keys
202190
*/
203191
public function testBadArrayCollectionKey()
204192
{
205193
$fixture = new TypeResolver();
206194
$fixture->resolve('array<object,string>', new Context(''));
207-
208195
}
209196

210197
/**
211198
* @covers ::__construct
212199
* @covers ::resolve
213-
* @covers ::<private>
214200
* @expectedException \RuntimeException
215201
* @expectedExceptionMessage Unexpected collection operator "<", class name is missing
216202
*/
217203
public function testMissingStartCollection()
218204
{
219205
$fixture = new TypeResolver();
220206
$fixture->resolve('<string>', new Context(''));
221-
222207
}
223208

224209
/**
225210
* @covers ::__construct
226211
* @covers ::resolve
227-
* @covers ::<private>
228212
* @expectedException \RuntimeException
229213
* @expectedExceptionMessage Collection: ">" is missing
230214
*/
231215
public function testMissingEndCollection()
232216
{
233217
$fixture = new TypeResolver();
234218
$fixture->resolve('ArrayObject<object|string', new Context(''));
235-
236219
}
237220

238-
239221
/**
240222
* @covers ::__construct
241223
* @covers ::resolve
242-
* @covers ::<private>
243224
* @expectedException \RuntimeException
244225
* @expectedExceptionMessage string is not a collection
245226
*/
246227
public function testBadCollectionClass()
247228
{
248229
$fixture = new TypeResolver();
249230
$fixture->resolve('string<integer>', new Context(''));
231+
}
250232

233+
/**
234+
*
235+
* @covers ::__construct
236+
* @covers ::resolve
237+
*
238+
* @uses \phpDocumentor\Reflection\Types\Context
239+
* @uses \phpDocumentor\Reflection\Types\Compound
240+
* @uses \phpDocumentor\Reflection\Types\Collection
241+
* @uses \phpDocumentor\Reflection\Types\String_
242+
*/
243+
public function testResolvingCollectionAsArray() {
244+
$fixture = new TypeResolver();
245+
246+
/** @var Collection $resolvedType */
247+
$resolvedType = $fixture->resolve('array<string,float>', new Context(''));
248+
249+
$this->assertInstanceOf(Array_::class, $resolvedType);
250+
$this->assertSame('array<string,float>', (string)$resolvedType);
251+
252+
/** @var Array_ $valueType */
253+
$valueType = $resolvedType->getValueType();
254+
255+
/** @var Compound $keyType */
256+
$keyType = $resolvedType->getKeyType();
257+
258+
$this->assertInstanceOf(Types\Float_::class, $valueType);
259+
$this->assertInstanceOf(Types\String_::class, $keyType);
251260
}
252-
}
261+
}

0 commit comments

Comments
 (0)