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 | 2175 | public function __construct(array $config = []) |
|
| 93 | { |
||
| 94 | 2175 | $this->config = new Configuration($config); |
|
| 95 | |||
| 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 | 2079 | public function mergeConfig(array $config = []) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * {@inheritdoc} |
||
| 113 | */ |
||
| 114 | 6 | public function setConfig(array $config = []) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * {@inheritdoc} |
||
| 123 | */ |
||
| 124 | 2082 | public function getConfig($key = null, $default = null) |
|
| 128 | |||
| 129 | /** |
||
| 130 | * {@inheritdoc} |
||
| 131 | */ |
||
| 132 | 2076 | public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 141 | |||
| 142 | /** |
||
| 143 | * {@inheritdoc} |
||
| 144 | */ |
||
| 145 | 2076 | public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 162 | |||
| 163 | /** |
||
| 164 | * {@inheritdoc} |
||
| 165 | */ |
||
| 166 | 2073 | public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface |
|
| 174 | |||
| 175 | /** |
||
| 176 | * {@inheritdoc} |
||
| 177 | */ |
||
| 178 | 2073 | public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 191 | |||
| 192 | /** |
||
| 193 | * {@inheritdoc} |
||
| 194 | */ |
||
| 195 | 2073 | public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 208 | |||
| 209 | /** |
||
| 210 | * {@inheritdoc} |
||
| 211 | */ |
||
| 212 | 2085 | public function getBlockParsers(): iterable |
|
| 218 | |||
| 219 | /** |
||
| 220 | * {@inheritdoc} |
||
| 221 | */ |
||
| 222 | 2025 | public function getInlineParsersForCharacter(string $character): iterable |
|
| 232 | |||
| 233 | /** |
||
| 234 | * {@inheritdoc} |
||
| 235 | */ |
||
| 236 | 2028 | public function getDelimiterProcessors(): DelimiterProcessorCollection |
|
| 242 | |||
| 243 | /** |
||
| 244 | * {@inheritdoc} |
||
| 245 | */ |
||
| 246 | 2073 | public function getBlockRenderersForClass(string $blockClass): iterable |
|
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | */ |
||
| 260 | 1788 | public function getInlineRenderersForClass(string $inlineClass): iterable |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Get all registered extensions |
||
| 273 | * |
||
| 274 | * @return ExtensionInterface[] |
||
| 275 | */ |
||
| 276 | 12 | public function getExtensions(): iterable |
|
| 280 | |||
| 281 | /** |
||
| 282 | * Add a single extension |
||
| 283 | * |
||
| 284 | * @param ExtensionInterface $extension |
||
| 285 | * |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | 2082 | public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface |
|
| 297 | |||
| 298 | 2139 | private function initializeExtensions() |
|
| 319 | |||
| 320 | 2100 | private function injectEnvironmentAndConfigurationIfNeeded($object) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @return Environment |
||
| 333 | */ |
||
| 334 | 2073 | public static function createCommonMarkEnvironment(): Environment |
|
| 351 | |||
| 352 | /** |
||
| 353 | * {@inheritdoc} |
||
| 354 | */ |
||
| 355 | 1842 | public function getInlineParserCharacterRegex(): string |
|
| 359 | |||
| 360 | /** |
||
| 361 | * {@inheritdoc} |
||
| 362 | */ |
||
| 363 | 6 | public function addEventListener(string $eventClass, callable $listener, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 375 | |||
| 376 | /** |
||
| 377 | * {@inheritdoc} |
||
| 378 | */ |
||
| 379 | 2067 | public function dispatch(AbstractEvent $event): void |
|
| 393 | |||
| 394 | 2139 | private function buildInlineParserCharacterRegex() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * @param string $message |
||
| 412 | * |
||
| 413 | * @throws \RuntimeException |
||
| 414 | */ |
||
| 415 | 2145 | private function assertUninitialized(string $message) |
|
| 421 | } |
||
| 422 |