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 |
||
| 28 | class Environment |
||
| 29 | { |
||
| 30 | const HTML_INPUT_STRIP = 'strip'; |
||
| 31 | const HTML_INPUT_ESCAPE = 'escape'; |
||
| 32 | const HTML_INPUT_ALLOW = 'allow'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var ExtensionInterface[] |
||
| 36 | */ |
||
| 37 | protected $extensions = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $extensionsInitialized = false; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var BlockParserInterface[] |
||
| 46 | */ |
||
| 47 | protected $blockParsers = []; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var InlineParserInterface[] |
||
| 51 | */ |
||
| 52 | protected $inlineParsers = []; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | protected $inlineParsersByCharacter = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var DocumentProcessorInterface[] |
||
| 61 | */ |
||
| 62 | protected $documentProcessors = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var InlineProcessorInterface[] |
||
| 66 | */ |
||
| 67 | protected $inlineProcessors = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var BlockRendererInterface[] |
||
| 71 | */ |
||
| 72 | protected $blockRenderersByClass = []; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var InlineRendererInterface[] |
||
| 76 | */ |
||
| 77 | protected $inlineRenderersByClass = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var Configuration |
||
| 81 | */ |
||
| 82 | protected $config; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @var string |
||
| 86 | */ |
||
| 87 | protected $inlineParserCharacterRegex; |
||
| 88 | |||
| 89 | 2052 | public function __construct(array $config = []) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * @param array $config |
||
| 96 | */ |
||
| 97 | 1953 | public function mergeConfig(array $config = []) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param array $config |
||
| 106 | */ |
||
| 107 | 6 | public function setConfig(array $config = []) |
|
| 113 | |||
| 114 | /** |
||
| 115 | * @param string|null $key |
||
| 116 | * @param mixed $default |
||
| 117 | * |
||
| 118 | * @return mixed |
||
| 119 | */ |
||
| 120 | 1959 | public function getConfig($key = null, $default = null) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param BlockParserInterface $parser |
||
| 127 | * |
||
| 128 | * @return $this |
||
| 129 | */ |
||
| 130 | 6 | public function addBlockParser(BlockParserInterface $parser) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param InlineParserInterface $parser |
||
| 141 | * |
||
| 142 | * @return $this |
||
| 143 | */ |
||
| 144 | 18 | public function addInlineParser(InlineParserInterface $parser) |
|
| 152 | |||
| 153 | /** |
||
| 154 | * @param InlineProcessorInterface $processor |
||
| 155 | * |
||
| 156 | * @return $this |
||
| 157 | */ |
||
| 158 | 6 | public function addInlineProcessor(InlineProcessorInterface $processor) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @param DocumentProcessorInterface $processor |
||
| 169 | * |
||
| 170 | * @return $this |
||
| 171 | */ |
||
| 172 | 9 | public function addDocumentProcessor(DocumentProcessorInterface $processor) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $blockClass |
||
| 183 | * @param BlockRendererInterface $blockRenderer |
||
| 184 | * |
||
| 185 | * @return $this |
||
| 186 | */ |
||
| 187 | 9 | public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * @param string $inlineClass |
||
| 198 | * @param InlineRendererInterface $renderer |
||
| 199 | * |
||
| 200 | * @return $this |
||
| 201 | */ |
||
| 202 | 9 | public function addInlineRenderer($inlineClass, InlineRendererInterface $renderer) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return BlockParserInterface[] |
||
| 213 | */ |
||
| 214 | 1950 | public function getBlockParsers() |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @param string $name |
||
| 223 | * |
||
| 224 | * @return InlineParserInterface |
||
| 225 | */ |
||
| 226 | 3 | public function getInlineParser($name) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @return InlineParserInterface[] |
||
| 235 | */ |
||
| 236 | 9 | public function getInlineParsers() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @param string $character |
||
| 245 | * |
||
| 246 | * @return InlineParserInterface[] |
||
| 247 | */ |
||
| 248 | 1665 | public function getInlineParsersForCharacter($character) |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @return InlineProcessorInterface[] |
||
| 261 | */ |
||
| 262 | 1668 | public function getInlineProcessors() |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @return DocumentProcessorInterface[] |
||
| 271 | */ |
||
| 272 | 1947 | public function getDocumentProcessors() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $blockClass |
||
| 281 | * |
||
| 282 | * @return BlockRendererInterface|null |
||
| 283 | */ |
||
| 284 | 1953 | public function getBlockRendererForClass($blockClass) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $inlineClass |
||
| 297 | * |
||
| 298 | * @return InlineRendererInterface|null |
||
| 299 | */ |
||
| 300 | 1674 | public function getInlineRendererForClass($inlineClass) |
|
| 310 | |||
| 311 | 6 | public function createInlineParserEngine() |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Get all registered extensions |
||
| 320 | * |
||
| 321 | * @return ExtensionInterface[] |
||
| 322 | */ |
||
| 323 | 15 | public function getExtensions() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Add a single extension |
||
| 330 | * |
||
| 331 | * @param ExtensionInterface $extension |
||
| 332 | * |
||
| 333 | * @return $this |
||
| 334 | */ |
||
| 335 | 1992 | public function addExtension(ExtensionInterface $extension) |
|
| 343 | |||
| 344 | 2016 | protected function initializeExtensions() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @param ExtensionInterface $extension |
||
| 365 | */ |
||
| 366 | 1974 | protected function initializeExtension(ExtensionInterface $extension) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * @param BlockParserInterface[] $blockParsers |
||
| 378 | */ |
||
| 379 | 1974 | private function initalizeBlockParsers($blockParsers) |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @param InlineParserInterface[] $inlineParsers |
||
| 396 | */ |
||
| 397 | 1974 | private function initializeInlineParsers($inlineParsers) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @param InlineProcessorInterface[] $inlineProcessors |
||
| 418 | */ |
||
| 419 | 1974 | private function initializeInlineProcessors($inlineProcessors) |
|
| 429 | |||
| 430 | /** |
||
| 431 | * @param DocumentProcessorInterface[] $documentProcessors |
||
| 432 | */ |
||
| 433 | 1974 | private function initializeDocumentProcessors($documentProcessors) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * @param BlockRendererInterface[] $blockRenderers |
||
| 446 | */ |
||
| 447 | 1974 | private function initializeBlockRenderers($blockRenderers) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * @param InlineRendererInterface[] $inlineRenderers |
||
| 460 | */ |
||
| 461 | 1974 | private function initializeInlineRenderers($inlineRenderers) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * @return Environment |
||
| 474 | */ |
||
| 475 | 1947 | public static function createCommonMarkEnvironment() |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Regex which matches any character which doesn't indicate an inline element |
||
| 496 | * |
||
| 497 | * This allows us to parse multiple non-special characters at once |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | 1548 | public function getInlineParserCharacterRegex() |
|
| 505 | |||
| 506 | 2016 | private function buildInlineParserCharacterRegex() |
|
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $message |
||
| 521 | * |
||
| 522 | * @throws \RuntimeException |
||
| 523 | */ |
||
| 524 | 2022 | private function assertUninitialized($message) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * @return MiscExtension |
||
| 533 | */ |
||
| 534 | 36 | private function getMiscExtension() |
|
| 546 | } |
||
| 547 |