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 Formatter 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 Formatter, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Formatter |
||
|
|||
26 | { |
||
27 | |||
28 | /** |
||
29 | * The formatting options. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | public $options; |
||
34 | |||
35 | /** |
||
36 | * Clauses that must be inlined. |
||
37 | * |
||
38 | * These clauses usually are short and it's nicer to have them inline. |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | public static $INLINE_CLAUSES = array( |
||
43 | 'CREATE' => true, |
||
44 | 'LIMIT' => true, |
||
45 | 'PARTITION BY' => true, |
||
46 | 'PARTITION' => true, |
||
47 | 'PROCEDURE' => true, |
||
48 | 'SUBPARTITION BY' => true, |
||
49 | 'VALUES' => true, |
||
50 | ); |
||
51 | |||
52 | /** |
||
53 | * Constructor. |
||
54 | * |
||
55 | * @param array $options The formatting options. |
||
56 | */ |
||
57 | 3 | public function __construct(array $options = array()) |
|
178 | |||
179 | /** |
||
180 | * Formats the given list of tokens. |
||
181 | * |
||
182 | * @param TokensList $list The list of tokens. |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | 3 | public function formatList($list) |
|
187 | { |
||
188 | |||
189 | /** |
||
190 | * The query to be returned. |
||
191 | * |
||
192 | * @var string $ret |
||
193 | */ |
||
194 | 3 | $ret = ''; |
|
195 | |||
196 | /** |
||
197 | * The indentation level. |
||
198 | * |
||
199 | * @var int $indent |
||
200 | */ |
||
201 | 3 | $indent = 0; |
|
202 | |||
203 | /** |
||
204 | * Whether the line ended. |
||
205 | * |
||
206 | * @var bool $lineEnded |
||
207 | */ |
||
208 | 3 | $lineEnded = false; |
|
209 | |||
210 | /** |
||
211 | * The name of the last clause. |
||
212 | * |
||
213 | * @var string $lastClause |
||
214 | */ |
||
215 | 3 | $lastClause = ''; |
|
216 | |||
217 | /** |
||
218 | * A stack that keeps track of the indentation level every time a new |
||
219 | * block is found. |
||
220 | * |
||
221 | * @var array $blocksIndentation |
||
222 | */ |
||
223 | 3 | $blocksIndentation = array(); |
|
224 | |||
225 | /** |
||
226 | * A stack that keeps track of the line endings every time a new block |
||
227 | * is found. |
||
228 | * |
||
229 | * @var array $blocksLineEndings |
||
230 | */ |
||
231 | 3 | $blocksLineEndings = array(); |
|
232 | |||
233 | /** |
||
234 | * Whether clause's options were formatted. |
||
235 | * |
||
236 | * @var bool $formattedOptions |
||
237 | */ |
||
238 | 3 | $formattedOptions = false; |
|
239 | |||
240 | /** |
||
241 | * Previously parsed token. |
||
242 | * |
||
243 | * @var Token $prev |
||
244 | */ |
||
245 | 3 | $prev = null; |
|
246 | |||
247 | /** |
||
248 | * Comments are being formatted separately to maintain the whitespaces |
||
249 | * before and after them. |
||
250 | * |
||
251 | * @var string $comment |
||
252 | */ |
||
253 | 3 | $comment = ''; |
|
254 | |||
255 | // In order to be able to format the queries correctly, the next token |
||
256 | // must be taken into consideration. The loop below uses two pointers, |
||
257 | // `$prev` and `$curr` which store two consecutive tokens. |
||
258 | // Actually, at every iteration the previous token is being used. |
||
259 | 3 | for ($list->idx = 0; $list->idx < $list->count; ++$list->idx) { |
|
260 | /** |
||
261 | * Token parsed at this moment. |
||
262 | * |
||
263 | * @var Token $curr |
||
264 | */ |
||
265 | 3 | $curr = $list->tokens[$list->idx]; |
|
266 | |||
267 | 3 | if ($curr->type === Token::TYPE_WHITESPACE) { |
|
268 | // Whitespaces are skipped because the formatter adds its own. |
||
269 | 3 | continue; |
|
270 | 3 | } elseif ($curr->type === Token::TYPE_COMMENT) { |
|
271 | // Whether the comments should be parsed. |
||
272 | if (!empty($this->options['remove_comments'])) { |
||
273 | continue; |
||
274 | } |
||
275 | |||
276 | if ($list->tokens[$list->idx - 1]->type === Token::TYPE_WHITESPACE) { |
||
277 | // The whitespaces before and after are preserved for |
||
278 | // formatting reasons. |
||
279 | $comment .= $list->tokens[$list->idx - 1]->token; |
||
280 | } |
||
281 | $comment .= $this->toString($curr); |
||
282 | if (($list->tokens[$list->idx + 1]->type === Token::TYPE_WHITESPACE) |
||
283 | && ($list->tokens[$list->idx + 2]->type !== Token::TYPE_COMMENT) |
||
284 | ) { |
||
285 | // Adding the next whitespace only there is no comment that |
||
286 | // follows it immediately which may cause adding a |
||
287 | // whitespace twice. |
||
288 | $comment .= $list->tokens[$list->idx + 1]->token; |
||
289 | } |
||
290 | |||
291 | // Everything was handled here, no need to continue. |
||
292 | continue; |
||
293 | } |
||
294 | |||
295 | // Checking if pointers were initialized. |
||
296 | 3 | if ($prev !== null) { |
|
297 | // Checking if a new clause started. |
||
298 | 3 | if (static::isClause($prev) !== false) { |
|
299 | 3 | $lastClause = $prev->value; |
|
300 | 3 | $formattedOptions = false; |
|
301 | 3 | } |
|
302 | |||
303 | // The options of a clause should stay on the same line and everything that follows. |
||
304 | 3 | if (($this->options['parts_newline']) |
|
305 | 3 | && (!$formattedOptions) |
|
306 | 3 | && (empty(self::$INLINE_CLAUSES[$lastClause])) |
|
307 | 3 | && (($curr->type !== Token::TYPE_KEYWORD) |
|
308 | 1 | || (($curr->type === Token::TYPE_KEYWORD) |
|
309 | 1 | && ($curr->flags & Token::FLAG_KEYWORD_FUNCTION))) |
|
310 | 3 | ) { |
|
311 | 3 | $formattedOptions = true; |
|
312 | 3 | $lineEnded = true; |
|
313 | 3 | ++$indent; |
|
314 | 3 | } |
|
315 | |||
316 | // Checking if this clause ended. |
||
317 | 3 | if ($tmp = static::isClause($curr)) { |
|
318 | 1 | if (($tmp == 2) || ($this->options['clause_newline'])) { |
|
319 | 1 | $lineEnded = true; |
|
320 | 1 | if ($this->options['parts_newline']) { |
|
321 | 1 | --$indent; |
|
322 | 1 | } |
|
323 | 1 | } |
|
324 | 1 | } |
|
325 | |||
326 | // Indenting BEGIN ... END blocks. |
||
327 | 3 | if (($prev->type === Token::TYPE_KEYWORD) && ($prev->value === 'BEGIN')) { |
|
328 | $lineEnded = true; |
||
329 | array_push($blocksIndentation, $indent); |
||
330 | ++$indent; |
||
331 | 3 | View Code Duplication | } elseif (($curr->type === Token::TYPE_KEYWORD) && ($curr->value === 'END')) { |
1 ignored issue
–
show
|
|||
332 | $lineEnded = true; |
||
333 | $indent = array_pop($blocksIndentation); |
||
334 | } |
||
335 | |||
336 | // Formatting fragments delimited by comma. |
||
337 | 3 | if (($prev->type === Token::TYPE_OPERATOR) && ($prev->value === ',')) { |
|
338 | // Fragments delimited by a comma are broken into multiple |
||
339 | // pieces only if the clause is not inlined or this fragment |
||
340 | // is between brackets that are on new line. |
||
341 | if (((empty(self::$INLINE_CLAUSES[$lastClause])) |
||
342 | && ($this->options['parts_newline'])) |
||
343 | || (end($blocksLineEndings) === true) |
||
344 | ) { |
||
345 | $lineEnded = true; |
||
346 | } |
||
347 | } |
||
348 | |||
349 | // Handling brackets. |
||
350 | // Brackets are indented only if the length of the fragment between |
||
351 | // them is longer than 30 characters. |
||
352 | 3 | if (($prev->type === Token::TYPE_OPERATOR) && ($prev->value === '(')) { |
|
353 | 1 | array_push($blocksIndentation, $indent); |
|
354 | 1 | if (static::getGroupLength($list) > 30) { |
|
355 | ++$indent; |
||
356 | $lineEnded = true; |
||
357 | } |
||
358 | 1 | array_push($blocksLineEndings, $lineEnded); |
|
359 | 3 | View Code Duplication | } elseif (($curr->type === Token::TYPE_OPERATOR) && ($curr->value === ')')) { |
1 ignored issue
–
show
|
|||
360 | 1 | $indent = array_pop($blocksIndentation); |
|
361 | 1 | $lineEnded |= array_pop($blocksLineEndings); |
|
362 | 1 | } |
|
363 | |||
364 | // Delimiter must be placed on the same line with the last |
||
365 | // clause. |
||
366 | 3 | if ($curr->type === Token::TYPE_DELIMITER) { |
|
367 | 3 | $lineEnded = false; |
|
368 | 3 | } |
|
369 | |||
370 | // Adding the token. |
||
371 | 3 | $ret .= $this->toString($prev); |
|
372 | |||
373 | // Finishing the line. |
||
374 | 3 | if ($lineEnded) { |
|
375 | 3 | if ($indent < 0) { |
|
376 | // TODO: Make sure this never occurs and delete it. |
||
377 | $indent = 0; |
||
378 | } |
||
379 | |||
380 | 3 | if ($curr->type !== Token::TYPE_COMMENT) { |
|
381 | 3 | $ret .= $this->options['line_ending'] |
|
382 | 3 | . str_repeat($this->options['indentation'], $indent); |
|
383 | 3 | } |
|
384 | 3 | $lineEnded = false; |
|
385 | 3 | } else { |
|
386 | // If the line ended there is no point in adding whitespaces. |
||
387 | // Also, some tokens do not have spaces before or after them. |
||
388 | 3 | if (!((($prev->type === Token::TYPE_OPERATOR) && (($prev->value === '.') || ($prev->value === '('))) |
|
389 | // No space after . ( |
||
390 | 3 | || (($curr->type === Token::TYPE_OPERATOR) && (($curr->value === '.') || ($curr->value === ',') || ($curr->value === '(') || ($curr->value === ')'))) |
|
391 | // No space before . , ( ) |
||
392 | 3 | || (($curr->type === Token::TYPE_DELIMITER)) && (mb_strlen($curr->value, 'UTF-8') < 2)) |
|
393 | // A space after delimiters that are longer than 2 characters. |
||
394 | 3 | || ($prev->value === 'DELIMITER') |
|
395 | 3 | ) { |
|
396 | 1 | $ret .= ' '; |
|
397 | 1 | } |
|
398 | } |
||
399 | 3 | } |
|
400 | |||
401 | 3 | if (!empty($comment)) { |
|
402 | $ret .= $comment; |
||
403 | $comment = ''; |
||
404 | } |
||
405 | |||
406 | // Iteration finished, consider current token as previous. |
||
407 | 3 | $prev = $curr; |
|
408 | 3 | } |
|
409 | |||
410 | 3 | return $ret; |
|
411 | } |
||
412 | |||
413 | /** |
||
414 | * Tries to print the query and returns the result. |
||
415 | * |
||
416 | * @param Token $token The token to be printed. |
||
417 | * |
||
418 | * @return string |
||
419 | */ |
||
420 | 3 | public function toString($token) |
|
421 | { |
||
422 | 3 | $text = $token->token; |
|
423 | |||
424 | 3 | foreach ($this->options['formats'] as $format) { |
|
425 | 3 | if (($token->type === $format['type']) |
|
426 | 3 | && (($token->flags & $format['flags']) === $format['flags']) |
|
427 | 3 | ) { |
|
428 | // Running transformation function. |
||
429 | 3 | if (!empty($format['function'])) { |
|
430 | 3 | $func = $format['function']; |
|
431 | 3 | $text = $func($text); |
|
432 | 3 | } |
|
433 | |||
434 | // Formatting HTML. |
||
435 | 3 | if ($this->options['type'] === 'html') { |
|
436 | 3 | return '<span ' . $format['html'] . '>' . $text . '</span>'; |
|
437 | } elseif ($this->options['type'] === 'cli') { |
||
438 | return $format['cli'] . $text; |
||
439 | } |
||
440 | |||
441 | break; |
||
442 | } |
||
443 | 3 | } |
|
444 | |||
445 | 2 | if ($this->options['type'] === 'cli') { |
|
446 | return "\e[39m" . $text; |
||
1 ignored issue
–
show
|
|||
447 | } |
||
448 | 2 | return $text; |
|
449 | } |
||
450 | |||
451 | /** |
||
452 | * Formats a query. |
||
453 | * |
||
454 | * @param string $query The query to be formatted |
||
455 | * @param array $options The formatting options. |
||
456 | * |
||
457 | * @return string The formatted string. |
||
458 | */ |
||
459 | 3 | public static function format($query, array $options = array()) |
|
465 | |||
466 | /** |
||
467 | * Computes the length of a group. |
||
468 | * |
||
469 | * A group is delimited by a pair of brackets. |
||
470 | * |
||
471 | * @param TokensList $list The list of tokens. |
||
472 | * |
||
473 | * @return int |
||
474 | */ |
||
475 | 1 | public static function getGroupLength($list) |
|
513 | |||
514 | /** |
||
515 | * Checks if a token is a statement or a clause inside a statement. |
||
516 | * |
||
517 | * @param Token $token The token to be checked. |
||
518 | * |
||
519 | * @return int|bool |
||
520 | */ |
||
521 | 3 | public static function isClause($token) |
|
532 | } |
||
533 |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.