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 PrioritizedList<DocumentProcessorInterface> |
||
64 | */ |
||
65 | private $documentProcessors; |
||
66 | |||
67 | /** |
||
68 | * @var DelimiterProcessorCollection |
||
69 | */ |
||
70 | private $delimiterProcessors; |
||
71 | |||
72 | /** |
||
73 | * @var array<string, PrioritizedList<BlockRendererInterface>> |
||
74 | */ |
||
75 | private $blockRenderersByClass = []; |
||
76 | |||
77 | /** |
||
78 | * @var array<string, PrioritizedList<InlineRendererInterface>> |
||
79 | */ |
||
80 | private $inlineRenderersByClass = []; |
||
81 | |||
82 | /** |
||
83 | * @var array<string, PrioritizedList<callable>> |
||
84 | */ |
||
85 | private $listeners = []; |
||
86 | |||
87 | /** |
||
88 | * @var Configuration |
||
89 | */ |
||
90 | private $config; |
||
91 | 2175 | ||
92 | /** |
||
93 | 2175 | * @var string |
|
94 | */ |
||
95 | 2175 | private $inlineParserCharacterRegex; |
|
96 | 2175 | ||
97 | 2175 | public function __construct(array $config = []) |
|
98 | 2175 | { |
|
99 | 2175 | $this->config = new Configuration($config); |
|
100 | |||
101 | $this->blockParsers = new PrioritizedList(); |
||
102 | $this->inlineParsers = new PrioritizedList(); |
||
103 | $this->documentProcessors = new PrioritizedList(); |
||
104 | 2076 | $this->delimiterProcessors = new DelimiterProcessorCollection(); |
|
105 | } |
||
106 | 2076 | ||
107 | /** |
||
108 | 2073 | * {@inheritdoc} |
|
109 | 2073 | */ |
|
110 | public function mergeConfig(array $config = []) |
||
111 | { |
||
112 | $this->assertUninitialized('Failed to modify configuration.'); |
||
113 | |||
114 | 6 | $this->config->merge($config); |
|
115 | } |
||
116 | 6 | ||
117 | /** |
||
118 | 3 | * {@inheritdoc} |
|
119 | 3 | */ |
|
120 | public function setConfig(array $config = []) |
||
121 | { |
||
122 | $this->assertUninitialized('Failed to modify configuration.'); |
||
123 | |||
124 | 2079 | $this->config->replace($config); |
|
125 | } |
||
126 | 2079 | ||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | public function getConfig($key = null, $default = null) |
||
131 | { |
||
132 | 2073 | return $this->config->get($key, $default); |
|
133 | } |
||
134 | 2073 | ||
135 | /** |
||
136 | 2070 | * {@inheritdoc} |
|
137 | 2070 | */ |
|
138 | public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
||
139 | 2070 | { |
|
140 | $this->assertUninitialized('Failed to add block parser.'); |
||
141 | |||
142 | $this->blockParsers->add($parser, $priority); |
||
143 | $this->injectEnvironmentAndConfigurationIfNeeded($parser); |
||
144 | |||
145 | 2073 | return $this; |
|
146 | } |
||
147 | 2073 | ||
148 | /** |
||
149 | 2070 | * {@inheritdoc} |
|
150 | 2070 | */ |
|
151 | public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
||
152 | 2070 | { |
|
153 | 2070 | $this->assertUninitialized('Failed to add inline parser.'); |
|
154 | 2070 | ||
155 | $this->inlineParsers->add($parser, $priority); |
||
156 | $this->injectEnvironmentAndConfigurationIfNeeded($parser); |
||
157 | 2070 | ||
158 | foreach ($parser->getCharacters() as $character) { |
||
159 | if (!isset($this->inlineParsersByCharacter[$character])) { |
||
160 | 2070 | $this->inlineParsersByCharacter[$character] = new PrioritizedList(); |
|
161 | } |
||
162 | |||
163 | $this->inlineParsersByCharacter[$character]->add($parser, $priority); |
||
164 | } |
||
165 | |||
166 | 2070 | return $this; |
|
167 | } |
||
168 | 2070 | ||
169 | 2067 | /** |
|
170 | 2067 | * {@inheritdoc} |
|
171 | */ |
||
172 | 2067 | public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface |
|
173 | { |
||
174 | $this->assertUninitialized('Failed to add delimiter processor.'); |
||
175 | $this->delimiterProcessors->add($processor); |
||
176 | $this->injectEnvironmentAndConfigurationIfNeeded($processor); |
||
177 | |||
178 | 9 | return $this; |
|
179 | } |
||
180 | 9 | ||
181 | /** |
||
182 | 6 | * {@inheritdoc} |
|
183 | 6 | */ |
|
184 | public function addDocumentProcessor(DocumentProcessorInterface $processor, int $priority = 0): ConfigurableEnvironmentInterface |
||
185 | 6 | { |
|
186 | $this->assertUninitialized('Failed to add document processor.'); |
||
187 | |||
188 | $this->documentProcessors->add($processor, $priority); |
||
189 | $this->injectEnvironmentAndConfigurationIfNeeded($processor); |
||
190 | |||
191 | 2070 | return $this; |
|
192 | } |
||
193 | 2070 | ||
194 | /** |
||
195 | 2067 | * {@inheritdoc} |
|
196 | 2067 | */ |
|
197 | public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface |
||
198 | { |
||
199 | 2067 | $this->assertUninitialized('Failed to add block renderer.'); |
|
200 | 2067 | ||
201 | if (!isset($this->blockRenderersByClass[$blockClass])) { |
||
202 | 2067 | $this->blockRenderersByClass[$blockClass] = new PrioritizedList(); |
|
203 | } |
||
204 | |||
205 | $this->blockRenderersByClass[$blockClass]->add($blockRenderer, $priority); |
||
206 | $this->injectEnvironmentAndConfigurationIfNeeded($blockRenderer); |
||
207 | |||
208 | 2070 | return $this; |
|
209 | } |
||
210 | 2070 | ||
211 | /** |
||
212 | 2067 | * {@inheritdoc} |
|
213 | 2067 | */ |
|
214 | public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface |
||
215 | { |
||
216 | 2067 | $this->assertUninitialized('Failed to add inline renderer.'); |
|
217 | 2067 | ||
218 | if (!isset($this->inlineRenderersByClass[$inlineClass])) { |
||
219 | 2067 | $this->inlineRenderersByClass[$inlineClass] = new PrioritizedList(); |
|
220 | } |
||
221 | |||
222 | $this->inlineRenderersByClass[$inlineClass]->add($renderer, $priority); |
||
223 | $this->injectEnvironmentAndConfigurationIfNeeded($renderer); |
||
224 | |||
225 | 2082 | return $this; |
|
226 | } |
||
227 | 2082 | ||
228 | /** |
||
229 | 2082 | * {@inheritdoc} |
|
230 | */ |
||
231 | public function getBlockParsers(): iterable |
||
232 | { |
||
233 | $this->initializeExtensions(); |
||
234 | |||
235 | 2022 | return $this->blockParsers->getIterator(); |
|
236 | } |
||
237 | 2022 | ||
238 | /** |
||
239 | 2022 | * {@inheritdoc} |
|
240 | 1854 | */ |
|
241 | public function getInlineParsersForCharacter(string $character): iterable |
||
242 | { |
||
243 | 1203 | $this->initializeExtensions(); |
|
244 | |||
245 | if (!isset($this->inlineParsersByCharacter[$character])) { |
||
246 | return []; |
||
247 | } |
||
248 | |||
249 | 2025 | return $this->inlineParsersByCharacter[$character]->getIterator(); |
|
250 | } |
||
251 | 2025 | ||
252 | /** |
||
253 | 2025 | * {@inheritdoc} |
|
254 | */ |
||
255 | public function getDelimiterProcessors(): DelimiterProcessorCollection |
||
256 | { |
||
257 | $this->initializeExtensions(); |
||
258 | |||
259 | 2070 | return $this->delimiterProcessors; |
|
260 | } |
||
261 | 2070 | ||
262 | /** |
||
263 | 2070 | * {@inheritdoc} |
|
264 | */ |
||
265 | public function getDocumentProcessors(): iterable |
||
266 | { |
||
267 | $this->initializeExtensions(); |
||
268 | |||
269 | 2073 | return $this->documentProcessors->getIterator(); |
|
270 | } |
||
271 | 2073 | ||
272 | /** |
||
273 | 2073 | * {@inheritdoc} |
|
274 | 6 | */ |
|
275 | public function getBlockRenderersForClass(string $blockClass): iterable |
||
276 | { |
||
277 | 2067 | $this->initializeExtensions(); |
|
278 | |||
279 | if (!isset($this->blockRenderersByClass[$blockClass])) { |
||
280 | return []; |
||
281 | } |
||
282 | |||
283 | 1788 | return $this->blockRenderersByClass[$blockClass]->getIterator(); |
|
284 | } |
||
285 | 1788 | ||
286 | /** |
||
287 | 1788 | * {@inheritdoc} |
|
288 | 9 | */ |
|
289 | public function getInlineRenderersForClass(string $inlineClass): iterable |
||
290 | { |
||
291 | 1779 | $this->initializeExtensions(); |
|
292 | |||
293 | if (!isset($this->inlineRenderersByClass[$inlineClass])) { |
||
294 | return []; |
||
295 | } |
||
296 | |||
297 | return $this->inlineRenderersByClass[$inlineClass]->getIterator(); |
||
298 | } |
||
299 | 12 | ||
300 | /** |
||
301 | 12 | * Get all registered extensions |
|
302 | * |
||
303 | * @return ExtensionInterface[] |
||
304 | */ |
||
305 | public function getExtensions(): iterable |
||
306 | { |
||
307 | return $this->extensions; |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | 2079 | * Add a single extension |
|
312 | * |
||
313 | 2079 | * @param ExtensionInterface $extension |
|
314 | * |
||
315 | 2076 | * @return $this |
|
316 | 2076 | */ |
|
317 | public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface |
||
318 | 2076 | { |
|
319 | $this->assertUninitialized('Failed to add extension.'); |
||
320 | |||
321 | 2142 | $this->extensions[] = $extension; |
|
322 | $this->uninitializedExtensions[] = $extension; |
||
323 | |||
324 | 2142 | return $this; |
|
325 | 2061 | } |
|
326 | |||
327 | private function initializeExtensions() |
||
348 | |||
349 | 2103 | private function injectEnvironmentAndConfigurationIfNeeded($object) |
|
350 | 2064 | { |
|
351 | if ($object instanceof EnvironmentAwareInterface) { |
||
352 | 2103 | $object->setEnvironment($this); |
|
353 | } |
||
354 | |||
355 | if ($object instanceof ConfigurationAwareInterface) { |
||
356 | $object->setConfiguration($this->config); |
||
357 | 2070 | } |
|
358 | } |
||
359 | 2070 | ||
360 | 2070 | /** |
|
361 | 2070 | * @return Environment |
|
362 | 1380 | */ |
|
363 | 690 | public static function createCommonMarkEnvironment(): Environment |
|
364 | { |
||
365 | $environment = new static(); |
||
366 | $environment->addExtension(new CommonMarkCoreExtension()); |
||
367 | 2070 | $environment->mergeConfig([ |
|
380 | 1839 | ||
381 | /** |
||
382 | * {@inheritdoc} |
||
383 | 2142 | */ |
|
384 | public function getInlineParserCharacterRegex(): string |
||
388 | |||
389 | /** |
||
390 | 2142 | * {@inheritdoc} |
|
391 | */ |
||
392 | 69 | public function addEventListener(string $eventClass, callable $listener, int $priority = 0): ConfigurableEnvironmentInterface |
|
404 | 2148 | ||
405 | /** |
||
406 | 2148 | * {@inheritdoc} |
|
407 | 27 | */ |
|
408 | public function dispatch(AbstractEvent $event): void |
||
422 | |||
423 | private function buildInlineParserCharacterRegex() |
||
438 | |||
439 | /** |
||
440 | * @param string $message |
||
441 | * |
||
442 | * @throws \RuntimeException |
||
443 | */ |
||
444 | private function assertUninitialized(string $message) |
||
450 | } |
||
451 |