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 | 2037 | public function __construct(EnvironmentInterface $environment) |
|
44 | { |
||
45 | 2037 | $this->environment = $environment; |
|
46 | 2037 | $this->inlineParserEngine = new InlineParserEngine($environment); |
|
47 | 2037 | $this->maxNestingLevel = $environment->getConfig('max_nesting_level', INF); |
|
48 | 2037 | } |
|
49 | |||
50 | /** |
||
51 | * @return EnvironmentInterface |
||
52 | */ |
||
53 | 2037 | public function getEnvironment(): EnvironmentInterface |
|
57 | |||
58 | /** |
||
59 | * @param string $input |
||
60 | * |
||
61 | * @return string[] |
||
62 | */ |
||
63 | 2028 | private function preProcessInput(string $input): array |
|
76 | |||
77 | /** |
||
78 | * @param string $input |
||
79 | * |
||
80 | * @return Document |
||
81 | */ |
||
82 | 2028 | public function parse(string $input): Document |
|
104 | |||
105 | 2028 | private function incorporateLine(ContextInterface $context) |
|
106 | { |
||
107 | 2028 | $context->getBlockCloser()->resetTip(); |
|
108 | 2028 | $context->setBlocksParsed(false); |
|
109 | |||
110 | 2028 | $cursor = new Cursor($context->getLine(), $context->getEncoding()); |
|
111 | |||
112 | 2028 | $this->resetContainer($context, $cursor); |
|
113 | 2028 | $context->getBlockCloser()->setLastMatchedContainer($context->getContainer()); |
|
114 | |||
115 | 2028 | $this->parseBlocks($context, $cursor); |
|
116 | |||
117 | // What remains at the offset is a text line. Add the text to the appropriate container. |
||
118 | // First check for a lazy paragraph continuation: |
||
119 | 2028 | if ($this->handleLazyParagraphContinuation($context, $cursor)) { |
|
120 | 36 | return; |
|
121 | } |
||
122 | |||
123 | // not a lazy continuation |
||
124 | // finalize any blocks not matched |
||
125 | 2028 | $context->getBlockCloser()->closeUnmatchedBlocks(); |
|
126 | |||
127 | // Determine whether the last line is blank, updating parents as needed |
||
128 | 2028 | $this->setAndPropagateLastLineBlank($context, $cursor); |
|
129 | |||
130 | // Handle any remaining cursor contents |
||
131 | 2028 | if ($context->getContainer() instanceof StringContainerInterface) { |
|
132 | 759 | $context->getContainer()->handleRemainingContents($context, $cursor); |
|
133 | 1779 | } elseif (!$cursor->isBlank()) { |
|
134 | // Create paragraph container for line |
||
135 | 1698 | $p = new Paragraph(); |
|
136 | 1698 | $context->addBlock($p); |
|
137 | 1698 | $cursor->advanceToNextNonSpaceOrTab(); |
|
138 | 1698 | $p->addLine($cursor->getRemainder()); |
|
139 | } |
||
140 | 2028 | } |
|
141 | |||
142 | 2028 | private function processDocument(ContextInterface $context) |
|
143 | { |
||
144 | 2028 | foreach ($this->getEnvironment()->getDocumentProcessors() as $documentProcessor) { |
|
145 | $documentProcessor->processDocument($context->getDocument()); |
||
146 | } |
||
147 | 2028 | } |
|
148 | |||
149 | 2028 | private function processInlines(ContextInterface $context) |
|
150 | { |
||
151 | 2028 | $walker = $context->getDocument()->walker(); |
|
152 | |||
153 | 2028 | while ($event = $walker->next()) { |
|
154 | 2028 | if (!$event->isEntering()) { |
|
155 | 2028 | continue; |
|
156 | } |
||
157 | |||
158 | 2028 | $node = $event->getNode(); |
|
159 | 2028 | if ($node instanceof InlineContainerInterface) { |
|
160 | 1743 | $this->inlineParserEngine->parse($node, $context->getDocument()->getReferenceMap()); |
|
161 | } |
||
162 | } |
||
163 | 2028 | } |
|
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 | 2028 | private function resetContainer(ContextInterface $context, Cursor $cursor) |
|
193 | |||
194 | /** |
||
195 | * Parse blocks |
||
196 | * |
||
197 | * @param ContextInterface $context |
||
198 | * @param Cursor $cursor |
||
199 | */ |
||
200 | 2028 | private function parseBlocks(ContextInterface $context, Cursor $cursor) |
|
217 | |||
218 | /** |
||
219 | * @param ContextInterface $context |
||
220 | * @param Cursor $cursor |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | 2028 | private function handleLazyParagraphContinuation(ContextInterface $context, Cursor $cursor): bool |
|
241 | |||
242 | /** |
||
243 | * @param ContextInterface $context |
||
244 | * @param Cursor $cursor |
||
245 | */ |
||
246 | 2028 | private function setAndPropagateLastLineBlank(ContextInterface $context, Cursor $cursor) |
|
264 | } |
||
265 |