Passed
Push — master ( 6c305a...f9f5fe )
by Kacper
02:57
created

UnprocessedTokens::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
class UnprocessedTokens
19
{
20
    private $_tokens  = [];
21
    private $_pending = [];
22
23 17
    private function _push(Token $token)
24
    {
25 17
        if (!isset($this->_tokens[$token->pos])) {
26 17
            $this->_tokens[$token->pos] = [];
27 17
        } else {
28 13
            $this->_pending[$token->pos] = true;
29
        }
30
31 17
        $this->_tokens[$token->pos][spl_object_hash($token)] = $token;
32
33 17
        return $this;
34
    }
35
36 16
    public function add(Token $token)
37
    {
38 16
        $this->_push($token);
39 16
        if ($token->getEnd()) {
40 16
            $this->_push($token->getEnd());
41 16
        }
42
43 16
        return $this;
44
    }
45
46 13
    public function batch($tokens)
47
    {
48 13
        foreach ($tokens as $token) {
49 2
            $this->_push($token);
50 13
        }
51
52 13
        return $this;
53
    }
54
55 17
    public function sort()
56
    {
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
}
75