Completed
Push — master ( 49d11e...aa9157 )
by Kevin
02:09
created

TokenCollection::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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
    public function filter(\Closure $filter)
124
    {
125
        return new static(array_filter($this->tokens, $filter));
0 ignored issues
show
Unused Code introduced by
The call to TokenCollection::__construct() has too many arguments starting with array_filter($this->tokens, $filter).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
126
    }
127
128
    public function map(\Closure $func)
129
    {
130
        return new static(array_map($func, $this->tokens));
0 ignored issues
show
Unused Code introduced by
The call to TokenCollection::__construct() has too many arguments starting with array_map($func, $this->tokens).

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
131
    }
132
133
    public function slice($offset, $length = null)
134
    {
135
        return array_slice($this->tokens, $offset, $length, true);
136
    }
137
}
138