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

TokenList   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 9
c 2
b 1
f 0
lcom 1
cbo 1
dl 0
loc 56
ccs 29
cts 29
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A _push() 0 12 2
A add() 0 9 2
A batch() 0 8 2
A sort() 0 13 2
A toArray() 0 4 1
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
}