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 | 2652 | public function __construct(array $config = []) |
|
| 104 | |||
| 105 | 2511 | public function mergeConfig(array $config = []): void |
|
| 111 | |||
| 112 | 6 | public function setConfig(array $config = []): void |
|
| 118 | |||
| 119 | 2514 | public function getConfig($key = null, $default = null) |
|
| 123 | |||
| 124 | 2502 | public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 133 | |||
| 134 | 2505 | public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 151 | |||
| 152 | 2502 | public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface |
|
| 160 | |||
| 161 | 2508 | public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 174 | |||
| 175 | 2508 | public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 188 | |||
| 189 | 2526 | public function getBlockParsers(): iterable |
|
| 197 | |||
| 198 | 2451 | public function getInlineParsersForCharacter(string $character): iterable |
|
| 210 | |||
| 211 | 2454 | public function getDelimiterProcessors(): DelimiterProcessorCollection |
|
| 219 | |||
| 220 | 2496 | public function getBlockRenderersForClass(string $blockClass): iterable |
|
| 228 | |||
| 229 | 2208 | 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 | 2514 | public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface |
|
| 264 | |||
| 265 | 2589 | private function initializeExtensions(): void |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @param object $object |
||
| 284 | */ |
||
| 285 | 2556 | private function injectEnvironmentAndConfigurationIfNeeded($object): void |
|
| 295 | |||
| 296 | 2502 | public static function createCommonMarkEnvironment(): ConfigurableEnvironmentInterface |
|
| 313 | |||
| 314 | 72 | public static function createGFMEnvironment(): ConfigurableEnvironmentInterface |
|
| 321 | |||
| 322 | 2262 | public function getInlineParserCharacterRegex(): string |
|
| 326 | |||
| 327 | 258 | public function addEventListener(string $eventClass, callable $listener, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 345 | |||
| 346 | 2496 | public function dispatch(AbstractEvent $event): void |
|
| 362 | |||
| 363 | 2589 | private function buildInlineParserCharacterRegex(): void |
|
| 378 | |||
| 379 | /** |
||
| 380 | * @param string $message |
||
| 381 | * |
||
| 382 | * @throws \RuntimeException |
||
| 383 | */ |
||
| 384 | 2607 | 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 | 2514 | 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.