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 |
||
31 | final class Environment implements ConfigurableEnvironmentInterface |
||
32 | { |
||
33 | /** |
||
34 | * @var ExtensionInterface[] |
||
35 | */ |
||
36 | private $extensions = []; |
||
37 | |||
38 | /** |
||
39 | * @var ExtensionInterface[] |
||
40 | */ |
||
41 | private $uninitializedExtensions = []; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | private $extensionsInitialized = false; |
||
47 | |||
48 | /** |
||
49 | * @var PrioritizedList<BlockParserInterface> |
||
50 | */ |
||
51 | private $blockParsers; |
||
52 | |||
53 | /** |
||
54 | * @var PrioritizedList<InlineParserInterface> |
||
55 | */ |
||
56 | private $inlineParsers; |
||
57 | |||
58 | /** |
||
59 | * @var array<string, PrioritizedList<InlineParserInterface>> |
||
60 | */ |
||
61 | private $inlineParsersByCharacter = []; |
||
62 | |||
63 | /** |
||
64 | * @var DelimiterProcessorCollection |
||
65 | */ |
||
66 | private $delimiterProcessors; |
||
67 | |||
68 | /** |
||
69 | * @var array<string, PrioritizedList<BlockRendererInterface>> |
||
70 | */ |
||
71 | private $blockRenderersByClass = []; |
||
72 | |||
73 | /** |
||
74 | * @var array<string, PrioritizedList<InlineRendererInterface>> |
||
75 | */ |
||
76 | private $inlineRenderersByClass = []; |
||
77 | |||
78 | /** |
||
79 | * @var array<string, PrioritizedList<callable>> |
||
80 | */ |
||
81 | private $listeners = []; |
||
82 | |||
83 | /** |
||
84 | * @var Configuration |
||
85 | */ |
||
86 | private $config; |
||
87 | |||
88 | /** |
||
89 | * @var string |
||
90 | */ |
||
91 | private $inlineParserCharacterRegex; |
||
92 | |||
93 | /** |
||
94 | * @param array<string, mixed> $config |
||
95 | */ |
||
96 | 2952 | public function __construct(array $config = []) |
|
97 | { |
||
98 | 2952 | $this->config = new Configuration($config); |
|
99 | |||
100 | 2952 | $this->blockParsers = new PrioritizedList(); |
|
101 | 2952 | $this->inlineParsers = new PrioritizedList(); |
|
102 | 2952 | $this->delimiterProcessors = new DelimiterProcessorCollection(); |
|
103 | 2952 | } |
|
104 | |||
105 | 2811 | public function mergeConfig(array $config = []) |
|
106 | { |
||
107 | 2811 | $this->assertUninitialized('Failed to modify configuration.'); |
|
108 | |||
109 | 2808 | $this->config->merge($config); |
|
110 | 2808 | } |
|
111 | |||
112 | 18 | public function setConfig(array $config = []) |
|
113 | { |
||
114 | 18 | $this->assertUninitialized('Failed to modify configuration.'); |
|
115 | |||
116 | 15 | $this->config->replace($config); |
|
117 | 15 | } |
|
118 | |||
119 | 2814 | public function getConfig($key = null, $default = null) |
|
123 | |||
124 | 2802 | public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
125 | { |
||
126 | 2802 | $this->assertUninitialized('Failed to add block parser.'); |
|
133 | |||
134 | 2805 | public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
151 | |||
152 | 2802 | public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface |
|
160 | |||
161 | 2808 | public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
174 | |||
175 | 2808 | public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
188 | |||
189 | 2823 | public function getBlockParsers(): iterable |
|
197 | |||
198 | 2748 | public function getInlineParsersForCharacter(string $character): iterable |
|
210 | |||
211 | 2751 | public function getDelimiterProcessors(): DelimiterProcessorCollection |
|
219 | |||
220 | 2793 | public function getBlockRenderersForClass(string $blockClass): iterable |
|
228 | |||
229 | 2502 | public function getInlineRenderersForClass(string $inlineClass): iterable |
|
237 | |||
238 | /** |
||
239 | * Get all registered extensions |
||
240 | * |
||
241 | * @return ExtensionInterface[] |
||
242 | */ |
||
243 | 12 | public function getExtensions(): iterable |
|
247 | |||
248 | /** |
||
249 | * Add a single extension |
||
250 | * |
||
251 | * @param ExtensionInterface $extension |
||
252 | * |
||
253 | * @return $this |
||
254 | */ |
||
255 | 2814 | public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface |
|
264 | |||
265 | 2889 | private function initializeExtensions(): void |
|
281 | |||
282 | /** |
||
283 | * @param object $object |
||
284 | */ |
||
285 | 2856 | private function injectEnvironmentAndConfigurationIfNeeded($object): void |
|
295 | |||
296 | 2802 | public static function createCommonMarkEnvironment(): ConfigurableEnvironmentInterface |
|
313 | |||
314 | 132 | public static function createGFMEnvironment(): ConfigurableEnvironmentInterface |
|
321 | |||
322 | 2559 | public function getInlineParserCharacterRegex(): string |
|
326 | |||
327 | 522 | public function addEventListener(string $eventClass, callable $listener, int $priority = 0): ConfigurableEnvironmentInterface |
|
345 | |||
346 | 2796 | public function dispatch(AbstractEvent $event): void |
|
362 | |||
363 | 2886 | private function buildInlineParserCharacterRegex(): void |
|
378 | |||
379 | /** |
||
380 | * @param string $message |
||
381 | * |
||
382 | * @throws \RuntimeException |
||
383 | */ |
||
384 | 2907 | private function assertUninitialized(string $message): void |
|
390 | |||
391 | /** |
||
392 | * @param array<string, PrioritizedList> $list |
||
393 | * @param string $class |
||
394 | * @param string $type |
||
395 | * |
||
396 | * @return iterable |
||
397 | * |
||
398 | * @phpstan-template T |
||
399 | * |
||
400 | * @phpstan-param array<string, PrioritizedList<T>> $list |
||
401 | * @phpstan-param string $class |
||
402 | * @phpstan-param class-string<T> $type |
||
403 | * |
||
404 | * @phpstan-return iterable<T> |
||
405 | */ |
||
406 | 2811 | private function getRenderersByClass(array &$list, string $class, string $type): iterable |
|
424 | } |
||
425 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.