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 |
||
24 | class DocParser |
||
25 | { |
||
26 | /** |
||
27 | * @var Environment |
||
28 | */ |
||
29 | protected $environment; |
||
30 | |||
31 | /** |
||
32 | * @var InlineParserEngine |
||
33 | */ |
||
34 | private $inlineParserEngine; |
||
35 | |||
36 | /** |
||
37 | * @param Environment $environment |
||
38 | */ |
||
39 | 1872 | public function __construct(Environment $environment) |
|
44 | |||
45 | /** |
||
46 | * @return Environment |
||
47 | */ |
||
48 | 1872 | public function getEnvironment() |
|
52 | |||
53 | /** |
||
54 | * @param string $input |
||
55 | * |
||
56 | * @return string[] |
||
57 | */ |
||
58 | 1872 | private function preProcessInput($input) |
|
71 | |||
72 | /** |
||
73 | * @param string $input |
||
74 | * |
||
75 | * @return Document |
||
76 | */ |
||
77 | 1872 | public function parse($input) |
|
78 | 3 | { |
|
79 | 1872 | $context = new Context(new Document(), $this->getEnvironment()); |
|
80 | |||
81 | 1872 | $lines = $this->preProcessInput($input); |
|
82 | 1872 | foreach ($lines as $line) { |
|
83 | 1872 | $context->setNextLine($line); |
|
84 | 1872 | $this->incorporateLine($context); |
|
85 | 1872 | } |
|
86 | |||
87 | 1872 | while ($context->getTip()) { |
|
88 | 1872 | $context->getTip()->finalize($context, count($lines)); |
|
89 | 1872 | } |
|
90 | |||
91 | 1872 | $this->processInlines($context, $context->getDocument()->walker()); |
|
92 | |||
93 | 1872 | $this->processDocument($context); |
|
94 | |||
95 | 1872 | return $context->getDocument(); |
|
96 | } |
||
97 | |||
98 | 1872 | private function incorporateLine(ContextInterface $context) |
|
141 | |||
142 | 1872 | private function processDocument(ContextInterface $context) |
|
143 | { |
||
144 | 1872 | foreach ($this->getEnvironment()->getDocumentProcessors() as $documentProcessor) { |
|
145 | $documentProcessor->processDocument($context->getDocument()); |
||
146 | 1872 | } |
|
147 | 1872 | } |
|
148 | |||
149 | 1872 | private function processInlines(ContextInterface $context, NodeWalker $walker) |
|
162 | |||
163 | /** |
||
164 | * Break out of all containing lists, resetting the tip of the |
||
165 | * document to the parent of the highest list, and finalizing |
||
166 | * all the lists. (This is used to implement the "two blank lines |
||
167 | * break out of all lists" feature.) |
||
168 | * |
||
169 | * @param ContextInterface $context |
||
170 | * @param AbstractBlock $block |
||
171 | */ |
||
172 | 39 | private function breakOutOfLists(ContextInterface $context, AbstractBlock $block) |
|
193 | |||
194 | /** |
||
195 | * Sets the container to the last open child (or its parent) |
||
196 | * |
||
197 | * @param ContextInterface $context |
||
198 | * @param Cursor $cursor |
||
199 | */ |
||
200 | 1872 | private function resetContainer(ContextInterface $context, Cursor $cursor) |
|
217 | |||
218 | /** |
||
219 | * Parse blocks |
||
220 | * |
||
221 | * @param ContextInterface $context |
||
222 | * @param Cursor $cursor |
||
223 | */ |
||
224 | 1872 | private function parseBlocks(ContextInterface $context, Cursor $cursor) |
|
240 | |||
241 | /** |
||
242 | * @param ContextInterface $context |
||
243 | * @param Cursor $cursor |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | 1872 | private function isLazyParagraphContinuation(ContextInterface $context, Cursor $cursor) |
|
254 | |||
255 | /** |
||
256 | * @param ContextInterface $context |
||
257 | * @param Cursor $cursor |
||
258 | */ |
||
259 | 1872 | private function setAndPropagateLastLineBlank(ContextInterface $context, $cursor) |
|
276 | } |
||
277 |