Complex classes like Environment 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 Environment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
30 | final class Environment implements EnvironmentInterface, ConfigurableEnvironmentInterface |
||
31 | { |
||
32 | /** |
||
33 | * @var ExtensionInterface[] |
||
34 | */ |
||
35 | private $extensions = []; |
||
36 | |||
37 | /** |
||
38 | * @var ExtensionInterface[] |
||
39 | */ |
||
40 | private $uninitializedExtensions = []; |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | private $extensionsInitialized = false; |
||
46 | |||
47 | /** |
||
48 | * @var PrioritizedList<BlockParserInterface> |
||
49 | */ |
||
50 | private $blockParsers; |
||
51 | |||
52 | /** |
||
53 | * @var PrioritizedList<InlineParserInterface> |
||
54 | */ |
||
55 | private $inlineParsers; |
||
56 | |||
57 | /** |
||
58 | * @var array<string, PrioritizedList<InlineParserInterface>> |
||
59 | */ |
||
60 | private $inlineParsersByCharacter = []; |
||
61 | |||
62 | /** |
||
63 | * @var DelimiterProcessorCollection |
||
64 | */ |
||
65 | private $delimiterProcessors; |
||
66 | |||
67 | /** |
||
68 | * @var array<string, PrioritizedList<BlockRendererInterface>> |
||
69 | */ |
||
70 | private $blockRenderersByClass = []; |
||
71 | |||
72 | /** |
||
73 | * @var array<string, PrioritizedList<InlineRendererInterface>> |
||
74 | */ |
||
75 | private $inlineRenderersByClass = []; |
||
76 | |||
77 | /** |
||
78 | * @var array<string, PrioritizedList<callable>> |
||
79 | */ |
||
80 | private $listeners = []; |
||
81 | |||
82 | /** |
||
83 | * @var Configuration |
||
84 | */ |
||
85 | private $config; |
||
86 | |||
87 | /** |
||
88 | * @var string |
||
89 | */ |
||
90 | private $inlineParserCharacterRegex; |
||
91 | |||
92 | 2181 | public function __construct(array $config = []) |
|
93 | { |
||
94 | 2181 | $this->config = new Configuration($config); |
|
95 | |||
96 | 2181 | $this->blockParsers = new PrioritizedList(); |
|
97 | 2181 | $this->inlineParsers = new PrioritizedList(); |
|
98 | 2181 | $this->delimiterProcessors = new DelimiterProcessorCollection(); |
|
99 | 2181 | } |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 2085 | public function mergeConfig(array $config = []) |
|
110 | |||
111 | /** |
||
112 | * {@inheritdoc} |
||
113 | */ |
||
114 | 6 | public function setConfig(array $config = []) |
|
120 | |||
121 | /** |
||
122 | * {@inheritdoc} |
||
123 | */ |
||
124 | 2088 | public function getConfig($key = null, $default = null) |
|
128 | |||
129 | /** |
||
130 | * {@inheritdoc} |
||
131 | */ |
||
132 | 2082 | public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
141 | |||
142 | /** |
||
143 | * {@inheritdoc} |
||
144 | */ |
||
145 | 2082 | public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
162 | |||
163 | /** |
||
164 | * {@inheritdoc} |
||
165 | */ |
||
166 | 2079 | public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface |
|
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | 2079 | public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
191 | |||
192 | /** |
||
193 | * {@inheritdoc} |
||
194 | */ |
||
195 | 2079 | public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | 2091 | public function getBlockParsers(): iterable |
|
220 | |||
221 | /** |
||
222 | 2031 | * {@inheritdoc} |
|
223 | */ |
||
224 | 2031 | public function getInlineParsersForCharacter(string $character): iterable |
|
236 | 2034 | ||
237 | /** |
||
238 | 2034 | * {@inheritdoc} |
|
239 | */ |
||
240 | 2034 | public function getDelimiterProcessors(): DelimiterProcessorCollection |
|
248 | 2079 | ||
249 | /** |
||
250 | 2079 | * {@inheritdoc} |
|
251 | 6 | */ |
|
252 | public function getBlockRenderersForClass(string $blockClass): iterable |
||
264 | 1794 | ||
265 | 9 | /** |
|
266 | * {@inheritdoc} |
||
267 | */ |
||
268 | 1785 | public function getInlineRenderersForClass(string $inlineClass): iterable |
|
280 | |||
281 | /** |
||
282 | * Get all registered extensions |
||
283 | * |
||
284 | * @return ExtensionInterface[] |
||
285 | */ |
||
286 | public function getExtensions(): iterable |
||
290 | 2088 | ||
291 | /** |
||
292 | 2085 | * Add a single extension |
|
293 | 2085 | * |
|
294 | * @param ExtensionInterface $extension |
||
295 | 2085 | * |
|
296 | * @return $this |
||
297 | */ |
||
298 | 2145 | public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface |
|
307 | 2073 | ||
308 | 2073 | private function initializeExtensions() |
|
324 | |||
325 | private function injectEnvironmentAndConfigurationIfNeeded($object) |
||
335 | |||
336 | 2079 | /** |
|
337 | 2079 | * @return Environment |
|
338 | 2079 | */ |
|
339 | 1386 | public static function createCommonMarkEnvironment(): Environment |
|
356 | |||
357 | 1848 | /** |
|
358 | * {@inheritdoc} |
||
359 | */ |
||
360 | public function getInlineParserCharacterRegex(): string |
||
364 | |||
365 | 6 | /** |
|
366 | * {@inheritdoc} |
||
367 | 6 | */ |
|
368 | 6 | public function addEventListener(string $eventClass, callable $listener, int $priority = 0): ConfigurableEnvironmentInterface |
|
380 | |||
381 | 2073 | /** |
|
382 | * {@inheritdoc} |
||
383 | 2073 | */ |
|
384 | public function dispatch(AbstractEvent $event): void |
||
400 | |||
401 | 2145 | private function buildInlineParserCharacterRegex() |
|
416 | |||
417 | 2151 | /** |
|
418 | 24 | * @param string $message |
|
419 | * |
||
420 | 2127 | * @throws \RuntimeException |
|
421 | */ |
||
422 | private function assertUninitialized(string $message) |
||
428 | } |
||
429 |