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 | 3093 | public function __construct(array $config = []) |
|
| 97 | { |
||
| 98 | 3093 | $this->config = new Configuration($config); |
|
| 99 | |||
| 100 | 3093 | $this->blockParsers = new PrioritizedList(); |
|
| 101 | 3093 | $this->inlineParsers = new PrioritizedList(); |
|
| 102 | 3093 | $this->delimiterProcessors = new DelimiterProcessorCollection(); |
|
| 103 | 3093 | } |
|
| 104 | |||
| 105 | 2937 | public function mergeConfig(array $config = []) |
|
| 106 | { |
||
| 107 | 2937 | if (\func_num_args() === 0) { |
|
| 108 | @\trigger_error('Calling Environment::mergeConfig() without any parameters is deprecated in league/commonmark 1.6 and will not be allowed in 2.0', \E_USER_DEPRECATED); |
||
| 109 | } |
||
| 110 | |||
| 111 | 2937 | $this->assertUninitialized('Failed to modify configuration.'); |
|
| 112 | |||
| 113 | 2934 | $this->config->merge($config); |
|
| 114 | 2934 | } |
|
| 115 | |||
| 116 | 24 | public function setConfig(array $config = []) |
|
| 117 | { |
||
| 118 | 24 | @\trigger_error('The Environment::setConfig() method is deprecated in league/commonmark 1.6 and will be removed in 2.0. Use mergeConfig() instead.', \E_USER_DEPRECATED); |
|
| 119 | |||
| 120 | 24 | $this->assertUninitialized('Failed to modify configuration.'); |
|
| 121 | |||
| 122 | 21 | $this->config->replace($config); |
|
| 123 | 21 | } |
|
| 124 | |||
| 125 | 2940 | public function getConfig($key = null, $default = null) |
|
| 126 | { |
||
| 127 | 2940 | return $this->config->get($key, $default); |
|
| 128 | } |
||
| 129 | |||
| 130 | 2919 | public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 131 | { |
||
| 132 | 2919 | $this->assertUninitialized('Failed to add block parser.'); |
|
| 133 | |||
| 134 | 2916 | $this->blockParsers->add($parser, $priority); |
|
| 135 | 2916 | $this->injectEnvironmentAndConfigurationIfNeeded($parser); |
|
| 136 | |||
| 137 | 2916 | return $this; |
|
| 138 | } |
||
| 139 | |||
| 140 | 2928 | public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 157 | |||
| 158 | 2919 | public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface |
|
| 159 | { |
||
| 160 | 2919 | $this->assertUninitialized('Failed to add delimiter processor.'); |
|
| 161 | 2916 | $this->delimiterProcessors->add($processor); |
|
| 162 | 2916 | $this->injectEnvironmentAndConfigurationIfNeeded($processor); |
|
| 166 | |||
| 167 | 2925 | public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 180 | |||
| 181 | 2925 | public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 194 | |||
| 195 | 2937 | public function getBlockParsers(): iterable |
|
| 203 | |||
| 204 | 2868 | public function getInlineParsersForCharacter(string $character): iterable |
|
| 216 | |||
| 217 | 2865 | public function getDelimiterProcessors(): DelimiterProcessorCollection |
|
| 225 | |||
| 226 | 2907 | public function getBlockRenderersForClass(string $blockClass): iterable |
|
| 234 | |||
| 235 | 2613 | public function getInlineRenderersForClass(string $inlineClass): iterable |
|
| 243 | |||
| 244 | /** |
||
| 245 | * Get all registered extensions |
||
| 246 | * |
||
| 247 | * @return ExtensionInterface[] |
||
| 248 | */ |
||
| 249 | 18 | public function getExtensions(): iterable |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Add a single extension |
||
| 256 | * |
||
| 257 | * @param ExtensionInterface $extension |
||
| 258 | * |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | 2940 | public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface |
|
| 270 | |||
| 271 | 3012 | private function initializeExtensions(): void |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @param object $object |
||
| 290 | */ |
||
| 291 | 2979 | private function injectEnvironmentAndConfigurationIfNeeded($object): void |
|
| 301 | |||
| 302 | 2928 | public static function createCommonMarkEnvironment(): ConfigurableEnvironmentInterface |
|
| 319 | |||
| 320 | 141 | public static function createGFMEnvironment(): ConfigurableEnvironmentInterface |
|
| 327 | |||
| 328 | 2673 | public function getInlineParserCharacterRegex(): string |
|
| 332 | |||
| 333 | 537 | public function addEventListener(string $eventClass, callable $listener, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 351 | |||
| 352 | 2913 | public function dispatch(AbstractEvent $event): void |
|
| 368 | |||
| 369 | 3006 | private function buildInlineParserCharacterRegex(): void |
|
| 389 | |||
| 390 | /** |
||
| 391 | * @param string $message |
||
| 392 | * |
||
| 393 | * @throws \RuntimeException |
||
| 394 | */ |
||
| 395 | 3039 | private function assertUninitialized(string $message): void |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @param array<string, PrioritizedList> $list |
||
| 404 | * @param string $class |
||
| 405 | * @param string $type |
||
| 406 | * |
||
| 407 | * @return iterable |
||
| 408 | * |
||
| 409 | * @phpstan-template T |
||
| 410 | * |
||
| 411 | * @phpstan-param array<string, PrioritizedList<T>> $list |
||
| 412 | * @phpstan-param string $class |
||
| 413 | * @phpstan-param class-string<T> $type |
||
| 414 | * |
||
| 415 | * @phpstan-return iterable<T> |
||
| 416 | */ |
||
| 417 | 2925 | private function getRenderersByClass(array &$list, string $class, string $type): iterable |
|
| 435 | } |
||
| 436 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.