| Total Complexity | 56 |
| Total Lines | 398 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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.
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 |
||
| 47 | final class Environment implements EnvironmentInterface, EnvironmentBuilderInterface, ListenerProviderInterface |
||
| 48 | { |
||
| 49 | /** |
||
| 50 | * @var ExtensionInterface[] |
||
| 51 | * |
||
| 52 | * @psalm-readonly-allow-private-mutation |
||
| 53 | */ |
||
| 54 | private array $extensions = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var ExtensionInterface[] |
||
| 58 | * |
||
| 59 | * @psalm-readonly-allow-private-mutation |
||
| 60 | */ |
||
| 61 | private array $uninitializedExtensions = []; |
||
| 62 | |||
| 63 | /** @psalm-readonly-allow-private-mutation */ |
||
| 64 | private bool $extensionsInitialized = false; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var PrioritizedList<BlockStartParserInterface> |
||
| 68 | * |
||
| 69 | * @psalm-readonly |
||
| 70 | */ |
||
| 71 | private PrioritizedList $blockStartParsers; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var PrioritizedList<InlineParserInterface> |
||
| 75 | * |
||
| 76 | * @psalm-readonly |
||
| 77 | */ |
||
| 78 | private PrioritizedList $inlineParsers; |
||
| 79 | |||
| 80 | /** @psalm-readonly */ |
||
| 81 | private DelimiterProcessorCollection $delimiterProcessors; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var array<string, PrioritizedList<NodeRendererInterface>> |
||
|
|
|||
| 85 | * |
||
| 86 | * @psalm-readonly-allow-private-mutation |
||
| 87 | */ |
||
| 88 | private array $renderersByClass = []; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var PrioritizedList<ListenerData> |
||
| 92 | * |
||
| 93 | * @psalm-readonly-allow-private-mutation |
||
| 94 | */ |
||
| 95 | private PrioritizedList $listenerData; |
||
| 96 | |||
| 97 | private ?EventDispatcherInterface $eventDispatcher = null; |
||
| 98 | |||
| 99 | /** @psalm-readonly */ |
||
| 100 | private Configuration $config; |
||
| 101 | |||
| 102 | private ?TextNormalizerInterface $slugNormalizer = null; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * @param array<string, mixed> $config |
||
| 106 | */ |
||
| 107 | public function __construct(array $config = []) |
||
| 108 | { |
||
| 109 | $this->config = self::createDefaultConfiguration(); |
||
| 110 | $this->config->merge($config); |
||
| 111 | |||
| 112 | $this->blockStartParsers = new PrioritizedList(); |
||
| 113 | $this->inlineParsers = new PrioritizedList(); |
||
| 114 | $this->listenerData = new PrioritizedList(); |
||
| 115 | $this->delimiterProcessors = new DelimiterProcessorCollection(); |
||
| 116 | |||
| 117 | // Performance optimization: always include a block "parser" that aborts parsing if a line starts with a letter |
||
| 118 | // and is therefore unlikely to match any lines as a block start. |
||
| 119 | $this->addBlockStartParser(new SkipLinesStartingWithLettersParser(), 249); |
||
| 120 | } |
||
| 121 | |||
| 122 | public function getConfiguration(): ConfigurationInterface |
||
| 123 | { |
||
| 124 | return $this->config->reader(); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @deprecated Environment::mergeConfig() is deprecated since league/commonmark v2.0 and will be removed in v3.0. Configuration should be set when instantiating the environment instead. |
||
| 129 | * |
||
| 130 | * @param array<string, mixed> $config |
||
| 131 | */ |
||
| 132 | public function mergeConfig(array $config): void |
||
| 133 | { |
||
| 134 | @\trigger_error('Environment::mergeConfig() is deprecated since league/commonmark v2.0 and will be removed in v3.0. Configuration should be set when instantiating the environment instead.', \E_USER_DEPRECATED); |
||
| 135 | |||
| 136 | $this->assertUninitialized('Failed to modify configuration.'); |
||
| 137 | |||
| 138 | $this->config->merge($config); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function addBlockStartParser(BlockStartParserInterface $parser, int $priority = 0): EnvironmentBuilderInterface |
||
| 142 | { |
||
| 143 | $this->assertUninitialized('Failed to add block start parser.'); |
||
| 144 | |||
| 145 | $this->blockStartParsers->add($parser, $priority); |
||
| 146 | $this->injectEnvironmentAndConfigurationIfNeeded($parser); |
||
| 147 | |||
| 148 | return $this; |
||
| 149 | } |
||
| 150 | |||
| 151 | public function addInlineParser(InlineParserInterface $parser, int $priority = 0): EnvironmentBuilderInterface |
||
| 152 | { |
||
| 153 | $this->assertUninitialized('Failed to add inline parser.'); |
||
| 154 | |||
| 155 | $this->inlineParsers->add($parser, $priority); |
||
| 156 | $this->injectEnvironmentAndConfigurationIfNeeded($parser); |
||
| 157 | |||
| 158 | return $this; |
||
| 159 | } |
||
| 160 | |||
| 161 | public function addDelimiterProcessor(DelimiterProcessorInterface $processor): EnvironmentBuilderInterface |
||
| 162 | { |
||
| 163 | $this->assertUninitialized('Failed to add delimiter processor.'); |
||
| 164 | $this->delimiterProcessors->add($processor); |
||
| 165 | $this->injectEnvironmentAndConfigurationIfNeeded($processor); |
||
| 166 | |||
| 167 | return $this; |
||
| 168 | } |
||
| 169 | |||
| 170 | public function addRenderer(string $nodeClass, NodeRendererInterface $renderer, int $priority = 0): EnvironmentBuilderInterface |
||
| 171 | { |
||
| 172 | $this->assertUninitialized('Failed to add renderer.'); |
||
| 173 | |||
| 174 | if (! isset($this->renderersByClass[$nodeClass])) { |
||
| 175 | $this->renderersByClass[$nodeClass] = new PrioritizedList(); |
||
| 176 | } |
||
| 177 | |||
| 178 | $this->renderersByClass[$nodeClass]->add($renderer, $priority); |
||
| 179 | $this->injectEnvironmentAndConfigurationIfNeeded($renderer); |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * {@inheritDoc} |
||
| 186 | */ |
||
| 187 | public function getBlockStartParsers(): iterable |
||
| 188 | { |
||
| 189 | if (! $this->extensionsInitialized) { |
||
| 190 | $this->initializeExtensions(); |
||
| 191 | } |
||
| 192 | |||
| 193 | return $this->blockStartParsers->getIterator(); |
||
| 194 | } |
||
| 195 | |||
| 196 | public function getDelimiterProcessors(): DelimiterProcessorCollection |
||
| 197 | { |
||
| 198 | if (! $this->extensionsInitialized) { |
||
| 199 | $this->initializeExtensions(); |
||
| 200 | } |
||
| 201 | |||
| 202 | return $this->delimiterProcessors; |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * {@inheritDoc} |
||
| 207 | */ |
||
| 208 | public function getRenderersForClass(string $nodeClass): iterable |
||
| 209 | { |
||
| 210 | if (! $this->extensionsInitialized) { |
||
| 211 | $this->initializeExtensions(); |
||
| 212 | } |
||
| 213 | |||
| 214 | // If renderers are defined for this specific class, return them immediately |
||
| 215 | if (isset($this->renderersByClass[$nodeClass])) { |
||
| 216 | return $this->renderersByClass[$nodeClass]; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** @psalm-suppress TypeDoesNotContainType -- Bug: https://github.com/vimeo/psalm/issues/3332 */ |
||
| 220 | while (\class_exists($parent ??= $nodeClass) && $parent = \get_parent_class($parent)) { |
||
| 221 | if (! isset($this->renderersByClass[$parent])) { |
||
| 222 | continue; |
||
| 223 | } |
||
| 224 | |||
| 225 | // "Cache" this result to avoid future loops |
||
| 226 | return $this->renderersByClass[$nodeClass] = $this->renderersByClass[$parent]; |
||
| 227 | } |
||
| 228 | |||
| 229 | return []; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * {@inheritDoc} |
||
| 234 | */ |
||
| 235 | public function getExtensions(): iterable |
||
| 236 | { |
||
| 237 | return $this->extensions; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Add a single extension |
||
| 242 | * |
||
| 243 | * @return $this |
||
| 244 | */ |
||
| 245 | public function addExtension(ExtensionInterface $extension): EnvironmentBuilderInterface |
||
| 257 | } |
||
| 258 | |||
| 259 | private function initializeExtensions(): void |
||
| 260 | { |
||
| 261 | // Initialize the slug normalizer |
||
| 262 | $this->getSlugNormalizer(); |
||
| 263 | |||
| 264 | // Ask all extensions to register their components |
||
| 265 | while (\count($this->uninitializedExtensions) > 0) { |
||
| 266 | foreach ($this->uninitializedExtensions as $i => $extension) { |
||
| 267 | $extension->register($this); |
||
| 268 | unset($this->uninitializedExtensions[$i]); |
||
| 269 | } |
||
| 270 | } |
||
| 271 | |||
| 272 | $this->extensionsInitialized = true; |
||
| 273 | |||
| 274 | // Create the special delimiter parser if any processors were registered |
||
| 275 | if ($this->delimiterProcessors->count() > 0) { |
||
| 276 | $this->inlineParsers->add(new DelimiterParser($this->delimiterProcessors), PHP_INT_MIN); |
||
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | private function injectEnvironmentAndConfigurationIfNeeded(object $object): void |
||
| 281 | { |
||
| 282 | if ($object instanceof EnvironmentAwareInterface) { |
||
| 283 | $object->setEnvironment($this); |
||
| 284 | } |
||
| 285 | |||
| 286 | if ($object instanceof ConfigurationAwareInterface) { |
||
| 287 | $object->setConfiguration($this->config->reader()); |
||
| 288 | } |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * @deprecated Instantiate the environment and add the extension yourself |
||
| 293 | * |
||
| 294 | * @param array<string, mixed> $config |
||
| 295 | */ |
||
| 296 | public static function createCommonMarkEnvironment(array $config = []): Environment |
||
| 297 | { |
||
| 298 | $environment = new self($config); |
||
| 299 | $environment->addExtension(new CommonMarkCoreExtension()); |
||
| 300 | |||
| 301 | return $environment; |
||
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * @deprecated Instantiate the environment and add the extension yourself |
||
| 306 | * |
||
| 307 | * @param array<string, mixed> $config |
||
| 308 | */ |
||
| 309 | public static function createGFMEnvironment(array $config = []): Environment |
||
| 310 | { |
||
| 311 | $environment = new self($config); |
||
| 312 | $environment->addExtension(new CommonMarkCoreExtension()); |
||
| 313 | $environment->addExtension(new GithubFlavoredMarkdownExtension()); |
||
| 314 | |||
| 315 | return $environment; |
||
| 316 | } |
||
| 317 | |||
| 318 | public function addEventListener(string $eventClass, callable $listener, int $priority = 0): EnvironmentBuilderInterface |
||
| 331 | } |
||
| 332 | |||
| 333 | public function dispatch(object $event): object |
||
| 352 | } |
||
| 353 | |||
| 354 | public function setEventDispatcher(EventDispatcherInterface $dispatcher): void |
||
| 357 | } |
||
| 358 | |||
| 359 | /** |
||
| 360 | * {@inheritDoc} |
||
| 361 | * |
||
| 362 | * @return iterable<callable> |
||
| 363 | */ |
||
| 364 | public function getListenersForEvent(object $event): iterable |
||
| 365 | { |
||
| 366 | foreach ($this->listenerData as $listenerData) { |
||
| 367 | \assert($listenerData instanceof ListenerData); |
||
| 368 | |||
| 369 | /** @psalm-suppress ArgumentTypeCoercion */ |
||
| 370 | if (! \is_a($event, $listenerData->getEvent())) { |
||
| 371 | continue; |
||
| 372 | } |
||
| 373 | |||
| 374 | yield function (object $event) use ($listenerData) { |
||
| 375 | if (! $this->extensionsInitialized) { |
||
| 376 | $this->initializeExtensions(); |
||
| 377 | } |
||
| 378 | |||
| 379 | return \call_user_func($listenerData->getListener(), $event); |
||
| 380 | }; |
||
| 381 | } |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @return iterable<InlineParserInterface> |
||
| 386 | */ |
||
| 387 | public function getInlineParsers(): iterable |
||
| 388 | { |
||
| 389 | if (! $this->extensionsInitialized) { |
||
| 390 | $this->initializeExtensions(); |
||
| 391 | } |
||
| 392 | |||
| 393 | return $this->inlineParsers->getIterator(); |
||
| 394 | } |
||
| 395 | |||
| 396 | public function getSlugNormalizer(): TextNormalizerInterface |
||
| 397 | { |
||
| 398 | if ($this->slugNormalizer === null) { |
||
| 399 | $normalizer = $this->config->get('slug_normalizer/instance'); |
||
| 400 | \assert($normalizer instanceof TextNormalizerInterface); |
||
| 401 | $this->injectEnvironmentAndConfigurationIfNeeded($normalizer); |
||
| 402 | |||
| 403 | if ($this->config->get('slug_normalizer/unique') !== UniqueSlugNormalizerInterface::DISABLED && ! $normalizer instanceof UniqueSlugNormalizer) { |
||
| 404 | $normalizer = new UniqueSlugNormalizer($normalizer); |
||
| 405 | } |
||
| 406 | |||
| 407 | if ($normalizer instanceof UniqueSlugNormalizer) { |
||
| 408 | if ($this->config->get('slug_normalizer/unique') === UniqueSlugNormalizerInterface::PER_DOCUMENT) { |
||
| 409 | $this->addEventListener(DocumentParsedEvent::class, [$normalizer, 'clearHistory'], -1000); |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | $this->slugNormalizer = $normalizer; |
||
| 414 | } |
||
| 415 | |||
| 416 | return $this->slugNormalizer; |
||
| 417 | } |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @throws AlreadyInitializedException |
||
| 421 | */ |
||
| 422 | private function assertUninitialized(string $message): void |
||
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | public static function createDefaultConfiguration(): Configuration |
||
| 430 | { |
||
| 445 | ]), |
||
| 446 | ]); |
||
| 447 | } |
||
| 448 | } |
||
| 449 |