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 |
||
| 34 | class Parser |
||
| 35 | { |
||
| 36 | private $lexer; |
||
| 37 | private $input; |
||
| 38 | private $file; |
||
| 39 | private $tags = array(); |
||
| 40 | private $languageSpecifierLine; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Initializes parser. |
||
| 44 | * |
||
| 45 | * @param Lexer $lexer Lexer instance |
||
| 46 | */ |
||
| 47 | 55 | public function __construct(Lexer $lexer) |
|
| 51 | |||
| 52 | /** |
||
| 53 | * Parses input & returns features array. |
||
| 54 | * |
||
| 55 | * @param string $input Gherkin string document |
||
| 56 | * @param string $file File name |
||
| 57 | * |
||
| 58 | * @return FeatureNode|null |
||
| 59 | * |
||
| 60 | * @throws ParserException |
||
| 61 | */ |
||
| 62 | 53 | public function parse($input, $file = null) |
|
| 63 | { |
||
| 64 | 53 | $this->languageSpecifierLine = null; |
|
| 65 | 53 | $this->input = $input; |
|
| 66 | 53 | $this->file = $file; |
|
| 67 | 53 | $this->tags = array(); |
|
| 68 | |||
| 69 | try { |
||
| 70 | 53 | $this->lexer->analyse($this->input, 'en'); |
|
| 71 | 53 | } catch (LexerException $e) { |
|
| 72 | throw new ParserException( |
||
| 73 | sprintf('Lexer exception "%s" thrown for file %s', $e->getMessage(), $file), |
||
| 74 | 0, |
||
| 75 | $e |
||
| 76 | ); |
||
| 77 | } |
||
| 78 | |||
| 79 | 53 | $feature = null; |
|
| 80 | 53 | while ('EOS' !== ($predicted = $this->predictTokenType())) { |
|
| 81 | 53 | $node = $this->parseExpression(); |
|
| 82 | |||
| 83 | 45 | if (null === $node || "\n" === $node) { |
|
| 84 | 3 | continue; |
|
| 85 | } |
||
| 86 | |||
| 87 | 44 | if (!$feature && $node instanceof FeatureNode) { |
|
| 88 | 44 | $feature = $node; |
|
| 89 | 44 | continue; |
|
| 90 | } |
||
| 91 | |||
| 92 | if ($feature && $node instanceof FeatureNode) { |
||
| 93 | throw new ParserException(sprintf( |
||
| 94 | 'Only one feature is allowed per feature file. But %s got multiple.', |
||
| 95 | $this->file |
||
| 96 | )); |
||
| 97 | } |
||
| 98 | |||
| 99 | if (is_string($node)) { |
||
| 100 | throw new ParserException(sprintf( |
||
| 101 | 'Expected Feature, but got text: "%s"%s', |
||
| 102 | $node, |
||
| 103 | $this->file ? ' in file: ' . $this->file : '' |
||
| 104 | )); |
||
| 105 | } |
||
| 106 | |||
| 107 | if (!$node instanceof FeatureNode) { |
||
| 108 | throw new ParserException(sprintf( |
||
| 109 | 'Expected Feature, but got %s on line: %d%s', |
||
| 110 | $node->getKeyword(), |
||
|
|
|||
| 111 | $node->getLine(), |
||
| 112 | $this->file ? ' in file: ' . $this->file : '' |
||
| 113 | )); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | 44 | return $feature; |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Returns next token if it's type equals to expected. |
||
| 122 | * |
||
| 123 | * @param string $type Token type |
||
| 124 | * |
||
| 125 | * @return array |
||
| 126 | * |
||
| 127 | * @throws Exception\ParserException |
||
| 128 | */ |
||
| 129 | 53 | protected function expectTokenType($type) |
|
| 130 | { |
||
| 131 | 53 | $types = (array) $type; |
|
| 132 | 53 | if (in_array($this->predictTokenType(), $types)) { |
|
| 133 | 53 | return $this->lexer->getAdvancedToken(); |
|
| 134 | } |
||
| 135 | |||
| 136 | 1 | $token = $this->lexer->predictToken(); |
|
| 137 | |||
| 138 | 1 | throw new ParserException(sprintf( |
|
| 139 | 1 | 'Expected %s token, but got %s on line: %d%s', |
|
| 140 | 1 | implode(' or ', $types), |
|
| 141 | 1 | $this->predictTokenType(), |
|
| 142 | 1 | $token['line'], |
|
| 143 | 1 | $this->file ? ' in file: ' . $this->file : '' |
|
| 144 | 1 | )); |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Returns next token if it's type equals to expected. |
||
| 149 | * |
||
| 150 | * @param string $type Token type |
||
| 151 | * |
||
| 152 | * @return null|array |
||
| 153 | */ |
||
| 154 | 35 | protected function acceptTokenType($type) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Returns next token type without real input reading (prediction). |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | 53 | protected function predictTokenType() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Parses current expression & returns Node. |
||
| 177 | * |
||
| 178 | * @return string|FeatureNode|BackgroundNode|ScenarioNode|OutlineNode|TableNode|StepNode |
||
| 179 | * |
||
| 180 | * @throws ParserException |
||
| 181 | */ |
||
| 182 | 53 | protected function parseExpression() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Parses feature token & returns it's node. |
||
| 220 | * |
||
| 221 | * @return FeatureNode |
||
| 222 | * |
||
| 223 | * @throws ParserException |
||
| 224 | */ |
||
| 225 | 52 | protected function parseFeature() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Parses background token & returns it's node. |
||
| 293 | * |
||
| 294 | * @return BackgroundNode |
||
| 295 | * |
||
| 296 | * @throws ParserException |
||
| 297 | */ |
||
| 298 | 10 | protected function parseBackground() |
|
| 299 | { |
||
| 300 | 10 | $token = $this->expectTokenType('Background'); |
|
| 301 | |||
| 302 | 10 | $title = trim($token['value']); |
|
| 303 | 10 | $keyword = $token['keyword']; |
|
| 304 | 10 | $line = $token['line']; |
|
| 305 | |||
| 306 | 10 | if (count($this->popTags())) { |
|
| 307 | 1 | throw new ParserException(sprintf( |
|
| 308 | 1 | 'Background can not be tagged, but it is on line: %d%s', |
|
| 309 | 1 | $line, |
|
| 310 | 1 | $this->file ? ' in file: ' . $this->file : '' |
|
| 311 | 1 | )); |
|
| 312 | } |
||
| 313 | |||
| 314 | // Parse description and steps |
||
| 315 | 9 | $steps = array(); |
|
| 316 | 9 | $allowedTokenTypes = array('Step', 'Newline', 'Text', 'Comment'); |
|
| 317 | 9 | while (in_array($this->predictTokenType(), $allowedTokenTypes)) { |
|
| 318 | 9 | $node = $this->parseExpression(); |
|
| 319 | |||
| 320 | 9 | if ($node instanceof StepNode) { |
|
| 321 | 8 | $steps[] = $this->normalizeStepNodeKeywordType($node, $steps); |
|
| 322 | 8 | continue; |
|
| 323 | } |
||
| 324 | |||
| 325 | 4 | View Code Duplication | if (!count($steps) && is_string($node)) { |
| 326 | 4 | $text = preg_replace('/^\s{0,' . ($token['indent'] + 2) . '}|\s*$/', '', $node); |
|
| 327 | 4 | $title .= "\n" . $text; |
|
| 328 | 4 | continue; |
|
| 329 | } |
||
| 330 | |||
| 331 | if ("\n" === $node) { |
||
| 332 | continue; |
||
| 333 | } |
||
| 334 | |||
| 335 | View Code Duplication | if (is_string($node)) { |
|
| 336 | throw new ParserException(sprintf( |
||
| 337 | 'Expected Step, but got text: "%s"%s', |
||
| 338 | $node, |
||
| 339 | $this->file ? ' in file: ' . $this->file : '' |
||
| 340 | )); |
||
| 341 | } |
||
| 342 | |||
| 343 | View Code Duplication | if (!$node instanceof StepNode) { |
|
| 344 | throw new ParserException(sprintf( |
||
| 345 | 'Expected Step, but got %s on line: %d%s', |
||
| 346 | $node->getNodeType(), |
||
| 347 | $node->getLine(), |
||
| 348 | $this->file ? ' in file: ' . $this->file : '' |
||
| 349 | )); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | 9 | return new BackgroundNode(rtrim($title) ?: null, $steps, $keyword, $line); |
|
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Parses scenario token & returns it's node. |
||
| 358 | * |
||
| 359 | * @return ScenarioNode |
||
| 360 | * |
||
| 361 | * @throws ParserException |
||
| 362 | */ |
||
| 363 | 39 | protected function parseScenario() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Parses scenario outline token & returns it's node. |
||
| 415 | * |
||
| 416 | * @return OutlineNode |
||
| 417 | * |
||
| 418 | * @throws ParserException |
||
| 419 | */ |
||
| 420 | 17 | protected function parseOutline() |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Parses step token & returns it's node. |
||
| 487 | * |
||
| 488 | * @return StepNode |
||
| 489 | */ |
||
| 490 | 40 | protected function parseStep() |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Parses examples table node. |
||
| 518 | * |
||
| 519 | * @return ExampleTableNode |
||
| 520 | */ |
||
| 521 | 16 | protected function parseExamples() |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Parses table token & returns it's node. |
||
| 532 | * |
||
| 533 | * @return TableNode |
||
| 534 | */ |
||
| 535 | 3 | protected function parseTable() |
|
| 536 | { |
||
| 537 | 3 | return new TableNode($this->parseTableRows()); |
|
| 538 | } |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Parses PyString token & returns it's node. |
||
| 542 | * |
||
| 543 | * @return PyStringNode |
||
| 544 | */ |
||
| 545 | 10 | protected function parsePyString() |
|
| 546 | { |
||
| 547 | 10 | $token = $this->expectTokenType('PyStringOp'); |
|
| 548 | |||
| 549 | 10 | $line = $token['line']; |
|
| 550 | |||
| 551 | 10 | $strings = array(); |
|
| 552 | 10 | while ('PyStringOp' !== ($predicted = $this->predictTokenType()) && 'Text' === $predicted) { |
|
| 553 | 10 | $token = $this->expectTokenType('Text'); |
|
| 554 | |||
| 555 | 10 | $strings[] = $token['value']; |
|
| 556 | 10 | } |
|
| 557 | |||
| 558 | 10 | $this->expectTokenType('PyStringOp'); |
|
| 559 | |||
| 560 | 9 | return new PyStringNode($strings, $line); |
|
| 561 | } |
||
| 562 | |||
| 563 | /** |
||
| 564 | * Parses tags. |
||
| 565 | * |
||
| 566 | * @return BackgroundNode|FeatureNode|OutlineNode|ScenarioNode|StepNode|TableNode|string |
||
| 567 | */ |
||
| 568 | 5 | protected function parseTags() |
|
| 575 | |||
| 576 | /** |
||
| 577 | * Returns current set of tags and clears tag buffer. |
||
| 578 | * |
||
| 579 | * @return array |
||
| 580 | */ |
||
| 581 | 52 | protected function popTags() |
|
| 588 | |||
| 589 | /** |
||
| 590 | * Parses next text line & returns it. |
||
| 591 | * |
||
| 592 | * @return string |
||
| 593 | */ |
||
| 594 | 29 | protected function parseText() |
|
| 600 | |||
| 601 | /** |
||
| 602 | * Parses next newline & returns \n. |
||
| 603 | * |
||
| 604 | * @return string |
||
| 605 | */ |
||
| 606 | 45 | protected function parseNewline() |
|
| 612 | |||
| 613 | /** |
||
| 614 | * Parses next comment token & returns it's string content. |
||
| 615 | * |
||
| 616 | * @return BackgroundNode|FeatureNode|OutlineNode|ScenarioNode|StepNode|TableNode|string |
||
| 617 | */ |
||
| 618 | 8 | protected function parseComment() |
|
| 624 | |||
| 625 | /** |
||
| 626 | * Parses language block and updates lexer configuration based on it. |
||
| 627 | * |
||
| 628 | * @return BackgroundNode|FeatureNode|OutlineNode|ScenarioNode|StepNode|TableNode|string |
||
| 629 | * |
||
| 630 | * @throws ParserException |
||
| 631 | */ |
||
| 632 | 9 | protected function parseLanguage() |
|
| 633 | { |
||
| 634 | 9 | $token = $this->expectTokenType('Language'); |
|
| 635 | |||
| 636 | 9 | if (null === $this->languageSpecifierLine) { |
|
| 637 | 9 | $this->lexer->analyse($this->input, $token['value']); |
|
| 638 | 9 | $this->languageSpecifierLine = $token['line']; |
|
| 639 | 9 | } elseif ($token['line'] !== $this->languageSpecifierLine) { |
|
| 640 | 1 | throw new ParserException(sprintf( |
|
| 641 | 1 | 'Ambiguous language specifiers on lines: %d and %d%s', |
|
| 642 | 1 | $this->languageSpecifierLine, |
|
| 643 | 1 | $token['line'], |
|
| 644 | 1 | $this->file ? ' in file: ' . $this->file : '' |
|
| 645 | 1 | )); |
|
| 646 | } |
||
| 647 | |||
| 648 | 9 | return $this->parseExpression(); |
|
| 649 | } |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Parses the rows of a table |
||
| 653 | * |
||
| 654 | * @return string[][] |
||
| 655 | */ |
||
| 656 | 17 | private function parseTableRows() |
|
| 657 | { |
||
| 658 | 17 | $table = array(); |
|
| 659 | 17 | while (in_array($predicted = $this->predictTokenType(), array('TableRow', 'Newline', 'Comment'))) { |
|
| 660 | 17 | if ('Comment' === $predicted || 'Newline' === $predicted) { |
|
| 661 | 11 | $this->acceptTokenType($predicted); |
|
| 662 | 11 | continue; |
|
| 663 | } |
||
| 664 | |||
| 665 | 17 | $token = $this->expectTokenType('TableRow'); |
|
| 666 | |||
| 667 | 17 | $table[$token['line']] = $token['columns']; |
|
| 668 | 17 | } |
|
| 669 | |||
| 670 | 17 | return $table; |
|
| 671 | } |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Changes step node type for types But, And to type of previous step if it exists else sets to Given |
||
| 675 | * |
||
| 676 | * @param StepNode $node |
||
| 677 | * @param StepNode[] $steps |
||
| 678 | * @return StepNode |
||
| 679 | */ |
||
| 680 | 39 | private function normalizeStepNodeKeywordType(StepNode $node, array $steps = array()) |
|
| 699 | } |
||
| 700 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: