Skip to content

Commit 6b2ed6e

Browse files
akireikinjaapio
authored andcommitted
Iterating over compound types in foreach loop
1 parent fb39335 commit 6b2ed6e

File tree

2 files changed

+115
-1
lines changed

2 files changed

+115
-1
lines changed

src/Types/Compound.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
namespace phpDocumentor\Reflection\Types;
1414

15+
use ArrayIterator;
16+
use IteratorAggregate;
1517
use phpDocumentor\Reflection\Type;
1618

1719
/**
@@ -21,7 +23,7 @@
2123
* using an OR operator (`|`). This combination of types signifies that whatever is associated with this compound type
2224
* may contain a value with any of the given types.
2325
*/
24-
final class Compound implements Type
26+
final class Compound implements Type, IteratorAggregate
2527
{
2628
/** @var Type[] */
2729
private $types;
@@ -80,4 +82,12 @@ public function __toString()
8082
{
8183
return implode('|', $this->types);
8284
}
85+
86+
/**
87+
* {@inheritdoc}
88+
*/
89+
public function getIterator()
90+
{
91+
return new ArrayIterator($this->types);
92+
}
8393
}

tests/unit/Types/CompoundTest.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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-2017 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+
/**
16+
* @coversDefaultClass \phpDocumentor\Reflection\Types\Compound
17+
*/
18+
class CompoundTest extends \PHPUnit_Framework_TestCase
19+
{
20+
/**
21+
* @covers ::__construct
22+
*
23+
* @expectedException \InvalidArgumentException
24+
* @expectedExceptionMessage A compound type can only have other types as elements
25+
*/
26+
public function testCompoundCannotBeConstructedFromType()
27+
{
28+
new Compound(['foo']);
29+
}
30+
31+
/**
32+
* @covers ::get
33+
*
34+
* @uses \phpDocumentor\Reflection\Types\Compound::__construct
35+
* @uses \phpDocumentor\Reflection\Types\Compound::has
36+
* @uses \phpDocumentor\Reflection\Types\Integer
37+
*/
38+
public function testCompoundGetType()
39+
{
40+
$integer = new Integer();
41+
42+
$this->assertSame($integer, (new Compound([$integer]))->get(0));
43+
}
44+
45+
/**
46+
* @covers ::get
47+
*
48+
* @uses \phpDocumentor\Reflection\Types\Compound::__construct
49+
* @uses \phpDocumentor\Reflection\Types\Compound::has
50+
*/
51+
public function testCompoundGetNotExistingType()
52+
{
53+
$this->assertNull((new Compound([]))->get(0));
54+
}
55+
56+
/**
57+
* @covers ::has
58+
*
59+
* @uses \phpDocumentor\Reflection\Types\Compound::__construct
60+
* @uses \phpDocumentor\Reflection\Types\Integer
61+
*/
62+
public function testCompoundHasType()
63+
{
64+
$this->assertTrue((new Compound([new Integer()]))->has(0));
65+
}
66+
67+
/**
68+
* @covers ::has
69+
*
70+
* @uses \phpDocumentor\Reflection\Types\Compound::__construct
71+
*/
72+
public function testCompoundHasNotExistingType()
73+
{
74+
$this->assertFalse((new Compound([]))->has(0));
75+
}
76+
77+
/**
78+
* @covers ::__construct
79+
* @covers ::__toString
80+
*
81+
* @uses \phpDocumentor\Reflection\Types\Integer
82+
* @uses \phpDocumentor\Reflection\Types\Boolean
83+
*/
84+
public function testCompoundCanBeConstructedAndStringifiedCorrectly()
85+
{
86+
$this->assertSame('int|bool', (string)(new Compound([new Integer(), new Boolean()])));
87+
}
88+
89+
/**
90+
* @covers ::getIterator
91+
*
92+
* @uses \phpDocumentor\Reflection\Types\Compound::__construct
93+
* @uses \phpDocumentor\Reflection\Types\Integer
94+
* @uses \phpDocumentor\Reflection\Types\Boolean
95+
*/
96+
public function testCompoundCanBeIterated()
97+
{
98+
$types = [new Integer(), new Boolean()];
99+
100+
foreach (new Compound($types) as $index => $type) {
101+
$this->assertSame($types[$index], $type);
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)