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 |
||
| 22 | class DocParser |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var Environment |
||
| 26 | */ |
||
| 27 | protected $environment; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var InlineParserEngine |
||
| 31 | */ |
||
| 32 | private $inlineParserEngine; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int|float |
||
| 36 | */ |
||
| 37 | private $maxNestingLevel; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param Environment $environment |
||
| 41 | */ |
||
| 42 | 1950 | public function __construct(Environment $environment) |
|
| 43 | 2 | { |
|
| 44 | 1950 | $this->environment = $environment; |
|
| 45 | 1950 | $this->inlineParserEngine = new InlineParserEngine($environment); |
|
| 46 | 1950 | $this->maxNestingLevel = $environment->getConfig('max_nesting_level', INF); |
|
| 47 | 1950 | } |
|
| 48 | |||
| 49 | /** |
||
| 50 | * @return Environment |
||
| 51 | */ |
||
| 52 | 1950 | public function getEnvironment() |
|
| 56 | |||
| 57 | /** |
||
| 58 | * @param string $input |
||
| 59 | * |
||
| 60 | * @return string[] |
||
| 61 | */ |
||
| 62 | 1941 | private function preProcessInput($input) |
|
| 63 | { |
||
| 64 | 1941 | $lines = preg_split('/\r\n|\n|\r/', $input); |
|
| 65 | |||
| 66 | // Remove any newline which appears at the very end of the string. |
||
| 67 | // We've already split the document by newlines, so we can simply drop |
||
| 68 | // any empty element which appears on the end. |
||
| 69 | 1941 | if (end($lines) === '') { |
|
| 70 | 1926 | array_pop($lines); |
|
| 71 | 642 | } |
|
| 72 | |||
| 73 | 1941 | return $lines; |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @param string $input |
||
| 78 | * |
||
| 79 | * @return Document |
||
| 80 | */ |
||
| 81 | 1941 | public function parse($input) |
|
| 82 | { |
||
| 83 | 1941 | $context = new Context(new Document(), $this->getEnvironment()); |
|
| 84 | 1941 | $context->setEncoding(mb_detect_encoding($input, 'ASCII,UTF-8', true) ?: 'ISO-8859-1'); |
|
| 85 | |||
| 86 | 1941 | $lines = $this->preProcessInput($input); |
|
| 87 | 1941 | foreach ($lines as $line) { |
|
| 88 | 1938 | $context->setNextLine($line); |
|
| 89 | 1938 | $this->incorporateLine($context); |
|
| 90 | 647 | } |
|
| 91 | |||
| 92 | 1941 | $lineCount = count($lines); |
|
| 93 | 1941 | while ($tip = $context->getTip()) { |
|
| 94 | 1941 | $tip->finalize($context, $lineCount); |
|
| 95 | 647 | } |
|
| 96 | |||
| 97 | 1941 | $this->processInlines($context); |
|
| 98 | |||
| 99 | 1941 | $this->processDocument($context); |
|
| 100 | |||
| 101 | 1941 | return $context->getDocument(); |
|
| 102 | } |
||
| 103 | |||
| 104 | 1938 | private function incorporateLine(ContextInterface $context) |
|
| 105 | { |
||
| 106 | 1938 | $context->getBlockCloser()->resetTip(); |
|
| 107 | 1938 | $context->setBlocksParsed(false); |
|
| 108 | |||
| 109 | 1938 | $cursor = new Cursor($context->getLine(), $context->getEncoding()); |
|
| 110 | |||
| 111 | 1938 | $this->resetContainer($context, $cursor); |
|
| 112 | 1938 | $context->getBlockCloser()->setLastMatchedContainer($context->getContainer()); |
|
| 113 | |||
| 114 | 1938 | $this->parseBlocks($context, $cursor); |
|
| 115 | |||
| 116 | // What remains at the offset is a text line. Add the text to the appropriate container. |
||
| 117 | // First check for a lazy paragraph continuation: |
||
| 118 | 1938 | if ($this->isLazyParagraphContinuation($context, $cursor)) { |
|
| 119 | // lazy paragraph continuation |
||
| 120 | 33 | $context->getTip()->addLine($cursor->getRemainder()); |
|
| 121 | |||
| 122 | 33 | return; |
|
| 123 | } |
||
| 124 | |||
| 125 | // not a lazy continuation |
||
| 126 | // finalize any blocks not matched |
||
| 127 | 1938 | $context->getBlockCloser()->closeUnmatchedBlocks(); |
|
| 128 | |||
| 129 | // Determine whether the last line is blank, updating parents as needed |
||
| 130 | 1938 | $this->setAndPropagateLastLineBlank($context, $cursor); |
|
| 131 | |||
| 132 | // Handle any remaining cursor contents |
||
| 133 | 1938 | if ($context->getContainer()->acceptsLines()) { |
|
| 134 | 726 | $context->getContainer()->handleRemainingContents($context, $cursor); |
|
| 135 | 1774 | } elseif (!$cursor->isBlank()) { |
|
| 136 | // Create paragraph container for line |
||
| 137 | 1614 | $context->addBlock(new Paragraph()); |
|
| 138 | 1614 | $cursor->advanceToNextNonSpaceOrTab(); |
|
| 139 | 1614 | $context->getTip()->addLine($cursor->getRemainder()); |
|
| 140 | 538 | } |
|
| 141 | 1938 | } |
|
| 142 | |||
| 143 | 1941 | private function processDocument(ContextInterface $context) |
|
| 144 | { |
||
| 145 | 1941 | foreach ($this->getEnvironment()->getDocumentProcessors() as $documentProcessor) { |
|
| 146 | $documentProcessor->processDocument($context->getDocument()); |
||
| 147 | 647 | } |
|
| 148 | 1941 | } |
|
| 149 | |||
| 150 | 1941 | private function processInlines(ContextInterface $context) |
|
| 151 | { |
||
| 152 | 1941 | $walker = $context->getDocument()->walker(); |
|
| 153 | |||
| 154 | 1941 | while ($event = $walker->next()) { |
|
| 155 | 1941 | if (!$event->isEntering()) { |
|
| 156 | 1941 | continue; |
|
| 157 | } |
||
| 158 | |||
| 159 | 1941 | $node = $event->getNode(); |
|
| 160 | 1941 | if ($node instanceof InlineContainerInterface) { |
|
| 161 | 1662 | $this->inlineParserEngine->parse($node, $context->getDocument()->getReferenceMap()); |
|
| 162 | 554 | } |
|
| 163 | 647 | } |
|
| 164 | 1941 | } |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Sets the container to the last open child (or its parent) |
||
| 168 | * |
||
| 169 | * @param ContextInterface $context |
||
| 170 | * @param Cursor $cursor |
||
| 171 | */ |
||
| 172 | 1938 | private function resetContainer(ContextInterface $context, Cursor $cursor) |
|
| 173 | { |
||
| 174 | 1938 | $container = $context->getDocument(); |
|
| 175 | |||
| 176 | 1938 | while ($lastChild = $container->lastChild()) { |
|
| 177 | 1080 | if (!($lastChild instanceof AbstractBlock)) { |
|
| 178 | break; |
||
| 179 | } |
||
| 180 | |||
| 181 | 1080 | if (!$lastChild->isOpen()) { |
|
| 182 | 450 | break; |
|
| 183 | } |
||
| 184 | |||
| 185 | 1071 | $container = $lastChild; |
|
| 186 | 1071 | if (!$container->matchesNextLine($cursor)) { |
|
| 187 | 693 | $container = $container->parent(); // back up to the last matching block |
|
| 188 | 693 | break; |
|
| 189 | } |
||
| 190 | 250 | } |
|
| 191 | |||
| 192 | 1938 | $context->setContainer($container); |
|
| 193 | 1938 | } |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Parse blocks |
||
| 197 | * |
||
| 198 | * @param ContextInterface $context |
||
| 199 | * @param Cursor $cursor |
||
| 200 | */ |
||
| 201 | 1938 | private function parseBlocks(ContextInterface $context, Cursor $cursor) |
|
| 202 | { |
||
| 203 | 1938 | while (!$context->getContainer()->isCode() && !$context->getBlocksParsed()) { |
|
| 204 | 1938 | $parsed = false; |
|
| 205 | 1938 | foreach ($this->environment->getBlockParsers() as $parser) { |
|
| 206 | 1938 | if ($parser->parse($context, $cursor)) { |
|
| 207 | 804 | $parsed = true; |
|
| 208 | 1560 | break; |
|
| 209 | } |
||
| 210 | 646 | } |
|
| 211 | |||
| 212 | 1938 | if (!$parsed || $context->getContainer()->acceptsLines() || $context->getTip()->getDepth() >= $this->maxNestingLevel) { |
|
| 213 | 1911 | $context->setBlocksParsed(true); |
|
| 214 | 1911 | break; |
|
| 215 | } |
||
| 216 | 132 | } |
|
| 217 | 1938 | } |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @param ContextInterface $context |
||
| 221 | * @param Cursor $cursor |
||
| 222 | * |
||
| 223 | * @return bool |
||
| 224 | */ |
||
| 225 | 1938 | private function isLazyParagraphContinuation(ContextInterface $context, Cursor $cursor) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param ContextInterface $context |
||
| 235 | * @param Cursor $cursor |
||
| 236 | */ |
||
| 237 | 1938 | private function setAndPropagateLastLineBlank(ContextInterface $context, Cursor $cursor) |
|
| 238 | { |
||
| 239 | 1938 | $container = $context->getContainer(); |
|
| 240 | |||
| 255 | } |
||
| 256 |