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 |
||
12 | final class RegularParser implements ParserInterface |
||
13 | { |
||
14 | private $lexerRegex; |
||
15 | private $tokens; |
||
16 | private $tokensCount; |
||
17 | private $position; |
||
18 | private $backtracks; |
||
19 | |||
20 | const TOKEN_OPEN = 1; |
||
21 | const TOKEN_CLOSE = 2; |
||
22 | const TOKEN_MARKER = 3; |
||
23 | const TOKEN_SEPARATOR = 4; |
||
24 | const TOKEN_DELIMITER = 5; |
||
25 | const TOKEN_STRING = 6; |
||
26 | const TOKEN_WS = 7; |
||
27 | |||
28 | 4 | public function __construct(SyntaxInterface $syntax = null) |
|
32 | |||
33 | /** |
||
34 | * @param string $text |
||
35 | * |
||
36 | * @return ParsedShortcode[] |
||
37 | */ |
||
38 | 40 | public function parse($text) |
|
39 | { |
||
40 | 40 | $this->tokens = $this->tokenize($text); |
|
41 | 40 | $this->backtracks = array(); |
|
42 | 40 | $this->position = 0; |
|
43 | 40 | $this->tokensCount = count($this->tokens); |
|
44 | |||
45 | 40 | $shortcodes = array(); |
|
46 | 40 | while($this->position < $this->tokensCount) { |
|
47 | 39 | while($this->position < $this->tokensCount && !$this->lookahead(self::TOKEN_OPEN)) { |
|
48 | 16 | $this->position++; |
|
49 | } |
||
50 | 39 | $names = array(); |
|
51 | 39 | $this->beginBacktrack(); |
|
52 | 39 | $matches = $this->shortcode($names); |
|
53 | 39 | if(is_array($matches)) { |
|
54 | 32 | foreach($matches as $shortcode) { |
|
55 | 32 | $shortcodes[] = $shortcode; |
|
56 | } |
||
57 | } |
||
58 | } |
||
59 | |||
60 | 40 | return $shortcodes; |
|
61 | } |
||
62 | |||
63 | 32 | private function getObject($name, $parameters, $bbCode, $offset, $content, $text) |
|
67 | |||
68 | /* --- RULES ----------------------------------------------------------- */ |
||
69 | |||
70 | 39 | private function shortcode(array &$names) |
|
116 | |||
117 | 23 | private function content(array &$names) |
|
118 | { |
||
119 | 23 | $content = null; |
|
120 | 23 | $shortcodes = array(); |
|
121 | 23 | $closingName = null; |
|
122 | $appendContent = function(array $token) use(&$content) { $content .= $token[1]; }; |
||
123 | |||
124 | 23 | while($this->position < $this->tokensCount) { |
|
125 | 15 | while($this->match(array(self::TOKEN_STRING, self::TOKEN_WS), $appendContent)) { |
|
126 | 14 | continue; |
|
127 | } |
||
128 | |||
129 | 15 | $this->beginBacktrack(); |
|
130 | 15 | $matchedShortcodes = $this->shortcode($names); |
|
131 | 15 | if(is_string($matchedShortcodes)) { |
|
132 | 1 | $closingName = $matchedShortcodes; |
|
133 | 1 | break; |
|
134 | } |
||
135 | 15 | if(false !== $matchedShortcodes) { |
|
136 | 6 | $shortcodes = array_merge($shortcodes, $matchedShortcodes); |
|
137 | 6 | continue; |
|
138 | } |
||
139 | 13 | $this->backtrack(); |
|
140 | |||
141 | 13 | $this->beginBacktrack(); |
|
142 | 13 | if(false !== ($closingName = $this->close($names))) { |
|
143 | 10 | if(null === $content) { $content = ''; } |
|
144 | 10 | $this->backtrack(); |
|
145 | 10 | $shortcodes = array(); |
|
146 | 10 | break; |
|
147 | } |
||
148 | 4 | $closingName = null; |
|
149 | 4 | $this->backtrack(); |
|
150 | 4 | if($this->position < $this->tokensCount) { |
|
151 | 1 | $shortcodes = array(); |
|
152 | 1 | break; |
|
153 | } |
||
154 | |||
155 | 3 | $this->match(null, $appendContent); |
|
156 | } |
||
157 | |||
158 | 23 | return array($this->position < $this->tokensCount ? $content : false, $shortcodes, $closingName); |
|
159 | } |
||
160 | |||
161 | 13 | private function close(array &$names) |
|
173 | |||
174 | 35 | private function bbCode() |
|
178 | |||
179 | 35 | private function parameters() |
|
180 | { |
||
181 | 35 | $parameters = array(); |
|
182 | $setName = function(array $token) use(&$name) { $name = $token[1]; }; |
||
183 | |||
184 | 35 | while(true) { |
|
185 | 35 | $name = null; |
|
|
|||
186 | |||
187 | 35 | $this->match(self::TOKEN_WS); |
|
188 | 35 | if($this->lookahead(array(self::TOKEN_MARKER, self::TOKEN_CLOSE))) { break; } |
|
189 | 22 | if(!$this->match(self::TOKEN_STRING, $setName, true)) { return false; } |
|
190 | 21 | if(!$this->match(self::TOKEN_SEPARATOR, null, true)) { $parameters[$name] = null; continue; } |
|
191 | 20 | if(false === ($value = $this->value())) { return false; } |
|
192 | 19 | $this->match(self::TOKEN_WS); |
|
193 | |||
194 | 19 | $parameters[$name] = $value; |
|
195 | } |
||
196 | |||
197 | 33 | return $parameters; |
|
198 | } |
||
199 | |||
200 | 21 | private function value() |
|
201 | { |
||
202 | 21 | $value = ''; |
|
203 | $appendValue = function(array $token) use(&$value) { $value .= $token[1]; }; |
||
204 | |||
205 | 21 | if($this->match(self::TOKEN_DELIMITER)) { |
|
206 | 18 | while($this->position < $this->tokensCount && !$this->lookahead(self::TOKEN_DELIMITER)) { |
|
207 | 18 | $this->match(null, $appendValue); |
|
208 | } |
||
209 | |||
210 | 18 | return $this->match(self::TOKEN_DELIMITER) ? $value : false; |
|
211 | } |
||
212 | |||
213 | 8 | return $this->match(self::TOKEN_STRING, $appendValue) ? $value : false; |
|
214 | } |
||
215 | |||
216 | /* --- PARSER ---------------------------------------------------------- */ |
||
217 | |||
218 | 39 | private function beginBacktrack() |
|
222 | |||
223 | private function getBacktrack() |
||
228 | |||
229 | 23 | private function backtrack($modifyPosition = true) |
|
230 | { |
||
231 | 23 | $tokens = array_pop($this->backtracks); |
|
232 | 23 | $count = count($tokens); |
|
233 | 23 | if($modifyPosition) { |
|
234 | 13 | $this->position -= $count; |
|
235 | } |
||
236 | |||
237 | 23 | foreach($this->backtracks as &$backtrack) { |
|
238 | // array_pop() in loop is much faster than array_slice() because |
||
239 | // it operates directly on the passed array |
||
240 | 23 | for($i = 0; $i < $count; $i++) { |
|
241 | 15 | array_pop($backtrack); |
|
242 | } |
||
243 | } |
||
244 | |||
245 | return implode('', array_map(function(array $token) { return $token[1]; }, $tokens)); |
||
246 | } |
||
247 | |||
248 | 39 | private function lookahead($type, $callback = null) |
|
265 | |||
266 | 39 | private function match($type, $callbacks = null, $ws = false) |
|
267 | { |
||
268 | 39 | if($this->position >= $this->tokensCount) { |
|
269 | 14 | return false; |
|
270 | } |
||
290 | |||
291 | /* --- LEXER ----------------------------------------------------------- */ |
||
292 | |||
293 | 40 | private function tokenize($text) |
|
315 | |||
316 | private function getTokenizerRegex(SyntaxInterface $syntax) |
||
334 | } |
||
335 |
It seems like you are assigning to a variable which was imported through a
use
statement which was not imported by reference.For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.
Change not visible in outer-scope
Change visible in outer-scope