| Conditions | 15 |
| Paths | 32 |
| Total Lines | 77 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 28 | public function __invoke(DocumentParsedEvent $e): void |
||
| 29 | { |
||
| 30 | $urlBase = (string) $this->config->get('twemoji.urlBase'); |
||
| 31 | |||
| 32 | $classPrefix = (string) $this->config->get('twemoji.classPrefix'); |
||
| 33 | |||
| 34 | /** @var ?int $size */ |
||
| 35 | $size = $this->config->get('twemoji.size'); |
||
| 36 | |||
| 37 | $relativeSvg = (bool) $this->config->get('twemoji.relativeSvg'); |
||
| 38 | |||
| 39 | $type = (string) $this->config->get('twemoji.type'); |
||
| 40 | |||
| 41 | $walker = $e->getDocument()->walker(); |
||
| 42 | while ($event = $walker->next()) { |
||
| 43 | $node = $event->getNode(); |
||
| 44 | if (! ($node instanceof Emoji) || $node->hexcode === null) { |
||
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | $parsedType = $node->getParsedType(); |
||
| 49 | $configPath = EmojiCoreProcessor::CONFIG_PATHS[$parsedType] ?? null; |
||
| 50 | $convertTo = null; |
||
| 51 | |||
| 52 | if ($configPath && $this->config->exists($configPath)) { |
||
| 53 | $convertTo = (string) ($this->config->get($configPath) ?? ''); |
||
| 54 | } |
||
| 55 | |||
| 56 | // Only convert types that are twemoji. |
||
| 57 | if ($convertTo !== 'twemoji') { |
||
| 58 | continue; |
||
| 59 | } |
||
| 60 | |||
| 61 | $url = \sprintf( |
||
| 62 | '%s/%s/%s.%s', |
||
| 63 | $urlBase, |
||
| 64 | $type === 'png' ? '72x72' : 'svg', |
||
| 65 | \strtolower($node->hexcode), |
||
| 66 | $type |
||
| 67 | ); |
||
| 68 | |||
| 69 | $image = new Image($node->getParsedValue(), $node, $url, $node->annotation); |
||
| 70 | |||
| 71 | /** @var string[] $classes */ |
||
| 72 | $classes = (array) $this->config->get('twemoji.classes'); |
||
| 73 | $image->addClass(...$classes); |
||
| 74 | |||
| 75 | if ($node->annotation !== null) { |
||
| 76 | $image->addClass(HtmlElement::cleanCssIdentifier($classPrefix |
||
| 77 | ? $classPrefix . $node->annotation |
||
| 78 | : $node->annotation)); |
||
| 79 | } |
||
| 80 | |||
| 81 | switch ($type) { |
||
| 82 | case 'png': |
||
| 83 | // Set default size for PNG images. |
||
| 84 | if ($size === null) { |
||
| 85 | $size = 72; |
||
| 86 | } |
||
| 87 | |||
| 88 | if ($size) { |
||
| 89 | $image->attributes->set('height', (string) $size); |
||
| 90 | $image->attributes->set('width', (string) $size); |
||
| 91 | } |
||
| 92 | |||
| 93 | break; |
||
| 94 | |||
| 95 | case 'svg': |
||
| 96 | // Ensure SVG images aren't massive and relative to its surroundings. |
||
| 97 | if ($relativeSvg) { |
||
| 98 | $image->attributes->set('style', 'width: 1em; height: 1em;'); |
||
| 99 | } |
||
| 100 | |||
| 101 | break; |
||
| 102 | } |
||
| 103 | |||
| 104 | $node->replaceWith($image); |
||
| 105 | } |
||
| 113 |