|
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
|
|
|
class TokenFactory implements TokenFactoryInterface |
|
20
|
|
|
{ |
|
21
|
|
|
const START = 0x1; |
|
22
|
|
|
const END = 0x2; |
|
23
|
|
|
|
|
24
|
|
|
private $_class; |
|
25
|
|
|
private $_base; |
|
26
|
|
|
private $_rule; |
|
27
|
|
|
private $_offset; |
|
28
|
|
|
|
|
29
|
|
|
private $_cache = []; |
|
30
|
|
|
|
|
31
|
|
|
public $type = 0x3; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* TokenFactory constructor. |
|
36
|
|
|
* |
|
37
|
|
|
* @param $class |
|
38
|
|
|
*/ |
|
39
|
61 |
|
public function __construct($class) { |
|
40
|
61 |
|
$this->setClass($class); |
|
41
|
60 |
|
} |
|
42
|
|
|
|
|
43
|
47 |
|
public function create($params) { |
|
44
|
47 |
|
$params[0] = !empty($params[0]) ? $this->getName($params[0]) : $this->_base; |
|
45
|
47 |
|
if(empty($params['rule'])) { |
|
46
|
42 |
|
$params['rule'] = $this->_rule; |
|
47
|
42 |
|
} |
|
48
|
|
|
|
|
49
|
47 |
|
if(isset($params['pos'])) { |
|
50
|
44 |
|
$params['pos'] += $this->_offset; |
|
51
|
44 |
|
} |
|
52
|
|
|
|
|
53
|
47 |
|
$end = null; |
|
|
|
|
|
|
54
|
|
|
|
|
55
|
47 |
|
if (isset($params['length']) && ($this->type & self::END)) { |
|
56
|
37 |
|
$end = $params; |
|
57
|
37 |
|
$end['pos'] += $params['length']; |
|
58
|
|
|
|
|
59
|
37 |
|
$params['end'] = new $this->_class($end); |
|
60
|
37 |
|
} |
|
61
|
|
|
|
|
62
|
47 |
|
$token = new $this->_class($params); |
|
63
|
|
|
|
|
64
|
47 |
|
if($this->type == 0x3) { |
|
65
|
47 |
|
return $token; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
if($this->type === self::START) { |
|
69
|
2 |
|
$token->setEnd(false); |
|
70
|
2 |
|
return $token; |
|
71
|
|
|
} else { |
|
72
|
1 |
|
$token->getEnd()->setStart(false); |
|
73
|
1 |
|
return $token->getEnd(); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
23 |
|
private function getName($name) { |
|
78
|
23 |
|
if (!isset($this->_cache[$name])) { |
|
79
|
23 |
|
$this->_cache[$name] = str_replace('$', $this->_base, $name); |
|
80
|
23 |
|
} |
|
81
|
|
|
|
|
82
|
23 |
|
return $this->_cache[$name]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public function getBase() |
|
89
|
|
|
{ |
|
90
|
1 |
|
return $this->_base; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $base |
|
95
|
|
|
*/ |
|
96
|
13 |
|
public function setBase($base) |
|
97
|
|
|
{ |
|
98
|
13 |
|
$this->_cache = []; // invalidate cache |
|
99
|
13 |
|
$this->_base = $base; |
|
100
|
13 |
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return mixed |
|
104
|
|
|
*/ |
|
105
|
1 |
|
public function getRule() |
|
106
|
|
|
{ |
|
107
|
1 |
|
return $this->_rule; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param mixed $rule |
|
112
|
|
|
*/ |
|
113
|
58 |
|
public function setRule($rule) |
|
114
|
|
|
{ |
|
115
|
58 |
|
$this->_rule = $rule; |
|
116
|
58 |
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return mixed |
|
120
|
|
|
*/ |
|
121
|
1 |
|
public function getClass() |
|
122
|
|
|
{ |
|
123
|
1 |
|
return $this->_class; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @return int |
|
128
|
|
|
*/ |
|
129
|
|
|
public function getOffset() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->_offset; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param int $offset |
|
136
|
|
|
*/ |
|
137
|
13 |
|
public function setOffset($offset) |
|
138
|
|
|
{ |
|
139
|
13 |
|
$this->_offset = $offset; |
|
140
|
13 |
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param mixed $class |
|
146
|
|
|
* |
|
147
|
|
|
* @throws \InvalidArgumentException |
|
148
|
|
|
*/ |
|
149
|
61 |
|
public function setClass($class) |
|
150
|
|
|
{ |
|
151
|
61 |
|
if(!is_a($class, 'Kadet\Highlighter\Parser\Token', true)) { |
|
152
|
1 |
|
throw new \InvalidArgumentException('$class must extend Kadet\Highlighter\Parser\Token'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
60 |
|
$this->_class = $class; |
|
156
|
|
|
} |
|
157
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.