Passed
Branch fuck-54 (18c095)
by Kacper
03:37
created

TokenList::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 6
cts 6
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Parser;
17
18
19
class TokenList
20
{
21
    private $_tokens  = [];
22
    private $_pending = [];
23
24 17
    private function _push(Token $token)
25
    {
26 17
        if (!isset($this->_tokens[$token->pos])) {
27 17
            $this->_tokens[$token->pos] = [];
28 17
        } else {
29 13
            $this->_pending[$token->pos] = true;
30
        }
31
32 17
        $this->_tokens[$token->pos][spl_object_hash($token)] = $token;
33
34 17
        return $this;
35
    }
36
37 16
    public function add(Token $token)
38
    {
39 16
        $this->_push($token);
40 16
        if ($token->getEnd()) {
41 16
            $this->_push($token->getEnd());
42 16
        }
43
44 16
        return $this;
45
    }
46
47 13
    public function batch(array $tokens)
48
    {
49 13
        foreach ($tokens as $token) {
50 2
            $this->_push($token);
51 13
        }
52
53 13
        return $this;
54
    }
55
56 17
    public function sort() {
57 17
        ksort($this->_tokens);
58
59 17
        foreach(array_keys($this->_pending) as $position) {
60 13
            uasort(
61 13
                $this->_tokens[$position],
62
                Token::class.'::compare'
63 13
            );
64 17
        }
65 17
        $this->_pending = [];
66
67 17
        return $this;
68
    }
69
70 17
    public function toArray()
71
    {
72 17
        return call_user_func_array('array_merge', $this->_tokens);
73
    }
74
}