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) |
|
143 | |||
144 | 2028 | private function processDocument(ContextInterface $context) |
|
150 | |||
151 | 2028 | private function processInlines(ContextInterface $context) |
|
166 | |||
167 | /** |
||
168 | * Sets the container to the last open child (or its parent) |
||
169 | * |
||
170 | * @param ContextInterface $context |
||
171 | * @param Cursor $cursor |
||
172 | */ |
||
173 | 2028 | private function resetContainer(ContextInterface $context, Cursor $cursor) |
|
195 | |||
196 | /** |
||
197 | * Parse blocks |
||
198 | * |
||
199 | * @param ContextInterface $context |
||
200 | * @param Cursor $cursor |
||
201 | */ |
||
202 | 2028 | private function parseBlocks(ContextInterface $context, Cursor $cursor) |
|
219 | |||
220 | /** |
||
221 | * @param ContextInterface $context |
||
222 | * @param Cursor $cursor |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | 2028 | private function isLazyParagraphContinuation(ContextInterface $context, Cursor $cursor): bool |
|
233 | |||
234 | /** |
||
235 | * @param ContextInterface $context |
||
236 | * @param Cursor $cursor |
||
237 | */ |
||
238 | 2028 | private function setAndPropagateLastLineBlank(ContextInterface $context, Cursor $cursor) |
|
256 | } |
||
257 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: