Completed
Push — master ( b2c699...f42af0 )
by Kacper
02:37
created

Token::compare()   B

Complexity

Conditions 11
Paths 12

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 2 Features 0
Metric Value
c 7
b 2
f 0
dl 0
loc 18
rs 7.1162
cc 11
eloc 12
nc 12
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2015, 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
use Kadet\Highlighter\Language\Language;
20
use Kadet\Highlighter\Utils\Helper;
21
use Kadet\Highlighter\Utils\StringHelper;
22
23
class Token
24
{
25
    const NAME = null;
26
27
    protected static $_id = 0;
28
29
    public $pos;
30
    public $name;
31
    public $index = 1;
32
33
    /**
34
     * @var Token
35
     */
36
    protected $_end;
37
38
    /**
39
     * @var Token
40
     */
41
    protected $_start;
42
43
    /** @var Rule */
44
    protected $_rule;
45
46
    protected $_valid;
47
    protected $_length;
48
49
    public $id;
50
51
    /**
52
     * Token constructor.
53
     */
54
    public function __construct(array $options)
55
    {
56
        // Name
57
        if (isset($options[0])) {
58
            $this->name = $options[0];
59
        }
60
61
        if (isset($options['pos'])) {
62
            $this->pos = $options['pos'];
63
        }
64
65
        if (isset($options['index'])) {
66
            $this->index = $options['index'];
67
        }
68
69
        if (isset($options['start'])) {
70
            $this->setStart($options['start']);
71
        }
72
73
        if (isset($options['rule'])) {
74
            $this->setRule($options['rule']);
75
        }
76
77
        if (isset($options['end'])) {
78
            $this->setEnd($options['end']);
79
        }
80
81
        $this->id = ++self::$_id;
82
83
        if (isset($options['length'])) {
84
            new static([$this->name, 'pos' => $this->pos + $options['length'], 'start' => $this, 'rule' => $this->getRule()]);
85
        }
86
    }
87
88
    public static function compare(Token $a, Token $b)
89
    {
90
        if ($a->pos === $b->pos) {
91
            $multiplier = $a->isEnd() ? -1 : 1;
92
93
            if (($rule = Helper::cmp($b->_rule->getPriority(), $a->_rule->getPriority())) !== 0) {
94
                return $multiplier*$rule;
95
            } elseif (($a->isStart() && !$b->isStart()) || ($a->isEnd() && !$b->isEnd())) {
96
                return $multiplier;
97
            } elseif (($rule = Helper::cmp($b->index, $a->index)) !== 0) {
98
                return $multiplier*$rule;
99
            } else {
100
                return $multiplier*($a->id < $b->id ? -1 : 1);
101
            }
102
        }
103
104
        return ($a->pos > $b->pos) ? 1 : -1;
105
    }
106
107
    public function isStart()
108
    {
109
        return $this->_start === null && !($this->_rule instanceof CloseRule);
110
    }
111
112
    public function isEnd()
113
    {
114
        return $this->_end === null && !($this->_rule instanceof OpenRule);
115
    }
116
117
    public function isValid(Language $language, $context = null)
118
    {
119
        if ($this->_valid === null) {
120
            $this->validate($language, $context);
121
        }
122
123
        return $this->_valid;
124
    }
125
126
    protected function validate(Language $language, $context)
127
    {
128
        $this->setValid(
129
            $language === $this->_rule->getLanguage() &&
130
            $this->_rule->validateContext($context, $this->isEnd() ? [$this->name => Rule::CONTEXT_IN] : [])
131
        );
132
    }
133
134
    public function setValid($valid = true)
135
    {
136
        $this->_valid = $valid;
137
138
        if ($this->_end !== null) {
139
            $this->_end->_valid = $this->_valid;
140
        } elseif ($this->_start !== null) {
141
            $this->_start->_valid = $this->_valid;
142
        }
143
    }
144
145
    /**
146
     * @return mixed
147
     */
148
    public function getStart()
149
    {
150
        return $this->_start;
151
    }
152
153
    /**
154
     * @param Token $start
155
     */
156 View Code Duplication
    public function setStart(Token $start = null)
157
    {
158
        $this->_end = null;
159
        $this->_start = $start;
160
161
        if ($start !== null) {
162
            $this->_start->_end = $this;
163
        }
164
    }
165
166
    /**
167
     * @return Token|null
168
     */
169
    public function getEnd()
170
    {
171
        return $this->_end;
172
    }
173
174
    /**
175
     * @param Token $end
176
     */
177 View Code Duplication
    public function setEnd(Token $end = null)
178
    {
179
        $this->_start = null;
180
        $this->_end = $end;
181
        $this->_length = null;
182
183
        if ($end !== null) {
184
            $this->_end->_start = $this;
185
        }
186
    }
187
188
    /**
189
     * @return Rule
190
     */
191
    public function getRule()
192
    {
193
        return $this->_rule;
194
    }
195
196
    /**
197
     * @param Rule $rule
198
     */
199
    public function setRule(Rule $rule)
200
    {
201
        $this->_rule = $rule;
202
    }
203
204
    public function getLength()
205
    {
206
        if ($this->_length === null) {
207
            $this->_length = $this->_end === null ? 0 : $this->_end->pos - $this->pos;
208
        }
209
210
        return $this->_length;
211
    }
212
213
    public function dump($text = null)
214
    {
215
        $pos = StringHelper::positionToLine($text, $this->pos);
216
        $pos = $pos['line'] . ':' . $pos['pos'];
217
218
        if ($this->isStart()) {
219
            $result = "Start ({$this->name}) $pos";
220
            if ($text !== null && $this->_end !== null) {
221
                $result .= "  \x02" . substr($text, $this->pos, $this->_end->pos - $this->pos) . "\x03";
222
            }
223
        } else {
224
            $result = "End ({$this->name}) $pos";
225
        }
226
227
        return $result;
228
    }
229
}