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 |
||
| 21 | class Formatter |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * The formatting options. |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public $options; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Clauses that must be inlined. |
||
| 32 | * |
||
| 33 | * These clauses usually are short and it's nicer to have them inline. |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | public static $INLINE_CLAUSES = array( |
||
| 38 | 'CREATE' => true, |
||
| 39 | 'LIMIT' => true, |
||
| 40 | 'PARTITION BY' => true, |
||
| 41 | 'PARTITION' => true, |
||
| 42 | 'PROCEDURE' => true, |
||
| 43 | 'SUBPARTITION BY' => true, |
||
| 44 | 'VALUES' => true, |
||
| 45 | ); |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Constructor. |
||
| 49 | * |
||
| 50 | * @param array $options the formatting options |
||
| 51 | */ |
||
| 52 | 18 | public function __construct(array $options = array()) |
|
| 56 | |||
| 57 | /** |
||
| 58 | * The specified formatting options are merged with the default values. |
||
| 59 | * |
||
| 60 | * @param array $options |
||
| 61 | * |
||
| 62 | * @return array |
||
| 63 | */ |
||
| 64 | 22 | private function getMergedOptions(array $options) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * The default formatting options. |
||
| 93 | * |
||
| 94 | * @return array |
||
| 95 | */ |
||
| 96 | 18 | protected function getDefaultOptions() |
|
| 151 | |||
| 152 | /** |
||
| 153 | * The styles used for HTML formatting. |
||
| 154 | * array($type, $flags, $span, $callback). |
||
| 155 | * |
||
| 156 | * @return array |
||
| 157 | */ |
||
| 158 | 18 | protected function getDefaultFormats() |
|
| 212 | |||
| 213 | 4 | private static function mergeFormats(array $formats, array $newFormats) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Formats the given list of tokens. |
||
| 257 | * |
||
| 258 | * @param TokensList $list the list of tokens |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | 18 | public function formatList($list) |
|
| 263 | { |
||
| 264 | /** |
||
| 265 | * The query to be returned. |
||
| 266 | * |
||
| 267 | * @var string |
||
| 268 | */ |
||
| 269 | 18 | $ret = ''; |
|
| 270 | |||
| 271 | /** |
||
| 272 | * The indentation level. |
||
| 273 | * |
||
| 274 | * @var int |
||
| 275 | */ |
||
| 276 | 18 | $indent = 0; |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Whether the line ended. |
||
| 280 | * |
||
| 281 | * @var bool |
||
| 282 | */ |
||
| 283 | 18 | $lineEnded = false; |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Whether current group is short (no linebreaks). |
||
| 287 | * |
||
| 288 | * @var bool |
||
| 289 | */ |
||
| 290 | 18 | $shortGroup = false; |
|
| 291 | |||
| 292 | /** |
||
| 293 | * The name of the last clause. |
||
| 294 | * |
||
| 295 | * @var string |
||
| 296 | */ |
||
| 297 | 18 | $lastClause = ''; |
|
| 298 | |||
| 299 | /** |
||
| 300 | * A stack that keeps track of the indentation level every time a new |
||
| 301 | * block is found. |
||
| 302 | * |
||
| 303 | * @var array |
||
| 304 | */ |
||
| 305 | 18 | $blocksIndentation = array(); |
|
| 306 | |||
| 307 | /** |
||
| 308 | * A stack that keeps track of the line endings every time a new block |
||
| 309 | * is found. |
||
| 310 | * |
||
| 311 | * @var array |
||
| 312 | */ |
||
| 313 | 18 | $blocksLineEndings = array(); |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Whether clause's options were formatted. |
||
| 317 | * |
||
| 318 | * @var bool |
||
| 319 | */ |
||
| 320 | 18 | $formattedOptions = false; |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Previously parsed token. |
||
| 324 | * |
||
| 325 | * @var Token|null |
||
| 326 | */ |
||
| 327 | 18 | $prev = null; |
|
| 328 | |||
| 329 | // In order to be able to format the queries correctly, the next token |
||
| 330 | // must be taken into consideration. The loop below uses two pointers, |
||
| 331 | // `$prev` and `$curr` which store two consecutive tokens. |
||
| 332 | // Actually, at every iteration the previous token is being used. |
||
| 333 | 18 | for ($list->idx = 0; $list->idx < $list->count; ++$list->idx) { |
|
| 334 | /** |
||
| 335 | * Token parsed at this moment. |
||
| 336 | * |
||
| 337 | * @var Token |
||
| 338 | */ |
||
| 339 | 18 | $curr = $list->tokens[$list->idx]; |
|
| 340 | |||
| 341 | 18 | if ($curr->type === Token::TYPE_WHITESPACE) { |
|
| 342 | // Whitespaces are skipped because the formatter adds its own. |
||
| 343 | 17 | continue; |
|
| 344 | } |
||
| 345 | |||
| 346 | 18 | if ($curr->type === Token::TYPE_COMMENT && $this->options['remove_comments']) { |
|
| 347 | // Skip Comments if option `remove_comments` is enabled |
||
| 348 | 1 | continue; |
|
| 349 | } |
||
| 350 | |||
| 351 | // Checking if pointers were initialized. |
||
| 352 | 18 | if ($prev !== null) { |
|
| 353 | // Checking if a new clause started. |
||
| 354 | 17 | if (static::isClause($prev) !== false) { |
|
| 355 | 17 | $lastClause = $prev->value; |
|
| 356 | 17 | $formattedOptions = false; |
|
| 357 | 17 | } |
|
| 358 | |||
| 359 | // The options of a clause should stay on the same line and everything that follows. |
||
| 360 | 17 | if ($this->options['parts_newline'] |
|
| 361 | 17 | && !$formattedOptions |
|
| 362 | 17 | && empty(self::$INLINE_CLAUSES[$lastClause]) |
|
| 363 | 17 | && ( |
|
| 364 | 16 | $curr->type !== Token::TYPE_KEYWORD |
|
| 365 | 16 | || ( |
|
| 366 | 4 | $curr->type === Token::TYPE_KEYWORD |
|
| 367 | 4 | && $curr->flags & Token::FLAG_KEYWORD_FUNCTION |
|
| 368 | 4 | ) |
|
| 369 | 4 | ) |
|
| 370 | 17 | ) { |
|
| 371 | 16 | $formattedOptions = true; |
|
| 372 | 16 | $lineEnded = true; |
|
| 373 | 16 | ++$indent; |
|
| 374 | 16 | } |
|
| 375 | |||
| 376 | // Checking if this clause ended. |
||
| 377 | 17 | if ($tmp = static::isClause($curr)) { |
|
| 378 | 7 | if ($tmp == 2 || $this->options['clause_newline']) { |
|
| 379 | 7 | $lineEnded = true; |
|
| 380 | 7 | if ($this->options['parts_newline']) { |
|
| 381 | 7 | --$indent; |
|
| 382 | 7 | } |
|
| 383 | 7 | } |
|
| 384 | 7 | } |
|
| 385 | |||
| 386 | // Indenting BEGIN ... END blocks. |
||
| 387 | 17 | if ($prev->type === Token::TYPE_KEYWORD && $prev->value === 'BEGIN') { |
|
| 388 | $lineEnded = true; |
||
| 389 | array_push($blocksIndentation, $indent); |
||
| 390 | ++$indent; |
||
| 391 | 17 | View Code Duplication | } elseif ($curr->type === Token::TYPE_KEYWORD && $curr->value === 'END') { |
| 392 | $lineEnded = true; |
||
| 393 | $indent = array_pop($blocksIndentation); |
||
| 394 | } |
||
| 395 | |||
| 396 | // Formatting fragments delimited by comma. |
||
| 397 | 17 | if ($prev->type === Token::TYPE_OPERATOR && $prev->value === ',') { |
|
| 398 | // Fragments delimited by a comma are broken into multiple |
||
| 399 | // pieces only if the clause is not inlined or this fragment |
||
| 400 | // is between brackets that are on new line. |
||
| 401 | 4 | if (end($blocksLineEndings) === true |
|
| 402 | 4 | || ( |
|
| 403 | 3 | empty(self::$INLINE_CLAUSES[$lastClause]) |
|
| 404 | 3 | && !$shortGroup |
|
| 405 | 3 | && $this->options['parts_newline'] |
|
| 406 | 2 | ) |
|
| 407 | 4 | ) { |
|
| 408 | 3 | $lineEnded = true; |
|
| 409 | 3 | } |
|
| 410 | 4 | } |
|
| 411 | |||
| 412 | // Handling brackets. |
||
| 413 | // Brackets are indented only if the length of the fragment between |
||
| 414 | // them is longer than 30 characters. |
||
| 415 | 17 | if ($prev->type === Token::TYPE_OPERATOR && $prev->value === '(') { |
|
| 416 | 6 | array_push($blocksIndentation, $indent); |
|
| 417 | 6 | $shortGroup = true; |
|
| 418 | 6 | if (static::getGroupLength($list) > 30) { |
|
| 419 | 1 | ++$indent; |
|
| 420 | 1 | $lineEnded = true; |
|
| 421 | 1 | $shortGroup = false; |
|
| 422 | 1 | } |
|
| 423 | 6 | array_push($blocksLineEndings, $lineEnded); |
|
| 424 | 17 | View Code Duplication | } elseif ($curr->type === Token::TYPE_OPERATOR && $curr->value === ')') { |
| 425 | 5 | $indent = array_pop($blocksIndentation); |
|
| 426 | 5 | $lineEnded |= array_pop($blocksLineEndings); |
|
| 427 | 5 | $shortGroup = false; |
|
| 428 | 5 | } |
|
| 429 | |||
| 430 | // Adding the token. |
||
| 431 | 17 | $ret .= $this->toString($prev); |
|
| 432 | |||
| 433 | // Finishing the line. |
||
| 434 | 17 | if ($lineEnded) { |
|
| 435 | 17 | if ($indent < 0) { |
|
| 436 | // TODO: Make sure this never occurs and delete it. |
||
|
|
|||
| 437 | 3 | $indent = 0; |
|
| 438 | 3 | } |
|
| 439 | |||
| 440 | 17 | $ret .= $this->options['line_ending'] |
|
| 441 | 17 | . str_repeat($this->options['indentation'], $indent); |
|
| 442 | |||
| 443 | 17 | $lineEnded = false; |
|
| 444 | 17 | } else { |
|
| 445 | // If the line ended there is no point in adding whitespaces. |
||
| 446 | // Also, some tokens do not have spaces before or after them. |
||
| 447 | if ( |
||
| 448 | // A space after delimiters that are longer than 2 characters. |
||
| 449 | 17 | $prev->value === 'DELIMITER' |
|
| 450 | 17 | || !( |
|
| 451 | 17 | ($prev->type === Token::TYPE_OPERATOR && ($prev->value === '.' || $prev->value === '(')) |
|
| 452 | // No space after . ( |
||
| 453 | 17 | || ($curr->type === Token::TYPE_OPERATOR && ($curr->value === '.' || $curr->value === ',' || $curr->value === '(' || $curr->value === ')')) |
|
| 454 | // No space before . , ( ) |
||
| 455 | 17 | || $curr->type === Token::TYPE_DELIMITER && mb_strlen($curr->value, 'UTF-8') < 2 |
|
| 456 | 17 | ) |
|
| 457 | 17 | ) { |
|
| 458 | 9 | $ret .= ' '; |
|
| 459 | 9 | } |
|
| 460 | } |
||
| 461 | 17 | } |
|
| 462 | |||
| 463 | // Iteration finished, consider current token as previous. |
||
| 464 | 18 | $prev = $curr; |
|
| 465 | 18 | } |
|
| 466 | |||
| 467 | 18 | if ($this->options['type'] === 'cli') { |
|
| 468 | 16 | return $ret . "\x1b[0m"; |
|
| 469 | } |
||
| 470 | |||
| 471 | 16 | return $ret; |
|
| 472 | } |
||
| 473 | |||
| 474 | 15 | public function escapeConsole($string) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Tries to print the query and returns the result. |
||
| 491 | * |
||
| 492 | * @param Token $token the token to be printed |
||
| 493 | * |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | 17 | public function toString($token) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Formats a query. |
||
| 532 | * |
||
| 533 | * @param string $query The query to be formatted |
||
| 534 | * @param array $options the formatting options |
||
| 535 | * |
||
| 536 | * @return string the formatted string |
||
| 537 | */ |
||
| 538 | 18 | public static function format($query, array $options = array()) |
|
| 545 | |||
| 546 | /** |
||
| 547 | * Computes the length of a group. |
||
| 548 | * |
||
| 549 | * A group is delimited by a pair of brackets. |
||
| 550 | * |
||
| 551 | * @param TokensList $list the list of tokens |
||
| 552 | * |
||
| 553 | * @return int |
||
| 554 | */ |
||
| 555 | 6 | public static function getGroupLength($list) |
|
| 593 | |||
| 594 | /** |
||
| 595 | * Checks if a token is a statement or a clause inside a statement. |
||
| 596 | * |
||
| 597 | * @param Token $token the token to be checked |
||
| 598 | * |
||
| 599 | * @return int|bool |
||
| 600 | */ |
||
| 601 | 17 | public static function isClause($token) |
|
| 616 | } |
||
| 617 |