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 | 2619 | public function __construct(array $config = []) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * {@inheritdoc} |
||
| 104 | */ |
||
| 105 | 2478 | public function mergeConfig(array $config = []) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * {@inheritdoc} |
||
| 114 | */ |
||
| 115 | 6 | public function setConfig(array $config = []) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * {@inheritdoc} |
||
| 124 | */ |
||
| 125 | 2481 | public function getConfig($key = null, $default = null) |
|
| 129 | |||
| 130 | /** |
||
| 131 | * {@inheritdoc} |
||
| 132 | */ |
||
| 133 | 2475 | public function addBlockParser(BlockParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 142 | |||
| 143 | /** |
||
| 144 | * {@inheritdoc} |
||
| 145 | */ |
||
| 146 | 2478 | public function addInlineParser(InlineParserInterface $parser, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 163 | |||
| 164 | /** |
||
| 165 | * {@inheritdoc} |
||
| 166 | */ |
||
| 167 | 2475 | public function addDelimiterProcessor(DelimiterProcessorInterface $processor): ConfigurableEnvironmentInterface |
|
| 175 | |||
| 176 | /** |
||
| 177 | * {@inheritdoc} |
||
| 178 | */ |
||
| 179 | 2481 | public function addBlockRenderer($blockClass, BlockRendererInterface $blockRenderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 192 | |||
| 193 | /** |
||
| 194 | * {@inheritdoc} |
||
| 195 | */ |
||
| 196 | 2481 | public function addInlineRenderer(string $inlineClass, InlineRendererInterface $renderer, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 209 | |||
| 210 | /** |
||
| 211 | * {@inheritdoc} |
||
| 212 | */ |
||
| 213 | 2493 | public function getBlockParsers(): iterable |
|
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | 2418 | public function getInlineParsersForCharacter(string $character): iterable |
|
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | 2421 | public function getDelimiterProcessors(): DelimiterProcessorCollection |
|
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | */ |
||
| 253 | 2463 | public function getBlockRenderersForClass(string $blockClass): iterable |
|
| 254 | { |
||
| 255 | 2463 | if (!$this->extensionsInitialized) { |
|
| 256 | 15 | $this->initializeExtensions(); |
|
| 257 | } |
||
| 258 | |||
| 259 | 2463 | return $this->getRenderersByClass($this->blockRenderersByClass, $blockClass); |
|
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * {@inheritdoc} |
||
| 264 | */ |
||
| 265 | 2175 | public function getInlineRenderersForClass(string $inlineClass): iterable |
|
| 266 | { |
||
| 267 | 2175 | if (!$this->extensionsInitialized) { |
|
| 268 | 18 | $this->initializeExtensions(); |
|
| 269 | } |
||
| 270 | |||
| 271 | 2175 | return $this->getRenderersByClass($this->inlineRenderersByClass, $inlineClass); |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get all registered extensions |
||
| 276 | * |
||
| 277 | * @return ExtensionInterface[] |
||
| 278 | */ |
||
| 279 | 12 | public function getExtensions(): iterable |
|
| 280 | { |
||
| 281 | 12 | return $this->extensions; |
|
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Add a single extension |
||
| 286 | * |
||
| 287 | * @param ExtensionInterface $extension |
||
| 288 | * |
||
| 289 | * @return $this |
||
| 290 | */ |
||
| 291 | 2481 | public function addExtension(ExtensionInterface $extension): ConfigurableEnvironmentInterface |
|
| 292 | { |
||
| 293 | 2481 | $this->assertUninitialized('Failed to add extension.'); |
|
| 294 | |||
| 295 | 2478 | $this->extensions[] = $extension; |
|
| 296 | 2478 | $this->uninitializedExtensions[] = $extension; |
|
| 297 | |||
| 298 | 2478 | return $this; |
|
| 299 | } |
||
| 300 | |||
| 301 | 2559 | private function initializeExtensions() |
|
| 302 | { |
||
| 303 | // Ask all extensions to register their components |
||
| 304 | 2559 | while (!empty($this->uninitializedExtensions)) { |
|
| 305 | 2466 | foreach ($this->uninitializedExtensions as $i => $extension) { |
|
| 306 | 2466 | $extension->register($this); |
|
| 307 | 2466 | unset($this->uninitializedExtensions[$i]); |
|
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | 2559 | $this->extensionsInitialized = true; |
|
| 312 | |||
| 313 | // Lastly, let's build a regex which matches non-inline characters |
||
| 314 | // This will enable a huge performance boost with inline parsing |
||
| 315 | 2559 | $this->buildInlineParserCharacterRegex(); |
|
| 316 | 2559 | } |
|
| 317 | |||
| 318 | 2529 | private function injectEnvironmentAndConfigurationIfNeeded($object) |
|
| 319 | { |
||
| 320 | 2529 | if ($object instanceof EnvironmentAwareInterface) { |
|
| 321 | 2481 | $object->setEnvironment($this); |
|
| 322 | } |
||
| 323 | |||
| 324 | 2529 | if ($object instanceof ConfigurationAwareInterface) { |
|
| 325 | 2481 | $object->setConfiguration($this->config); |
|
| 326 | } |
||
| 327 | 2529 | } |
|
| 328 | |||
| 329 | 2469 | public static function createCommonMarkEnvironment(): ConfigurableEnvironmentInterface |
|
| 330 | { |
||
| 331 | 2469 | $environment = new static(); |
|
| 332 | 2469 | $environment->addExtension(new CommonMarkCoreExtension()); |
|
| 333 | 2469 | $environment->mergeConfig([ |
|
| 334 | 1646 | 'renderer' => [ |
|
| 335 | 823 | 'block_separator' => "\n", |
|
| 336 | 'inner_separator' => "\n", |
||
| 337 | 'soft_break' => "\n", |
||
| 338 | ], |
||
| 339 | 2469 | 'html_input' => self::HTML_INPUT_ALLOW, |
|
| 340 | 'allow_unsafe_links' => true, |
||
| 341 | 'max_nesting_level' => \INF, |
||
| 342 | ]); |
||
| 343 | |||
| 344 | 2469 | return $environment; |
|
| 345 | } |
||
| 346 | |||
| 347 | 72 | public static function createGFMEnvironment(): ConfigurableEnvironmentInterface |
|
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritdoc} |
||
| 357 | */ |
||
| 358 | 2229 | public function getInlineParserCharacterRegex(): string |
|
| 359 | { |
||
| 360 | 2229 | return $this->inlineParserCharacterRegex; |
|
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * {@inheritdoc} |
||
| 365 | */ |
||
| 366 | 222 | public function addEventListener(string $eventClass, callable $listener, int $priority = 0): ConfigurableEnvironmentInterface |
|
| 382 | |||
| 383 | /** |
||
| 384 | * {@inheritdoc} |
||
| 385 | */ |
||
| 386 | 2466 | public function dispatch(AbstractEvent $event): void |
|
| 402 | |||
| 403 | 2559 | private function buildInlineParserCharacterRegex() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $message |
||
| 421 | * |
||
| 422 | * @throws \RuntimeException |
||
| 423 | */ |
||
| 424 | 2571 | private function assertUninitialized(string $message) |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @param array<string, PrioritizedList> $list |
||
| 433 | * @param string $class |
||
| 434 | * |
||
| 435 | * @return iterable |
||
| 436 | */ |
||
| 437 | 2481 | private function getRenderersByClass(array &$list, string $class): iterable |
|
| 455 | } |
||
| 456 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.