Total Complexity | 58 |
Total Lines | 435 |
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 |
||
41 | final class Environment implements EnvironmentBuilderInterface |
||
42 | { |
||
43 | /** @var Configuration */ |
||
44 | private $config; |
||
45 | |||
46 | /** @var ?RuntimeDataset */ |
||
47 | private $dataset; |
||
48 | |||
49 | /** @var ?EventDispatcherInterface */ |
||
50 | private $eventDispatcher; |
||
51 | |||
52 | /** |
||
53 | * @var ExtensionInterface[] |
||
54 | * |
||
55 | * @psalm-readonly-allow-private-mutation |
||
56 | */ |
||
57 | private $extensions = []; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | * |
||
62 | * @psalm-readonly-allow-private-mutation |
||
63 | */ |
||
64 | private $extensionsInitialized = false; |
||
65 | |||
66 | /** |
||
67 | * @var bool |
||
68 | * |
||
69 | * @psalm-readonly-allow-private-mutation |
||
70 | */ |
||
71 | private $initialized = false; |
||
72 | |||
73 | /** |
||
74 | * @var ?PrioritizedList<ListenerData> |
||
75 | * |
||
76 | * @psalm-readonly-allow-private-mutation |
||
77 | */ |
||
78 | private $listenerData; |
||
79 | |||
80 | /** @var ?EmojiParserInterface */ |
||
81 | private $parser; |
||
82 | |||
83 | /** |
||
84 | * @var array<string, PrioritizedList<NodeRendererInterface>> |
||
|
|||
85 | * |
||
86 | * @psalm-readonly-allow-private-mutation |
||
87 | */ |
||
88 | private $renderersByClass = []; |
||
89 | |||
90 | /** |
||
91 | * @var ExtensionInterface[] |
||
92 | * |
||
93 | * @psalm-readonly-allow-private-mutation |
||
94 | */ |
||
95 | private $uninitializedExtensions = []; |
||
96 | |||
97 | /** |
||
98 | * @param array<string, mixed> $config |
||
99 | */ |
||
100 | public function __construct(array $config = []) |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * @param array<string, mixed> $configuration |
||
108 | */ |
||
109 | public static function create(array $configuration = []): self |
||
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param string|string[] $value |
||
122 | * |
||
123 | * @return string[] |
||
124 | */ |
||
125 | public static function normalizeConvert($value): array |
||
126 | { |
||
127 | if (\is_array($value)) { |
||
128 | return $value; |
||
129 | } |
||
130 | |||
131 | return \array_fill_keys(EmojiConverterInterface::TYPES, $value); |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * @return ExtensionInterface[] |
||
136 | */ |
||
137 | protected static function defaultExtensions(): iterable |
||
138 | { |
||
139 | return [new EmojiCoreExtension()]; |
||
140 | } |
||
141 | |||
142 | public static function normalizeLocale(string $locale): string |
||
143 | { |
||
144 | // Immediately return if locale is an exact match. |
||
145 | if (\in_array($locale, EmojibaseDatasetInterface::SUPPORTED_LOCALES, true)) { |
||
146 | return $locale; |
||
147 | } |
||
148 | |||
149 | // Immediately return if this local has already been normalized. |
||
150 | /** @var string[] $normalized */ |
||
151 | static $normalized = []; |
||
152 | if (isset($normalized[$locale])) { |
||
153 | return $normalized[$locale]; |
||
154 | } |
||
155 | |||
156 | $original = $locale; |
||
157 | $normalized[$original] = ''; |
||
158 | |||
159 | // Otherwise, see if it just needs some TLC. |
||
160 | $locale = \strtolower($locale); |
||
161 | $locale = \preg_replace('/[^a-z]/', '-', $locale) ?? $locale; |
||
162 | foreach ([$locale, \current(\explode('-', $locale, 2))] as $locale) { |
||
163 | if (\in_array($locale, EmojibaseDatasetInterface::SUPPORTED_LOCALES, true)) { |
||
164 | $normalized[$original] = $locale; |
||
165 | break; |
||
166 | } |
||
167 | } |
||
168 | |||
169 | return $normalized[$original]; |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * @param string|string[] $presets |
||
174 | * |
||
175 | * @return string[] |
||
176 | */ |
||
177 | public static function normalizePresets($presets): array |
||
178 | { |
||
179 | // Presets. |
||
180 | $normalized = []; |
||
181 | foreach ((array) $presets as $preset) { |
||
182 | if (isset(EmojibaseShortcodeInterface::PRESET_ALIASES[$preset])) { |
||
183 | $normalized[] = EmojibaseShortcodeInterface::PRESET_ALIASES[$preset]; |
||
184 | } elseif (isset(EmojibaseShortcodeInterface::PRESETS[$preset])) { |
||
185 | $normalized[] = EmojibaseShortcodeInterface::PRESETS[$preset]; |
||
186 | } else { |
||
187 | throw InvalidConfigurationException::forConfigOption( |
||
188 | 'preset', |
||
189 | $preset, |
||
190 | \sprintf( |
||
191 | 'Accepted values are: %s.', |
||
192 | \implode( |
||
193 | ', ', |
||
194 | \array_map( |
||
195 | static function ($s) { |
||
196 | return \sprintf('"%s"', $s); |
||
197 | }, |
||
198 | EmojibaseShortcodeInterface::SUPPORTED_PRESETS |
||
199 | ) |
||
200 | ) |
||
201 | ) |
||
202 | ); |
||
203 | } |
||
204 | } |
||
205 | |||
206 | return \array_values(\array_unique($normalized)); |
||
207 | } |
||
208 | |||
209 | public function addEventListener(string $eventClass, callable $listener, int $priority = 0): EnvironmentBuilderInterface |
||
234 | } |
||
235 | |||
236 | public function addExtension(ExtensionInterface $extension): EnvironmentBuilderInterface |
||
248 | } |
||
249 | |||
250 | public function addRenderer(string $nodeClass, NodeRendererInterface $renderer, int $priority = 0): EnvironmentBuilderInterface |
||
251 | { |
||
252 | $this->assertUninitialized('Failed to add renderer.'); |
||
253 | |||
254 | if (! isset($this->renderersByClass[$nodeClass])) { |
||
255 | /** @var PrioritizedList<NodeRendererInterface> $renderers */ |
||
256 | $renderers = new PrioritizedList(); |
||
257 | |||
258 | $this->renderersByClass[$nodeClass] = $renderers; |
||
259 | } |
||
260 | |||
261 | $this->renderersByClass[$nodeClass]->add($renderer, $priority); |
||
262 | |||
263 | if ($renderer instanceof EnvironmentAwareInterface) { |
||
264 | $renderer->setEnvironment($this); |
||
265 | } |
||
266 | |||
267 | if ($renderer instanceof ConfigurationAwareInterface) { |
||
268 | $renderer->setConfiguration($this->getConfiguration()); |
||
269 | } |
||
270 | |||
271 | return $this; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * @throws \RuntimeException |
||
276 | */ |
||
277 | protected function assertUninitialized(string $message): void |
||
278 | { |
||
279 | if ($this->initialized) { |
||
280 | throw new \RuntimeException(\sprintf('%s The Environment has already been initialized.', $message)); |
||
281 | } |
||
282 | } |
||
283 | |||
284 | /** |
||
285 | * {@inheritDoc} |
||
286 | */ |
||
287 | public function dispatch(object $event) |
||
288 | { |
||
289 | $this->initialize(); |
||
290 | |||
291 | if ($this->eventDispatcher !== null) { |
||
292 | return $this->eventDispatcher->dispatch($event); |
||
293 | } |
||
294 | |||
295 | foreach ($this->getListenersForEvent($event) as $listener) { |
||
296 | if ($event instanceof StoppableEventInterface && $event->isPropagationStopped()) { |
||
297 | return $event; |
||
298 | } |
||
299 | |||
300 | $listener($event); |
||
301 | } |
||
302 | |||
303 | return $event; |
||
304 | } |
||
305 | |||
306 | public function getConfiguration(): ConfigurationInterface |
||
307 | { |
||
308 | $this->initializeConfiguration(); |
||
309 | |||
310 | return $this->config->reader(); |
||
311 | } |
||
312 | |||
313 | public function getRuntimeDataset(string $index = 'hexcode'): RuntimeDataset |
||
314 | { |
||
315 | $this->initialize(); |
||
316 | |||
317 | if ($this->dataset === null) { |
||
318 | $this->dataset = new RuntimeDataset($this->getConfiguration()); |
||
319 | } |
||
320 | |||
321 | return $this->dataset->indexBy($index); |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * {@inheritDoc} |
||
326 | * |
||
327 | * @return ExtensionInterface[] |
||
328 | */ |
||
329 | public function getExtensions(): iterable |
||
330 | { |
||
331 | return $this->extensions; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * {@inheritDoc} |
||
336 | * |
||
337 | * @return iterable<callable> |
||
338 | */ |
||
339 | public function getListenersForEvent(object $event): iterable |
||
340 | { |
||
341 | if ($this->listenerData === null) { |
||
342 | /** @var PrioritizedList<ListenerData> $listenerData */ |
||
343 | $listenerData = new PrioritizedList(); |
||
344 | $this->listenerData = $listenerData; |
||
345 | } |
||
346 | |||
347 | /** @var ListenerData $listenerData */ |
||
348 | foreach ($this->listenerData as $listenerData) { |
||
349 | if (! \is_a($event, $listenerData->getEvent())) { |
||
350 | continue; |
||
351 | } |
||
352 | |||
353 | yield function (object $event) use ($listenerData): void { |
||
354 | $this->initialize(); |
||
355 | |||
356 | \call_user_func($listenerData->getListener(), $event); |
||
357 | }; |
||
358 | } |
||
359 | } |
||
360 | |||
361 | public function getParser(): EmojiParserInterface |
||
362 | { |
||
363 | if ($this->parser === null) { |
||
364 | $this->parser = new EmojiParser($this); |
||
365 | } |
||
366 | |||
367 | return $this->parser; |
||
368 | } |
||
369 | |||
370 | /** |
||
371 | * {@inheritDoc} |
||
372 | */ |
||
373 | public function getRenderersForClass(string $nodeClass): iterable |
||
374 | { |
||
375 | $this->initialize(); |
||
376 | |||
377 | // If renderers are defined for this specific class, return them immediately |
||
378 | if (isset($this->renderersByClass[$nodeClass])) { |
||
379 | return $this->renderersByClass[$nodeClass]; |
||
380 | } |
||
381 | |||
382 | while (\class_exists($parent = (string) ($parent ?? $nodeClass)) && ($parent = \get_parent_class($parent))) { |
||
383 | if (! isset($this->renderersByClass[$parent])) { |
||
384 | continue; |
||
385 | } |
||
386 | |||
387 | // "Cache" this result to avoid future loops |
||
388 | return $this->renderersByClass[$nodeClass] = $this->renderersByClass[$parent]; |
||
389 | } |
||
390 | |||
391 | return []; |
||
392 | } |
||
393 | |||
394 | protected function initialize(): void |
||
395 | { |
||
396 | if ($this->initialized) { |
||
397 | return; |
||
398 | } |
||
399 | |||
400 | $this->initializeConfiguration(); |
||
401 | |||
402 | $this->initializeExtensions(); |
||
403 | |||
404 | $this->initialized = true; |
||
405 | } |
||
406 | |||
407 | protected function initializeConfiguration(): void |
||
408 | { |
||
409 | $this->config->addSchema('allow_unsafe_links', Expect::bool(true)); |
||
410 | |||
411 | $default = EmojiConverterInterface::UNICODE; |
||
412 | |||
413 | /** @var string[] $conversionTypes */ |
||
414 | $conversionTypes = (array) EmojiConverterInterface::TYPES; |
||
415 | |||
416 | foreach ($this->extensions as $extension) { |
||
417 | if ($extension instanceof ConfigureConversionTypesInterface) { |
||
418 | $extension->configureConversionTypes($default, $conversionTypes, $this->config->data()); |
||
419 | } |
||
420 | } |
||
421 | |||
422 | $conversionTypes = \array_unique($conversionTypes); |
||
423 | |||
424 | $structuredConversionTypes = Expect::structure(\array_combine( |
||
425 | EmojiConverterInterface::TYPES, |
||
426 | \array_map(static function (string $conversionType) use ($conversionTypes, $default): Schema { |
||
427 | return Expect::anyOf(false, ...$conversionTypes)->default($default)->nullable(); |
||
428 | }, EmojiConverterInterface::TYPES) |
||
429 | ))->castTo('array'); |
||
430 | |||
431 | $this->config->addSchema('convert', Expect::anyOf($structuredConversionTypes, ...$conversionTypes) |
||
432 | ->default(\array_fill_keys(EmojiConverterInterface::TYPES, $default)) |
||
433 | ->before('\UnicornFail\Emoji\Environment\Environment::normalizeConvert')); |
||
434 | |||
435 | $this->config->addSchema('exclude', Expect::structure([ |
||
436 | 'shortcodes' => Expect::arrayOf('string') |
||
437 | ->default([]) |
||
438 | ->before('\UnicornFail\Emoji\Util\Normalize::shortcodes'), |
||
439 | ])->castTo('array')); |
||
440 | |||
441 | $this->config->addSchema('locale', Expect::anyOf(...EmojibaseDatasetInterface::SUPPORTED_LOCALES) |
||
442 | ->default('en') |
||
443 | ->before('\UnicornFail\Emoji\Environment\Environment::normalizeLocale')); |
||
444 | |||
445 | $this->config->addSchema('native', Expect::bool()->nullable()); |
||
446 | |||
447 | $this->config->addSchema('presentation', Expect::anyOf(...EmojibaseDatasetInterface::SUPPORTED_PRESENTATIONS) |
||
448 | ->default(EmojibaseDatasetInterface::EMOJI)); |
||
449 | |||
450 | $this->config->addSchema('preset', Expect::arrayOf('string') |
||
451 | ->default(EmojibaseShortcodeInterface::DEFAULT_PRESETS) |
||
452 | ->mergeDefaults(false) |
||
453 | ->before('\UnicornFail\Emoji\Environment\Environment::normalizePresets')); |
||
454 | } |
||
455 | |||
456 | protected function initializeExtensions(): void |
||
457 | { |
||
458 | if ($this->extensionsInitialized) { |
||
459 | return; |
||
460 | } |
||
461 | |||
462 | // Ask all extensions to register their components. |
||
463 | while (\count($this->uninitializedExtensions) > 0) { |
||
464 | foreach ($this->uninitializedExtensions as $i => $extension) { |
||
465 | $extension->register($this); |
||
466 | unset($this->uninitializedExtensions[$i]); |
||
467 | } |
||
468 | } |
||
469 | |||
470 | $this->extensionsInitialized = true; |
||
471 | } |
||
472 | |||
473 | public function setEventDispatcher(EventDispatcherInterface $dispatcher): void |
||
476 | } |
||
477 | } |
||
478 |