Skip to content

Commit 8bf1d7f

Browse files
Implement IteratorAggregate
1 parent 015dbb6 commit 8bf1d7f

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

ChangeLog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ All notable changes are documented in this file using the [Keep a CHANGELOG](htt
1111
* `SebastianBergmann\Diff\Line::content()` and `SebastianBergmann\Diff\Diff::type()`
1212
* `SebastianBergmann\Diff\Line::isAdded()`,`SebastianBergmann\Diff\Line::isRemoved()`, and `SebastianBergmann\Diff\Line::isUnchanged()`
1313

14+
### Changed
15+
16+
* `SebastianBergmann\Diff\Diff` now implements `IteratorAggregate`, iterating over it yields the aggregated `SebastianBergmann\Diff\Chunk` objects
17+
* `SebastianBergmann\Diff\Chunk` now implements `IteratorAggregate`, iterating over it yields the aggregated `SebastianBergmann\Diff\Line` objects
18+
1419
### Deprecated
1520

1621
* `SebastianBergmann\Diff\Chunk::getStart()`, `SebastianBergmann\Diff\Chunk::getStartRange()`, `SebastianBergmann\Diff\Chunk::getEnd()`, `SebastianBergmann\Diff\Chunk::getEndRange()`, and `SebastianBergmann\Diff\Chunk::getLines()`

src/Chunk.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
*/
1010
namespace SebastianBergmann\Diff;
1111

12-
final class Chunk
12+
use ArrayIterator;
13+
use IteratorAggregate;
14+
use Traversable;
15+
16+
/**
17+
* @template-implements IteratorAggregate<int, Line>
18+
*/
19+
final class Chunk implements IteratorAggregate
1320
{
1421
private int $start;
1522
private int $startRange;
@@ -109,4 +116,9 @@ public function getLines(): array
109116
{
110117
return $this->lines;
111118
}
119+
120+
public function getIterator(): Traversable
121+
{
122+
return new ArrayIterator($this->lines);
123+
}
112124
}

src/Diff.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
*/
1010
namespace SebastianBergmann\Diff;
1111

12-
final class Diff
12+
use ArrayIterator;
13+
use IteratorAggregate;
14+
use Traversable;
15+
16+
/**
17+
* @template-implements IteratorAggregate<int, Chunk>
18+
*/
19+
final class Diff implements IteratorAggregate
1320
{
1421
/**
1522
* @psalm-var non-empty-string
@@ -99,4 +106,9 @@ public function getChunks(): array
99106
{
100107
return $this->chunks;
101108
}
109+
110+
public function getIterator(): Traversable
111+
{
112+
return new ArrayIterator($this->chunks);
113+
}
102114
}

tests/ChunkTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,17 @@ public function testLinesCanBeSet(): void
5959
$this->assertSame($lines, $chunk->lines());
6060
}
6161

62+
public function testCanBeIterated(): void
63+
{
64+
$line = new Line;
65+
$chunk = new Chunk(lines: [$line]);
66+
67+
foreach ($chunk as $index => $_line) {
68+
$this->assertSame(0, $index);
69+
$this->assertSame($line, $_line);
70+
}
71+
}
72+
6273
private function chunk(): Chunk
6374
{
6475
return new Chunk(

tests/DiffTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,15 @@ public function testSetChunksAfterConstruction(): void
6464
$this->assertSame($chunks, $diff->chunks(), 'Expect chunks to be passed value.');
6565
$this->assertSame($chunks, $diff->getChunks(), 'Expect chunks to be passed value.');
6666
}
67+
68+
public function testCanBeIterated(): void
69+
{
70+
$chunk = new Chunk;
71+
$diff = new Diff('from', 'to', [$chunk]);
72+
73+
foreach ($diff as $index => $_chunk) {
74+
$this->assertSame(0, $index);
75+
$this->assertSame($chunk, $_chunk);
76+
}
77+
}
6778
}

0 commit comments

Comments
 (0)