Completed
Push — master ( b928ab...26824d )
by Kevin
02:08
created

TokenCollection   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 72.09%

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 18
c 3
b 0
f 2
lcom 1
cbo 0
dl 0
loc 114
ccs 31
cts 43
cp 0.7209
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 9 2
A reset() 0 4 1
A current() 0 4 1
A end() 0 4 1
A next() 0 4 1
A prev() 0 4 1
A remove() 0 11 2
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 8 2
A offsetUnset() 0 4 1
A count() 0 4 1
A isEmpty() 0 4 1
A getIterator() 0 4 1
1
<?php
2
3
namespace Kevintweber\HtmlTokenizer\Tokens;
4
5
/**
6
 * A TokenCollection is a group of tokens designed to act similiar to
7
 * an array.
8
 */
9
class TokenCollection implements \ArrayAccess, \IteratorAggregate
10
{
11
    /** @var array[Token] */
12
    private $tokens;
13
14
    /**
15
     * Constructor
16
     */
17 6
    public function __construct()
18
    {
19 6
        $this->tokens = array();
20 6
    }
21
22 3
    public function toArray()
23
    {
24 3
        $result = array();
25 3
        foreach ($this->tokens as $token) {
26 2
            $result[] = $token->toArray();
27 3
        }
28
29 3
        return $result;
30
    }
31
32
    public function reset()
33
    {
34
        return reset($this->tokens);
35
    }
36
37
    public function current()
38
    {
39
        return current($this->tokens);
40
    }
41
42
    public function end()
43
    {
44
        return end($this->tokens);
45
    }
46
47
    public function next()
48
    {
49
        return next($this->tokens);
50
    }
51
52
    public function prev()
53
    {
54
        return prev($this->tokens);
55
    }
56
57 1
    public function remove(Token $token)
58
    {
59 1
        $key = array_search($token, $this->tokens, true);
60 1
        if ($key === false) {
61 1
            return false;
62
        }
63
64 1
        unset($this->tokens[$key]);
65
66 1
        return true;
67
    }
68
69
    /**
70
     * Required by the ArrayAccess interface.
71
     */
72 2
    public function offsetExists($offset)
73
    {
74 2
        return array_key_exists($offset, $this->tokens);
75
    }
76
77
    /**
78
     * Required by the ArrayAccess interface.
79
     */
80 1
    public function offsetGet($offset)
81
    {
82 1
        return $this->tokens[$offset];
83
    }
84
85
    /**
86
     * Required by the ArrayAccess interface.
87
     */
88 5
    public function offsetSet($offset, $value)
89
    {
90 5
        if (!$value instanceof Token) {
91 1
            throw new \InvalidArgumentException('Value must be of type Token.');
92
        }
93
94 4
        $this->tokens[$offset] = $value;
95 4
    }
96
97
    /**
98
     * Required by the ArrayAccess interface.
99
     */
100 1
    public function offsetUnset($offset)
101
    {
102 1
        unset($this->tokens[$offset]);
103 1
    }
104
105 3
    public function count()
106
    {
107 3
        return count($this->tokens);
108
    }
109
110 2
    public function isEmpty()
111
    {
112 2
        return empty($this->tokens);
113
    }
114
115
    /**
116
     * Required by the IteratorAggregate interface.
117
     */
118
    public function getIterator()
119
    {
120
        return new \ArrayIterator($this->tokens);
121
    }
122
}
123