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

Token   B

Complexity

Total Complexity 46

Size/Duplication

Total Lines 207
Duplicated Lines 9.18 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 17
Bugs 2 Features 0
Metric Value
wmc 46
c 17
b 2
f 0
lcom 1
cbo 3
dl 19
loc 207
rs 8.3999

15 Methods

Rating   Name   Duplication   Size   Complexity  
D __construct() 0 33 8
B compare() 0 18 11
A isStart() 0 4 2
A isEnd() 0 4 2
A isValid() 0 8 2
A validate() 0 7 3
A setValid() 0 10 3
A getStart() 0 4 1
A setStart() 9 9 2
A getEnd() 0 4 1
A setEnd() 10 10 2
A getRule() 0 4 1
A setRule() 0 4 1
A getLength() 0 8 3
A dump() 0 16 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Token often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Token, and based on these observations, apply Extract Interface, too.

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
}