Skip to content

Commit a5108c1

Browse files
authored
Merge pull request #25 from staabm/coll
Simplify TokenCollection
2 parents ad5778d + 058a17e commit a5108c1

File tree

2 files changed

+14
-76
lines changed

2 files changed

+14
-76
lines changed

src/TokenCollection.php

Lines changed: 12 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,30 @@
11
<?php declare(strict_types = 1);
22
namespace TheSeer\Tokenizer;
33

4-
class TokenCollection implements \ArrayAccess, \Iterator, \Countable {
4+
use ArrayIterator;
5+
use Countable;
6+
use Iterator;
7+
use IteratorAggregate;
8+
9+
/**
10+
* @implements IteratorAggregate<int, Token>
11+
*/
12+
class TokenCollection implements IteratorAggregate, Countable {
513

614
/** @var Token[] */
715
private $tokens = [];
816

9-
/** @var int */
10-
private $pos;
11-
1217
public function addToken(Token $token): void {
1318
$this->tokens[] = $token;
1419
}
1520

16-
public function current(): Token {
17-
return \current($this->tokens);
18-
}
19-
20-
public function key(): int {
21-
return \key($this->tokens);
22-
}
23-
24-
public function next(): void {
25-
\next($this->tokens);
26-
$this->pos++;
27-
}
28-
29-
public function valid(): bool {
30-
return $this->count() > $this->pos;
31-
}
32-
33-
public function rewind(): void {
34-
\reset($this->tokens);
35-
$this->pos = 0;
21+
public function getIterator(): Iterator
22+
{
23+
return new ArrayIterator($this->tokens);
3624
}
3725

3826
public function count(): int {
3927
return \count($this->tokens);
4028
}
4129

42-
public function offsetExists($offset): bool {
43-
return isset($this->tokens[$offset]);
44-
}
45-
46-
/**
47-
* @throws TokenCollectionException
48-
*/
49-
public function offsetGet($offset): Token {
50-
if (!$this->offsetExists($offset)) {
51-
throw new TokenCollectionException(
52-
\sprintf('No Token at offest %s', $offset)
53-
);
54-
}
55-
56-
return $this->tokens[$offset];
57-
}
58-
59-
/**
60-
* @param Token $value
61-
*
62-
* @throws TokenCollectionException
63-
*/
64-
public function offsetSet($offset, $value): void {
65-
if (!\is_int($offset)) {
66-
$type = \gettype($offset);
67-
68-
throw new TokenCollectionException(
69-
\sprintf(
70-
'Offset must be of type integer, %s given',
71-
$type === 'object' ? \get_class($value) : $type
72-
)
73-
);
74-
}
75-
76-
if (!$value instanceof Token) {
77-
$type = \gettype($value);
78-
79-
throw new TokenCollectionException(
80-
\sprintf(
81-
'Value must be of type %s, %s given',
82-
Token::class,
83-
$type === 'object' ? \get_class($value) : $type
84-
)
85-
);
86-
}
87-
$this->tokens[$offset] = $value;
88-
}
89-
90-
public function offsetUnset($offset): void {
91-
unset($this->tokens[$offset]);
92-
}
9330
}

src/XMLSerializer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ public function toXML(TokenCollection $tokens): string {
4040
$writer->startElement('line');
4141
$writer->writeAttribute('no', '1');
4242

43-
$previousToken = $tokens[0];
43+
$tokens->rewind();
44+
$previousToken = $tokens->current();
4445

4546
foreach ($tokens as $token) {
4647
if ($previousToken->getLine() < $token->getLine()) {

0 commit comments

Comments
 (0)