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 |
||
| 23 | class DocParser |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var EnvironmentInterface |
||
| 27 | */ |
||
| 28 | protected $environment; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var InlineParserEngine |
||
| 32 | */ |
||
| 33 | private $inlineParserEngine; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int|float |
||
| 37 | */ |
||
| 38 | private $maxNestingLevel; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param EnvironmentInterface $environment |
||
| 42 | */ |
||
| 43 | 1950 | public function __construct(EnvironmentInterface $environment) |
|
| 49 | |||
| 50 | /** |
||
| 51 | * @return EnvironmentInterface |
||
| 52 | */ |
||
| 53 | 1950 | public function getEnvironment() |
|
| 57 | |||
| 58 | /** |
||
| 59 | * @param string $input |
||
| 60 | * |
||
| 61 | * @return string[] |
||
| 62 | */ |
||
| 63 | 1941 | private function preProcessInput($input) |
|
| 76 | |||
| 77 | /** |
||
| 78 | * @param string $input |
||
| 79 | * |
||
| 80 | * @return Document |
||
| 81 | */ |
||
| 82 | 1941 | public function parse($input) |
|
| 104 | |||
| 105 | 1938 | private function incorporateLine(ContextInterface $context) |
|
| 143 | |||
| 144 | 1941 | private function processDocument(ContextInterface $context) |
|
| 150 | |||
| 151 | 1941 | private function processInlines(ContextInterface $context, NodeWalker $walker) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * Sets the container to the last open child (or its parent) |
||
| 167 | * |
||
| 168 | * @param ContextInterface $context |
||
| 169 | * @param Cursor $cursor |
||
| 170 | */ |
||
| 171 | 1938 | private function resetContainer(ContextInterface $context, Cursor $cursor) |
|
| 172 | { |
||
| 173 | 1938 | $container = $context->getDocument(); |
|
| 174 | |||
| 175 | 1938 | while ($lastChild = $container->lastChild()) { |
|
| 176 | 1080 | if (!($lastChild instanceof AbstractBlock)) { |
|
| 177 | break; |
||
| 178 | } |
||
| 179 | |||
| 180 | 1080 | if (!$lastChild->isOpen()) { |
|
| 181 | 450 | break; |
|
| 182 | } |
||
| 183 | |||
| 184 | 1071 | $container = $lastChild; |
|
| 185 | 1071 | if (!$container->matchesNextLine($cursor)) { |
|
| 186 | 693 | $container = $container->parent(); // back up to the last matching block |
|
| 187 | 693 | break; |
|
| 188 | } |
||
| 189 | 250 | } |
|
| 190 | |||
| 191 | 1938 | $context->setContainer($container); |
|
| 192 | 1938 | } |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Parse blocks |
||
| 196 | * |
||
| 197 | * @param ContextInterface $context |
||
| 198 | * @param Cursor $cursor |
||
| 199 | */ |
||
| 200 | 1938 | private function parseBlocks(ContextInterface $context, Cursor $cursor) |
|
| 201 | { |
||
| 202 | 1938 | while (!$context->getContainer()->isCode() && !$context->getBlocksParsed()) { |
|
| 203 | 1938 | $parsed = false; |
|
| 204 | 1938 | foreach ($this->environment->getBlockParsers() as $parser) { |
|
| 205 | 1938 | if ($parser->parse($context, $cursor)) { |
|
| 206 | 804 | $parsed = true; |
|
| 207 | 1560 | break; |
|
| 208 | } |
||
| 209 | 646 | } |
|
| 210 | |||
| 211 | 1938 | if (!$parsed || $context->getContainer()->acceptsLines() || $context->getTip()->getDepth() >= $this->maxNestingLevel) { |
|
| 212 | 1911 | $context->setBlocksParsed(true); |
|
| 213 | 1911 | break; |
|
| 214 | } |
||
| 215 | 132 | } |
|
| 216 | 1938 | } |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param ContextInterface $context |
||
| 220 | * @param Cursor $cursor |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | 1938 | private function isLazyParagraphContinuation(ContextInterface $context, Cursor $cursor) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @param ContextInterface $context |
||
| 234 | * @param Cursor $cursor |
||
| 235 | */ |
||
| 236 | 1938 | private function setAndPropagateLastLineBlank(ContextInterface $context, Cursor $cursor) |
|
| 254 | } |
||
| 255 |