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 Parser 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 Parser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class Parser |
||
| 22 | { |
||
| 23 | const BLOCK_SCALAR_HEADER_PATTERN = '(?P<separator>\||>)(?P<modifiers>\+|\-|\d+|\+\d+|\-\d+|\d+\+|\d+\-)?(?P<comments> +#.*)?'; |
||
| 24 | // BC - wrongly named |
||
| 25 | const FOLDED_SCALAR_PATTERN = self::BLOCK_SCALAR_HEADER_PATTERN; |
||
| 26 | |||
| 27 | private $offset = 0; |
||
| 28 | private $totalNumberOfLines; |
||
| 29 | private $lines = array(); |
||
| 30 | private $currentLineNb = -1; |
||
| 31 | private $currentLine = ''; |
||
| 32 | private $refs = array(); |
||
| 33 | private $skippedLineNumbers = array(); |
||
| 34 | private $locallySkippedLineNumbers = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Constructor. |
||
| 38 | * |
||
| 39 | * @param int $offset The offset of YAML document (used for line numbers in error messages) |
||
| 40 | * @param int|null $totalNumberOfLines The overall number of lines being parsed |
||
| 41 | * @param int[] $skippedLineNumbers Number of comment lines that have been skipped by the parser |
||
| 42 | */ |
||
| 43 | public function __construct($offset = 0, $totalNumberOfLines = null, array $skippedLineNumbers = array()) |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Parses a YAML string to a PHP value. |
||
| 52 | * |
||
| 53 | * @param string $value A YAML string |
||
| 54 | * @param bool $exceptionOnInvalidType true if an exception must be thrown on invalid types (a PHP resource or object), false otherwise |
||
| 55 | * @param bool $objectSupport true if object support is enabled, false otherwise |
||
| 56 | * @param bool $objectForMap true if maps should return a stdClass instead of array() |
||
| 57 | * |
||
| 58 | * @return mixed A PHP value |
||
| 59 | * |
||
| 60 | * @throws ParseException If the YAML is not valid |
||
| 61 | */ |
||
| 62 | public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false) |
||
| 316 | |||
| 317 | private function parseBlock($offset, $yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap) |
||
| 318 | { |
||
| 319 | $skippedLineNumbers = $this->skippedLineNumbers; |
||
| 320 | |||
| 321 | foreach ($this->locallySkippedLineNumbers as $lineNumber) { |
||
| 322 | if ($lineNumber < $offset) { |
||
| 323 | continue; |
||
| 324 | } |
||
| 325 | |||
| 326 | $skippedLineNumbers[] = $lineNumber; |
||
| 327 | } |
||
| 328 | |||
| 329 | $parser = new self($offset, $this->totalNumberOfLines, $skippedLineNumbers); |
||
| 330 | $parser->refs = &$this->refs; |
||
| 331 | |||
| 332 | return $parser->parse($yaml, $exceptionOnInvalidType, $objectSupport, $objectForMap); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Returns the current line number (takes the offset into account). |
||
| 337 | * |
||
| 338 | * @return int The current line number |
||
| 339 | */ |
||
| 340 | private function getRealCurrentLineNb() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Returns the current line indentation. |
||
| 357 | * |
||
| 358 | * @return int The current line indentation |
||
| 359 | */ |
||
| 360 | private function getCurrentLineIndentation() |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Returns the next embed block of YAML. |
||
| 367 | * |
||
| 368 | * @param int $indentation The indent level at which the block is to be read, or null for default |
||
| 369 | * @param bool $inSequence True if the enclosing data structure is a sequence |
||
| 370 | * |
||
| 371 | * @return string A YAML string |
||
| 372 | * |
||
| 373 | * @throws ParseException When indentation problem are detected |
||
| 374 | */ |
||
| 375 | private function getNextEmbedBlock($indentation = null, $inSequence = false) |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Moves the parser to the next line. |
||
| 482 | * |
||
| 483 | * @return bool |
||
| 484 | */ |
||
| 485 | View Code Duplication | private function moveToNextLine() |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Moves the parser to the previous line. |
||
| 498 | * |
||
| 499 | * @return bool |
||
| 500 | */ |
||
| 501 | View Code Duplication | private function moveToPreviousLine() |
|
| 511 | |||
| 512 | /** |
||
| 513 | * Parses a YAML value. |
||
| 514 | * |
||
| 515 | * @param string $value A YAML value |
||
| 516 | * @param bool $exceptionOnInvalidType True if an exception must be thrown on invalid types false otherwise |
||
| 517 | * @param bool $objectSupport True if object support is enabled, false otherwise |
||
| 518 | * @param bool $objectForMap true if maps should return a stdClass instead of array() |
||
| 519 | * @param string $context The parser context (either sequence or mapping) |
||
| 520 | * |
||
| 521 | * @return mixed A PHP value |
||
| 522 | * |
||
| 523 | * @throws ParseException When reference does not exist |
||
| 524 | */ |
||
| 525 | private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $objectForMap, $context) |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Parses a block scalar. |
||
| 568 | * |
||
| 569 | * @param string $style The style indicator that was used to begin this block scalar (| or >) |
||
| 570 | * @param string $chomping The chomping indicator that was used to begin this block scalar (+ or -) |
||
| 571 | * @param int $indentation The indentation indicator that was used to begin this block scalar |
||
| 572 | * |
||
| 573 | * @return string The text value |
||
| 574 | */ |
||
| 575 | private function parseBlockScalar($style, $chomping = '', $indentation = 0) |
||
| 576 | { |
||
| 577 | $notEOF = $this->moveToNextLine(); |
||
| 578 | if (!$notEOF) { |
||
| 579 | return ''; |
||
| 580 | } |
||
| 581 | |||
| 582 | $isCurrentLineBlank = $this->isCurrentLineBlank(); |
||
| 583 | $blockLines = array(); |
||
| 584 | |||
| 585 | // leading blank lines are consumed before determining indentation |
||
| 586 | while ($notEOF && $isCurrentLineBlank) { |
||
| 587 | // newline only if not EOF |
||
| 588 | if ($notEOF = $this->moveToNextLine()) { |
||
| 589 | $blockLines[] = ''; |
||
| 590 | $isCurrentLineBlank = $this->isCurrentLineBlank(); |
||
| 591 | } |
||
| 592 | } |
||
| 593 | |||
| 594 | // determine indentation if not specified |
||
| 595 | if (0 === $indentation) { |
||
| 596 | if (preg_match('/^ +/', $this->currentLine, $matches)) { |
||
| 597 | $indentation = strlen($matches[0]); |
||
| 598 | } |
||
| 599 | } |
||
| 600 | |||
| 601 | if ($indentation > 0) { |
||
| 602 | $pattern = sprintf('/^ {%d}(.*)$/', $indentation); |
||
| 603 | |||
| 604 | while ( |
||
| 605 | $notEOF && ( |
||
| 606 | $isCurrentLineBlank || |
||
| 607 | preg_match($pattern, $this->currentLine, $matches) |
||
| 608 | ) |
||
| 609 | ) { |
||
| 610 | if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) { |
||
| 611 | $blockLines[] = substr($this->currentLine, $indentation); |
||
| 612 | } elseif ($isCurrentLineBlank) { |
||
| 613 | $blockLines[] = ''; |
||
| 614 | } else { |
||
| 615 | $blockLines[] = $matches[1]; |
||
| 616 | } |
||
| 617 | |||
| 618 | // newline only if not EOF |
||
| 619 | if ($notEOF = $this->moveToNextLine()) { |
||
| 620 | $isCurrentLineBlank = $this->isCurrentLineBlank(); |
||
| 621 | } |
||
| 622 | } |
||
| 623 | } elseif ($notEOF) { |
||
| 624 | $blockLines[] = ''; |
||
| 625 | } |
||
| 626 | |||
| 627 | if ($notEOF) { |
||
| 628 | $blockLines[] = ''; |
||
| 629 | $this->moveToPreviousLine(); |
||
| 630 | } elseif (!$notEOF && !$this->isCurrentLineLastLineInDocument()) { |
||
| 631 | $blockLines[] = ''; |
||
| 632 | } |
||
| 633 | |||
| 634 | // folded style |
||
| 635 | if ('>' === $style) { |
||
| 636 | $text = ''; |
||
| 637 | $previousLineIndented = false; |
||
| 638 | $previousLineBlank = false; |
||
| 639 | |||
| 640 | for ($i = 0; $i < count($blockLines); ++$i) { |
||
| 641 | if ('' === $blockLines[$i]) { |
||
| 642 | $text .= "\n"; |
||
| 643 | $previousLineIndented = false; |
||
| 644 | $previousLineBlank = true; |
||
| 645 | } elseif (' ' === $blockLines[$i][0]) { |
||
| 646 | $text .= "\n".$blockLines[$i]; |
||
| 647 | $previousLineIndented = true; |
||
| 648 | $previousLineBlank = false; |
||
| 649 | View Code Duplication | } elseif ($previousLineIndented) { |
|
| 650 | $text .= "\n".$blockLines[$i]; |
||
| 651 | $previousLineIndented = false; |
||
| 652 | $previousLineBlank = false; |
||
| 653 | } elseif ($previousLineBlank || 0 === $i) { |
||
| 654 | $text .= $blockLines[$i]; |
||
| 655 | $previousLineIndented = false; |
||
| 656 | $previousLineBlank = false; |
||
| 657 | View Code Duplication | } else { |
|
| 658 | $text .= ' '.$blockLines[$i]; |
||
| 659 | $previousLineIndented = false; |
||
| 660 | $previousLineBlank = false; |
||
| 661 | } |
||
| 662 | } |
||
| 663 | } else { |
||
| 664 | $text = implode("\n", $blockLines); |
||
| 665 | } |
||
| 666 | |||
| 667 | // deal with trailing newlines |
||
| 668 | if ('' === $chomping) { |
||
| 669 | $text = preg_replace('/\n+$/', "\n", $text); |
||
| 670 | } elseif ('-' === $chomping) { |
||
| 671 | $text = preg_replace('/\n+$/', '', $text); |
||
| 672 | } |
||
| 673 | |||
| 674 | return $text; |
||
| 675 | } |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Returns true if the next line is indented. |
||
| 679 | * |
||
| 680 | * @return bool Returns true if the next line is indented, false otherwise |
||
| 681 | */ |
||
| 682 | private function isNextLineIndented() |
||
| 704 | |||
| 705 | /** |
||
| 706 | * Returns true if the current line is blank or if it is a comment line. |
||
| 707 | * |
||
| 708 | * @return bool Returns true if the current line is empty or if it is a comment line, false otherwise |
||
| 709 | */ |
||
| 710 | private function isCurrentLineEmpty() |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Returns true if the current line is blank. |
||
| 717 | * |
||
| 718 | * @return bool Returns true if the current line is blank, false otherwise |
||
| 719 | */ |
||
| 720 | private function isCurrentLineBlank() |
||
| 724 | |||
| 725 | /** |
||
| 726 | * Returns true if the current line is a comment line. |
||
| 727 | * |
||
| 728 | * @return bool Returns true if the current line is a comment line, false otherwise |
||
| 729 | */ |
||
| 730 | private function isCurrentLineComment() |
||
| 737 | |||
| 738 | private function isCurrentLineLastLineInDocument() |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Cleanups a YAML string to be parsed. |
||
| 745 | * |
||
| 746 | * @param string $value The input YAML string |
||
| 747 | * |
||
| 748 | * @return string A cleaned up YAML string |
||
| 749 | */ |
||
| 750 | private function cleanup($value) |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Returns true if the next line starts unindented collection. |
||
| 783 | * |
||
| 784 | * @return bool Returns true if the next line starts unindented collection, false otherwise |
||
| 785 | */ |
||
| 786 | private function isNextLineUnIndentedCollection() |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Returns true if the string is un-indented collection item. |
||
| 815 | * |
||
| 816 | * @return bool Returns true if the string is un-indented collection item, false otherwise |
||
| 817 | */ |
||
| 818 | private function isStringUnIndentedCollectionItem() |
||
| 822 | |||
| 823 | /** |
||
| 824 | * Tests whether or not the current line is the header of a block scalar. |
||
| 825 | * |
||
| 826 | * @return bool |
||
| 827 | */ |
||
| 828 | private function isBlockScalarHeader() |
||
| 832 | } |
||
| 833 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: