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 | 2175 | ||
| 92 | public function __construct(array $config = []) |
||
| 93 | 2175 | { |
|
| 94 | $this->config = new Configuration($config); |
||
| 95 | 2175 | ||
| 96 | 2175 | $this->blockParsers = new PrioritizedList(); |
|
| 97 | 2175 | $this->inlineParsers = new PrioritizedList(); |
|
| 98 | 2175 | $this->delimiterProcessors = new DelimiterProcessorCollection(); |
|
| 99 | 2175 | } |
|
| 100 | |||
| 101 | /** |
||
| 102 | * {@inheritdoc} |
||
| 103 | */ |
||
| 104 | 2076 | public function mergeConfig(array $config = []) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | 6 | public function setConfig(array $config = []) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 2079 | public function getConfig($key = null, $default = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | 2073 | public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | 2073 | public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | 2070 | public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface |
|
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | 9 | public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 179 | { |
||
| 180 | 9 | $this->assertUninitialized('Failed to add block renderer.'); |
|
| 181 | |||
| 182 | 6 | if (!isset($this->blockRenderersByClass[$blockClass])) { |
|
| 183 | 6 | $this->blockRenderersByClass[$blockClass] = new PrioritizedList(); |
|
| 184 | } |
||
| 185 | 6 | ||
| 186 | $this->blockRenderersByClass[$blockClass]->add($blockRenderer, $priority); |
||
| 187 | $this->injectEnvironmentAndConfigurationIfNeeded($blockRenderer); |
||
| 188 | |||
| 189 | return $this; |
||
| 190 | } |
||
| 191 | 2070 | ||
| 192 | /** |
||
| 193 | 2070 | * {@inheritdoc} |
|
| 194 | */ |
||
| 195 | 2067 | public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 196 | 2067 | { |
|
| 197 | $this->assertUninitialized('Failed to add inline renderer.'); |
||
| 198 | |||
| 199 | 2067 | if (!isset($this->inlineRenderersByClass[$inlineClass])) { |
|
| 200 | 2067 | $this->inlineRenderersByClass[$inlineClass] = new PrioritizedList(); |
|
| 201 | } |
||
| 202 | 2067 | ||
| 203 | $this->inlineRenderersByClass[$inlineClass]->add($renderer, $priority); |
||
| 204 | $this->injectEnvironmentAndConfigurationIfNeeded($renderer); |
||
| 205 | |||
| 206 | return $this; |
||
| 207 | } |
||
| 208 | 2070 | ||
| 209 | /** |
||
| 210 | 2070 | * {@inheritdoc} |
|
| 211 | */ |
||
| 212 | 2067 | public function getBlockParsers(): iterable |
|
| 213 | 2067 | { |
|
| 214 | $this->initializeExtensions(); |
||
| 215 | |||
| 216 | 2067 | return $this->blockParsers->getIterator(); |
|
| 217 | 2067 | } |
|
| 218 | |||
| 219 | 2067 | /** |
|
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | public function getInlineParsersForCharacter(string $character): iterable |
||
| 223 | { |
||
| 224 | $this->initializeExtensions(); |
||
| 225 | 2082 | ||
| 226 | if (!isset($this->inlineParsersByCharacter[$character])) { |
||
| 227 | 2082 | return []; |
|
| 228 | } |
||
| 229 | 2082 | ||
| 230 | return $this->inlineParsersByCharacter[$character]->getIterator(); |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | 2022 | */ |
|
| 236 | public function getDelimiterProcessors(): DelimiterProcessorCollection |
||
| 242 | |||
| 243 | 1203 | /** |
|
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | public function getBlockRenderersForClass(string $blockClass): iterable |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | 2070 | */ |
|
| 260 | public function getInlineRenderersForClass(string $inlineClass): iterable |
||
| 270 | |||
| 271 | 2073 | /** |
|
| 272 | * Get all registered extensions |
||
| 273 | 2073 | * |
|
| 274 | 6 | * @return ExtensionInterface[] |
|
| 275 | */ |
||
| 276 | public function getExtensions(): iterable |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Add a single extension |
||
| 283 | 1788 | * |
|
| 284 | * @param ExtensionInterface $extension |
||
| 285 | 1788 | * |
|
| 286 | * @return $this |
||
| 287 | 1788 | */ |
|
| 288 | 9 | public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface |
|
| 297 | |||
| 298 | private function initializeExtensions() |
||
| 319 | |||
| 320 | private function injectEnvironmentAndConfigurationIfNeeded($object) |
||
| 330 | 2064 | ||
| 331 | 2064 | /** |
|
| 332 | 2064 | * @return Environment |
|
| 333 | */ |
||
| 334 | public static function createCommonMarkEnvironment(): Environment |
||
| 351 | |||
| 352 | 2103 | /** |
|
| 353 | * {@inheritdoc} |
||
| 354 | */ |
||
| 355 | public function getInlineParserCharacterRegex(): string |
||
| 359 | 2070 | ||
| 360 | 2070 | /** |
|
| 361 | 2070 | * {@inheritdoc} |
|
| 362 | 1380 | */ |
|
| 363 | 690 | public function addEventListener(string $eventClass, callable $listener, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritdoc} |
||
| 378 | 1839 | */ |
|
| 379 | public function dispatch(AbstractEvent $event): void |
||
| 393 | |||
| 394 | private function buildInlineParserCharacterRegex() |
||
| 409 | 2121 | ||
| 410 | /** |
||
| 411 | * @param string $message |
||
| 412 | * |
||
| 413 | * @throws \RuntimeException |
||
| 414 | */ |
||
| 415 | private function assertUninitialized(string $message) |
||
| 421 | } |
||
| 422 |