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 | final class DocParser implements DocParserInterface |
||
24 | { |
||
25 | /** |
||
26 | * @var EnvironmentInterface |
||
27 | */ |
||
28 | private $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 | 2079 | public function __construct(EnvironmentInterface $environment) |
|
49 | |||
50 | /** |
||
51 | * @return EnvironmentInterface |
||
52 | * |
||
53 | * @deprecated |
||
54 | */ |
||
55 | 3 | public function getEnvironment(): EnvironmentInterface |
|
56 | { |
||
57 | 3 | @trigger_error('DocParser::getEnvironment() has been deprecated and will be removed in a future release.', E_USER_DEPRECATED); |
|
1 ignored issue
–
show
|
|||
58 | |||
59 | 3 | return $this->environment; |
|
60 | } |
||
61 | |||
62 | /** |
||
63 | * @param string $input |
||
64 | * |
||
65 | * @return string[] |
||
66 | */ |
||
67 | 2067 | private function preProcessInput(string $input): array |
|
80 | |||
81 | /** |
||
82 | * @param string $input |
||
83 | * |
||
84 | * @return Document |
||
85 | */ |
||
86 | 2067 | public function parse(string $input): Document |
|
107 | |||
108 | 2067 | private function incorporateLine(ContextInterface $context) |
|
144 | |||
145 | 2067 | private function processDocument(ContextInterface $context) |
|
151 | |||
152 | 2067 | private function processInlines(ContextInterface $context) |
|
167 | |||
168 | /** |
||
169 | * Sets the container to the last open child (or its parent) |
||
170 | * |
||
171 | * @param ContextInterface $context |
||
172 | * @param Cursor $cursor |
||
173 | */ |
||
174 | 2067 | private function resetContainer(ContextInterface $context, Cursor $cursor) |
|
196 | |||
197 | /** |
||
198 | * Parse blocks |
||
199 | * |
||
200 | * @param ContextInterface $context |
||
201 | * @param Cursor $cursor |
||
202 | */ |
||
203 | 2067 | private function parseBlocks(ContextInterface $context, Cursor $cursor) |
|
220 | |||
221 | /** |
||
222 | * @param ContextInterface $context |
||
223 | * @param Cursor $cursor |
||
224 | * |
||
225 | * @return bool |
||
226 | */ |
||
227 | 2067 | private function handleLazyParagraphContinuation(ContextInterface $context, Cursor $cursor): bool |
|
244 | |||
245 | /** |
||
246 | * @param ContextInterface $context |
||
247 | * @param Cursor $cursor |
||
248 | */ |
||
249 | 2067 | private function setAndPropagateLastLineBlank(ContextInterface $context, Cursor $cursor) |
|
267 | } |
||
268 |
If you suppress an error, we recommend checking for the error condition explicitly: