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 | 211 | 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 | 209 | public function parse($input, $file = null) |
|
| 63 | { |
||
| 64 | 209 | $this->languageSpecifierLine = null; |
|
| 65 | 209 | $this->input = $input; |
|
| 66 | 209 | $this->file = $file; |
|
| 67 | 209 | $this->tags = array(); |
|
| 68 | |||
| 69 | try { |
||
| 70 | 209 | $this->lexer->analyse($this->input, 'en'); |
|
| 71 | } 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 | 209 | $feature = null; |
|
| 80 | 209 | while ('EOS' !== ($predicted = $this->predictTokenType())) { |
|
| 81 | 209 | $node = $this->parseExpression(); |
|
| 82 | |||
| 83 | 201 | if (null === $node || "\n" === $node) { |
|
| 84 | 1 | continue; |
|
| 85 | } |
||
| 86 | |||
| 87 | 200 | if (!$feature && $node instanceof FeatureNode) { |
|
| 88 | 200 | $feature = $node; |
|
| 89 | 200 | 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 | 200 | 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 | 209 | protected function expectTokenType($type) |
|
| 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 | 199 | protected function acceptTokenType($type) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Returns next token type without real input reading (prediction). |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | 209 | 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 | 209 | protected function parseExpression() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Parses feature token & returns it's node. |
||
| 220 | * |
||
| 221 | * @return FeatureNode |
||
| 222 | * |
||
| 223 | * @throws ParserException |
||
| 224 | */ |
||
| 225 | 208 | protected function parseFeature() |
|
| 290 | |||
| 291 | /** |
||
| 292 | * Parses background token & returns it's node. |
||
| 293 | * |
||
| 294 | * @return BackgroundNode |
||
| 295 | * |
||
| 296 | * @throws ParserException |
||
| 297 | */ |
||
| 298 | 194 | View Code Duplication | protected function parseBackground() |
| 361 | |||
| 362 | /** |
||
| 363 | * Parses scenario token & returns it's node. |
||
| 364 | * |
||
| 365 | * @return ScenarioNode |
||
| 366 | * |
||
| 367 | * @throws ParserException |
||
| 368 | */ |
||
| 369 | 203 | protected function parseScenario() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Parses scenario outline token & returns it's node. |
||
| 421 | * |
||
| 422 | * @return OutlineNode |
||
| 423 | * |
||
| 424 | * @throws ParserException |
||
| 425 | */ |
||
| 426 | 196 | View Code Duplication | protected function parseOutline() |
| 490 | |||
| 491 | /** |
||
| 492 | * Parses step token & returns it's node. |
||
| 493 | * |
||
| 494 | * @return StepNode |
||
| 495 | */ |
||
| 496 | 202 | protected function parseStep() |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Parses examples table node. |
||
| 524 | * |
||
| 525 | * @return ExampleTableNode |
||
| 526 | */ |
||
| 527 | 196 | protected function parseExamples() |
|
| 535 | |||
| 536 | /** |
||
| 537 | * Parses table token & returns it's node. |
||
| 538 | * |
||
| 539 | * @return TableNode |
||
| 540 | */ |
||
| 541 | protected function parseTable() |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Parses PyString token & returns it's node. |
||
| 548 | * |
||
| 549 | * @return PyStringNode |
||
| 550 | */ |
||
| 551 | 4 | protected function parsePyString() |
|
| 568 | |||
| 569 | /** |
||
| 570 | * Parses tags. |
||
| 571 | * |
||
| 572 | * @return BackgroundNode|FeatureNode|OutlineNode|ScenarioNode|StepNode|TableNode|string |
||
| 573 | */ |
||
| 574 | 3 | protected function parseTags() |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Returns current set of tags and clears tag buffer. |
||
| 584 | * |
||
| 585 | * @return array |
||
| 586 | */ |
||
| 587 | 208 | protected function popTags() |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Parses next text line & returns it. |
||
| 597 | * |
||
| 598 | * @return string |
||
| 599 | */ |
||
| 600 | 196 | protected function parseText() |
|
| 606 | |||
| 607 | /** |
||
| 608 | * Parses next newline & returns \n. |
||
| 609 | * |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | 204 | protected function parseNewline() |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Parses next comment token & returns it's string content. |
||
| 621 | * |
||
| 622 | * @return BackgroundNode|FeatureNode|OutlineNode|ScenarioNode|StepNode|TableNode|string |
||
| 623 | */ |
||
| 624 | protected function parseComment() |
||
| 630 | |||
| 631 | /** |
||
| 632 | * Parses language block and updates lexer configuration based on it. |
||
| 633 | * |
||
| 634 | * @return BackgroundNode|FeatureNode|OutlineNode|ScenarioNode|StepNode|TableNode|string |
||
| 635 | * |
||
| 636 | * @throws ParserException |
||
| 637 | */ |
||
| 638 | 185 | protected function parseLanguage() |
|
| 656 | |||
| 657 | /** |
||
| 658 | * Parses the rows of a table |
||
| 659 | * |
||
| 660 | * @return string[][] |
||
| 661 | */ |
||
| 662 | 196 | private function parseTableRows() |
|
| 678 | |||
| 679 | /** |
||
| 680 | * Changes step node type for types But, And to type of previous step if it exists else sets to Given |
||
| 681 | * |
||
| 682 | * @param StepNode $node |
||
| 683 | * @param StepNode[] $steps |
||
| 684 | * @return StepNode |
||
| 685 | */ |
||
| 686 | 201 | private function normalizeStepNodeKeywordType(StepNode $node, array $steps = array()) |
|
| 705 | } |
||
| 706 |
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: