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) |
|
44 | { |
||
45 | 1950 | $this->environment = $environment; |
|
46 | 1950 | $this->inlineParserEngine = new InlineParserEngine($environment); |
|
47 | 1950 | $this->maxNestingLevel = $environment->getConfig('max_nesting_level', INF); |
|
48 | 1950 | } |
|
49 | |||
50 | /** |
||
51 | * @return EnvironmentInterface |
||
52 | */ |
||
53 | 1950 | public function getEnvironment(): EnvironmentInterface |
|
57 | |||
58 | /** |
||
59 | * @param string $input |
||
60 | * |
||
61 | * @return string[] |
||
62 | */ |
||
63 | 1941 | private function preProcessInput(string $input): array |
|
76 | |||
77 | /** |
||
78 | * @param string $input |
||
79 | * |
||
80 | * @return Document |
||
81 | */ |
||
82 | 1941 | public function parse(string $input): Document |
|
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) |
|
193 | |||
194 | /** |
||
195 | * Parse blocks |
||
196 | * |
||
197 | * @param ContextInterface $context |
||
198 | * @param Cursor $cursor |
||
199 | */ |
||
200 | 1938 | private function parseBlocks(ContextInterface $context, Cursor $cursor) |
|
217 | |||
218 | /** |
||
219 | * @param ContextInterface $context |
||
220 | * @param Cursor $cursor |
||
221 | * |
||
222 | * @return bool |
||
223 | */ |
||
224 | 1938 | private function isLazyParagraphContinuation(ContextInterface $context, Cursor $cursor): bool |
|
231 | |||
232 | /** |
||
233 | * @param ContextInterface $context |
||
234 | * @param Cursor $cursor |
||
235 | */ |
||
236 | 1938 | private function setAndPropagateLastLineBlank(ContextInterface $context, Cursor $cursor) |
|
254 | } |
||
255 |