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 classes like RegularParser 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 RegularParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | final class RegularParser implements ParserInterface |
||
14 | { |
||
15 | private $lexerRegex; |
||
16 | private $nameRegex; |
||
17 | private $tokens; |
||
18 | private $tokensCount; |
||
19 | private $position; |
||
20 | /** @var int[] */ |
||
21 | private $backtracks; |
||
22 | private $lastBacktrack; |
||
23 | private $tokenMap; |
||
24 | |||
25 | const TOKEN_OPEN = 1; |
||
26 | const TOKEN_CLOSE = 2; |
||
27 | const TOKEN_MARKER = 3; |
||
28 | const TOKEN_SEPARATOR = 4; |
||
29 | const TOKEN_DELIMITER = 5; |
||
30 | const TOKEN_STRING = 6; |
||
31 | const TOKEN_WS = 7; |
||
32 | |||
33 | 14 | public function __construct(SyntaxInterface $syntax = null) |
|
34 | { |
||
35 | 14 | $this->lexerRegex = $this->prepareLexer($syntax ?: new CommonSyntax()); |
|
36 | 14 | $this->nameRegex = '~^'.RegexBuilderUtility::buildNameRegex().'$~us'; |
|
37 | 14 | } |
|
38 | |||
39 | /** |
||
40 | * @param string $text |
||
41 | * |
||
42 | * @return ParsedShortcode[] |
||
43 | */ |
||
44 | 57 | public function parse($text) |
|
45 | { |
||
46 | 57 | $this->tokens = $this->tokenize($text); |
|
47 | 57 | $this->backtracks = array(); |
|
48 | 57 | $this->lastBacktrack = 0; |
|
49 | 57 | $this->position = 0; |
|
50 | 57 | $this->tokensCount = \count($this->tokens); |
|
51 | |||
52 | 57 | $shortcodes = array(); |
|
53 | 57 | while($this->position < $this->tokensCount) { |
|
54 | 56 | while($this->position < $this->tokensCount && false === $this->lookahead(self::TOKEN_OPEN)) { |
|
55 | 24 | $this->position++; |
|
56 | } |
||
57 | 56 | $names = array(); |
|
58 | 56 | $this->beginBacktrack(); |
|
59 | 56 | $matches = $this->shortcode($names); |
|
60 | 56 | if(\is_array($matches)) { |
|
61 | 48 | foreach($matches as $shortcode) { |
|
62 | 48 | $shortcodes[] = $shortcode; |
|
63 | } |
||
64 | } |
||
65 | } |
||
66 | |||
67 | 57 | return $shortcodes; |
|
68 | } |
||
69 | |||
70 | 48 | private function getObject($name, $parameters, $bbCode, $offset, $content, $text) |
|
71 | { |
||
72 | 48 | return new ParsedShortcode(new Shortcode($name, $parameters, $content, $bbCode), $text, $offset); |
|
73 | } |
||
74 | |||
75 | /* --- RULES ----------------------------------------------------------- */ |
||
76 | |||
77 | 56 | private function shortcode(array &$names) |
|
78 | { |
||
79 | 56 | $name = null; |
|
|
|||
80 | 56 | $offset = null; |
|
81 | |||
82 | 56 | if(!$this->match(self::TOKEN_OPEN, false)) { return false; } |
|
83 | 56 | $offset = $this->tokens[$this->position - 1][2]; |
|
84 | 56 | $this->match(self::TOKEN_WS, false); |
|
85 | 56 | if('' === $name = $this->match(self::TOKEN_STRING, false)) { return false; } |
|
86 | 53 | if($this->lookahead(self::TOKEN_STRING)) { return false; } |
|
87 | 53 | if(1 !== preg_match($this->nameRegex, $name, $matches)) { return false; } |
|
88 | 52 | $this->match(self::TOKEN_WS, false); |
|
89 | // bbCode |
||
90 | 52 | $bbCode = $this->match(self::TOKEN_SEPARATOR, true) ? $this->value() : null; |
|
91 | 52 | if(false === $bbCode) { return false; } |
|
92 | // parameters |
||
93 | 51 | if(false === ($parameters = $this->parameters())) { return false; } |
|
94 | |||
95 | // self-closing |
||
96 | 49 | if($this->match(self::TOKEN_MARKER, true)) { |
|
97 | 16 | if(!$this->match(self::TOKEN_CLOSE, false)) { return false; } |
|
98 | |||
99 | 15 | return array($this->getObject($name, $parameters, $bbCode, $offset, null, $this->getBacktrack())); |
|
100 | } |
||
101 | |||
102 | // just-closed or with-content |
||
103 | 37 | if(!$this->match(self::TOKEN_CLOSE, false)) { return false; } |
|
104 | 37 | $this->beginBacktrack(); |
|
105 | 37 | $names[] = $name; |
|
106 | 37 | list($content, $shortcodes, $closingName) = $this->content($names); |
|
107 | 37 | if(null !== $closingName && $closingName !== $name) { |
|
108 | 6 | array_pop($names); |
|
109 | 6 | array_pop($this->backtracks); |
|
110 | 6 | array_pop($this->backtracks); |
|
111 | |||
112 | 6 | return $closingName; |
|
113 | } |
||
114 | 37 | if(false === $content || $closingName !== $name) { |
|
115 | 24 | $this->backtrack(false); |
|
116 | 24 | $text = $this->backtrack(false); |
|
117 | |||
118 | 24 | return array_merge(array($this->getObject($name, $parameters, $bbCode, $offset, null, $text)), $shortcodes); |
|
119 | } |
||
120 | 19 | $content = $this->getBacktrack(); |
|
121 | 19 | if(!$this->close($names)) { return false; } |
|
122 | |||
123 | 19 | return array($this->getObject($name, $parameters, $bbCode, $offset, $content, $this->getBacktrack())); |
|
124 | } |
||
125 | |||
126 | 37 | private function content(array &$names) |
|
127 | { |
||
128 | 37 | $content = ''; |
|
129 | 37 | $shortcodes = array(); |
|
130 | 37 | $closingName = null; |
|
131 | |||
132 | 37 | while($this->position < $this->tokensCount) { |
|
133 | 28 | View Code Duplication | while($this->position < $this->tokensCount && false === $this->lookahead(self::TOKEN_OPEN)) { |
134 | 24 | $content .= $this->match(null, true); |
|
135 | } |
||
136 | |||
137 | 28 | $this->beginBacktrack(); |
|
138 | 28 | $matchedShortcodes = $this->shortcode($names); |
|
139 | 28 | if(\is_string($matchedShortcodes)) { |
|
140 | 6 | $closingName = $matchedShortcodes; |
|
141 | 6 | break; |
|
142 | } |
||
143 | 28 | if(\is_array($matchedShortcodes)) { |
|
144 | 15 | foreach($matchedShortcodes as $matchedShortcode) { |
|
145 | 15 | $shortcodes[] = $matchedShortcode; |
|
146 | } |
||
147 | 15 | continue; |
|
148 | } |
||
149 | 22 | $this->backtrack(); |
|
150 | |||
151 | 22 | $this->beginBacktrack(); |
|
152 | 22 | if(false !== ($closingName = $this->close($names))) { |
|
153 | 19 | if(null === $content) { $content = ''; } |
|
154 | 19 | $this->backtrack(); |
|
155 | 19 | $shortcodes = array(); |
|
156 | 19 | break; |
|
157 | } |
||
158 | 8 | $closingName = null; |
|
159 | 8 | $this->backtrack(); |
|
160 | |||
161 | 8 | $content .= $this->match(null, false); |
|
162 | } |
||
163 | |||
164 | 37 | return array($this->position < $this->tokensCount ? $content : false, $shortcodes, $closingName); |
|
165 | } |
||
166 | |||
167 | 22 | private function close(array &$names) |
|
168 | { |
||
169 | 22 | if(!$this->match(self::TOKEN_OPEN, true)) { return false; } |
|
170 | 20 | if(!$this->match(self::TOKEN_MARKER, true)) { return false; } |
|
171 | 20 | if(!$closingName = $this->match(self::TOKEN_STRING, true)) { return false; } |
|
172 | 20 | if(!$this->match(self::TOKEN_CLOSE, false)) { return false; } |
|
173 | |||
174 | 20 | return \in_array($closingName, $names, true) ? $closingName : false; |
|
175 | } |
||
176 | |||
177 | 51 | private function parameters() |
|
178 | { |
||
179 | 51 | $parameters = array(); |
|
180 | |||
181 | 51 | while(true) { |
|
182 | 51 | $name = null; |
|
183 | |||
184 | 51 | $this->match(self::TOKEN_WS, false); |
|
185 | 51 | if($this->lookahead(self::TOKEN_MARKER) || $this->lookahead(self::TOKEN_CLOSE)) { break; } |
|
186 | 27 | if(!$name = $this->match(self::TOKEN_STRING, true)) { return false; } |
|
187 | 26 | if(!$this->match(self::TOKEN_SEPARATOR, true)) { $parameters[$name] = null; continue; } |
|
188 | 25 | if(false === ($value = $this->value())) { return false; } |
|
189 | 24 | $this->match(self::TOKEN_WS, false); |
|
190 | |||
191 | 24 | $parameters[$name] = $value; |
|
192 | } |
||
193 | |||
194 | 49 | return $parameters; |
|
195 | } |
||
196 | |||
197 | 27 | private function value() |
|
198 | { |
||
199 | 27 | $value = ''; |
|
200 | |||
201 | 27 | if($this->match(self::TOKEN_DELIMITER, false)) { |
|
202 | 19 | View Code Duplication | while($this->position < $this->tokensCount && false === $this->lookahead(self::TOKEN_DELIMITER)) { |
203 | 19 | $value .= $this->match(null, false); |
|
204 | } |
||
205 | |||
206 | 19 | return $this->match(self::TOKEN_DELIMITER, false) ? $value : false; |
|
207 | } |
||
208 | |||
209 | 14 | if($tmp = $this->match(self::TOKEN_STRING, false)) { |
|
210 | 13 | $value .= $tmp; |
|
211 | 13 | while($tmp = $this->match(self::TOKEN_STRING, false)) { |
|
212 | 2 | $value .= $tmp; |
|
213 | } |
||
214 | |||
215 | 13 | return $value; |
|
216 | } |
||
217 | |||
218 | 1 | return false; |
|
219 | } |
||
220 | |||
221 | /* --- PARSER ---------------------------------------------------------- */ |
||
222 | |||
223 | 56 | private function beginBacktrack() |
|
224 | { |
||
225 | 56 | $this->backtracks[] = $this->position; |
|
226 | 56 | $this->lastBacktrack = $this->position; |
|
227 | 56 | } |
|
228 | |||
229 | 30 | private function getBacktrack() |
|
230 | { |
||
231 | 30 | $position = array_pop($this->backtracks); |
|
232 | 30 | $backtrack = ''; |
|
233 | 30 | View Code Duplication | for($i = $position; $i < $this->position; $i++) { |
234 | 30 | $backtrack .= $this->tokens[$i][1]; |
|
235 | } |
||
236 | |||
237 | 30 | return $backtrack; |
|
238 | } |
||
239 | |||
240 | 37 | private function backtrack($modifyPosition = true) |
|
241 | { |
||
242 | 37 | $position = array_pop($this->backtracks); |
|
243 | 37 | if($modifyPosition) { |
|
244 | 22 | $this->position = $position; |
|
245 | } |
||
246 | |||
247 | 37 | $backtrack = ''; |
|
248 | 37 | View Code Duplication | for($i = $position; $i < $this->lastBacktrack; $i++) { |
249 | 24 | $backtrack .= $this->tokens[$i][1]; |
|
250 | } |
||
251 | 37 | $this->lastBacktrack = $position; |
|
252 | |||
253 | 37 | return $backtrack; |
|
254 | } |
||
255 | |||
256 | 56 | private function lookahead($type) |
|
260 | |||
261 | 56 | private function match($type, $ws) |
|
262 | { |
||
263 | 56 | if($this->position >= $this->tokensCount) { |
|
264 | 20 | return ''; |
|
265 | } |
||
266 | |||
267 | 56 | $token = $this->tokens[$this->position]; |
|
268 | 56 | if(!empty($type) && $token[0] !== $type) { |
|
269 | 56 | return ''; |
|
270 | } |
||
271 | |||
279 | |||
280 | /* --- LEXER ----------------------------------------------------------- */ |
||
281 | |||
282 | 57 | private function tokenize($text) |
|
301 | |||
302 | 14 | private function prepareLexer(SyntaxInterface $syntax) |
|
319 | } |
||
320 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.