Completed
Push — master ( 26824d...9bbb30 )
by Kevin
02:15
created

TokenCollection   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 12
c 4
b 0
f 3
lcom 1
cbo 0
dl 0
loc 83
ccs 30
cts 30
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 9 2
A offsetExists() 0 4 1
A offsetGet() 0 4 1
A offsetSet() 0 14 3
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
    /**
33
     * Required by the ArrayAccess interface.
34
     */
35 2
    public function offsetExists($offset)
36
    {
37 2
        return array_key_exists($offset, $this->tokens);
38
    }
39
40
    /**
41
     * Required by the ArrayAccess interface.
42
     */
43 1
    public function offsetGet($offset)
44
    {
45 1
        return $this->tokens[$offset];
46
    }
47
48
    /**
49
     * Required by the ArrayAccess interface.
50
     */
51 5
    public function offsetSet($offset, $value)
52
    {
53 5
        if (!$value instanceof Token) {
54 1
            throw new \InvalidArgumentException('Value must be of type Token.');
55
        }
56
57 4
        if ($offset === null) {
58 3
            $this->tokens[] = $value;
59
60 3
            return;
61
        }
62
63 1
        $this->tokens[$offset] = $value;
64 1
    }
65
66
    /**
67
     * Required by the ArrayAccess interface.
68
     */
69 1
    public function offsetUnset($offset)
70
    {
71 1
        unset($this->tokens[$offset]);
72 1
    }
73
74 2
    public function count()
75
    {
76 2
        return count($this->tokens);
77
    }
78
79 3
    public function isEmpty()
80
    {
81 3
        return empty($this->tokens);
82
    }
83
84
    /**
85
     * Required by the IteratorAggregate interface.
86
     */
87 1
    public function getIterator()
88
    {
89 1
        return new \ArrayIterator($this->tokens);
90
    }
91
}
92