Complex classes like DocParser 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 DocParser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | final class DocParser implements DocParserInterface |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var EnvironmentInterface |
||
| 29 | */ |
||
| 30 | private $environment; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var InlineParserEngine |
||
| 34 | */ |
||
| 35 | private $inlineParserEngine; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int|float |
||
| 39 | */ |
||
| 40 | private $maxNestingLevel; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param EnvironmentInterface $environment |
||
| 44 | */ |
||
| 45 | 2442 | public function __construct(EnvironmentInterface $environment) |
|
| 46 | { |
||
| 47 | 2442 | $this->environment = $environment; |
|
| 48 | 2442 | $this->inlineParserEngine = new InlineParserEngine($environment); |
|
| 49 | 2442 | $this->maxNestingLevel = $environment->getConfig('max_nesting_level', \INF); |
|
| 50 | 2442 | } |
|
| 51 | |||
| 52 | /** |
||
| 53 | * @param string $input |
||
| 54 | * |
||
| 55 | * @return string[] |
||
| 56 | */ |
||
| 57 | 2427 | private function preProcessInput(string $input): array |
|
| 58 | { |
||
| 59 | /** @var string[] $lines */ |
||
| 60 | 2427 | $lines = \preg_split('/\r\n|\n|\r/', $input); |
|
| 61 | |||
| 62 | // Remove any newline which appears at the very end of the string. |
||
| 63 | // We've already split the document by newlines, so we can simply drop |
||
| 64 | // any empty element which appears on the end. |
||
| 65 | 2427 | if (\end($lines) === '') { |
|
| 66 | 2187 | \array_pop($lines); |
|
| 67 | } |
||
| 68 | |||
| 69 | 2427 | return $lines; |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $input |
||
| 74 | * |
||
| 75 | * @throws \RuntimeException |
||
| 76 | * |
||
| 77 | * @return Document |
||
| 78 | */ |
||
| 79 | 2433 | public function parse(string $input): Document |
|
| 80 | { |
||
| 81 | 2433 | $document = new Document(); |
|
| 82 | 2433 | $context = new Context($document, $this->environment); |
|
| 83 | |||
| 84 | 2433 | $this->assertValidUTF8($input); |
|
| 85 | 2427 | $lines = $this->preProcessInput($input); |
|
| 86 | 2427 | foreach ($lines as $line) { |
|
| 87 | 2427 | $context->setNextLine($line); |
|
| 88 | 2427 | $this->incorporateLine($context); |
|
| 89 | } |
||
| 90 | |||
| 91 | 2427 | $lineCount = \count($lines); |
|
| 92 | 2427 | while ($tip = $context->getTip()) { |
|
| 93 | 2427 | $tip->finalize($context, $lineCount); |
|
| 94 | } |
||
| 95 | |||
| 96 | 2427 | $this->processInlines($context); |
|
| 97 | |||
| 98 | 2427 | $this->environment->dispatch(new DocumentParsedEvent($document)); |
|
| 99 | |||
| 100 | 2427 | return $document; |
|
| 101 | } |
||
| 102 | |||
| 103 | 2427 | private function incorporateLine(ContextInterface $context) |
|
| 104 | { |
||
| 105 | 2427 | $context->getBlockCloser()->resetTip(); |
|
| 106 | 2427 | $context->setBlocksParsed(false); |
|
| 107 | |||
| 108 | 2427 | $cursor = new Cursor($context->getLine()); |
|
| 109 | |||
| 110 | 2427 | $this->resetContainer($context, $cursor); |
|
| 111 | 2427 | $context->getBlockCloser()->setLastMatchedContainer($context->getContainer()); |
|
| 112 | |||
| 113 | 2427 | $this->parseBlocks($context, $cursor); |
|
| 114 | |||
| 115 | // What remains at the offset is a text line. Add the text to the appropriate container. |
||
| 116 | // First check for a lazy paragraph continuation: |
||
| 117 | 2427 | if ($this->handleLazyParagraphContinuation($context, $cursor)) { |
|
| 118 | 36 | return; |
|
| 119 | } |
||
| 120 | |||
| 121 | // not a lazy continuation |
||
| 122 | // finalize any blocks not matched |
||
| 123 | 2427 | $context->getBlockCloser()->closeUnmatchedBlocks(); |
|
| 124 | |||
| 125 | // Determine whether the last line is blank, updating parents as needed |
||
| 126 | 2427 | $this->setAndPropagateLastLineBlank($context, $cursor); |
|
| 127 | |||
| 128 | // Handle any remaining cursor contents |
||
| 129 | 2427 | if ($context->getContainer() instanceof StringContainerInterface) { |
|
| 130 | 864 | $context->getContainer()->handleRemainingContents($context, $cursor); |
|
| 131 | 2175 | } elseif (!$cursor->isBlank()) { |
|
| 132 | // Create paragraph container for line |
||
| 133 | 2094 | $p = new Paragraph(); |
|
| 134 | 2094 | $context->addBlock($p); |
|
| 135 | 2094 | $cursor->advanceToNextNonSpaceOrTab(); |
|
| 136 | 2094 | $p->addLine($cursor->getRemainder()); |
|
| 137 | } |
||
| 138 | 2427 | } |
|
| 139 | |||
| 140 | 2427 | private function processInlines(ContextInterface $context) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Sets the container to the last open child (or its parent) |
||
| 158 | * |
||
| 159 | * @param ContextInterface $context |
||
| 160 | * @param Cursor $cursor |
||
| 161 | */ |
||
| 162 | 2427 | private function resetContainer(ContextInterface $context, Cursor $cursor) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * Parse blocks |
||
| 187 | * |
||
| 188 | * @param ContextInterface $context |
||
| 189 | * @param Cursor $cursor |
||
| 190 | */ |
||
| 191 | 2427 | private function parseBlocks(ContextInterface $context, Cursor $cursor) |
|
| 208 | |||
| 209 | /** |
||
| 210 | * @param ContextInterface $context |
||
| 211 | * @param Cursor $cursor |
||
| 212 | * |
||
| 213 | * @return bool |
||
| 214 | */ |
||
| 215 | 2427 | private function handleLazyParagraphContinuation(ContextInterface $context, Cursor $cursor): bool |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param ContextInterface $context |
||
| 235 | * @param Cursor $cursor |
||
| 236 | */ |
||
| 237 | 2427 | private function setAndPropagateLastLineBlank(ContextInterface $context, Cursor $cursor) |
|
| 255 | |||
| 256 | 2433 | private function assertValidUTF8(string $input) |
|
| 262 | } |
||
| 263 |