| Conditions | 14 |
| 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 |
||
| 30 | public function __invoke(DocumentParsedEvent $e): void |
||
| 31 | { |
||
| 32 | $urlBase = (string) $this->config->get('twemoji.urlBase'); |
||
| 33 | |||
| 34 | $classPrefix = (string) $this->config->get('twemoji.classPrefix'); |
||
| 35 | |||
| 36 | /** @var ?int $size */ |
||
| 37 | $size = $this->config->get('twemoji.size'); |
||
| 38 | |||
| 39 | $inlineSvg = (bool) $this->config->get('twemoji.inlineSvg'); |
||
| 40 | |||
| 41 | $type = (string) $this->config->get('twemoji.type'); |
||
| 42 | |||
| 43 | $walker = $e->getDocument()->walker(); |
||
| 44 | while ($event = $walker->next()) { |
||
| 45 | $node = $event->getNode(); |
||
| 46 | if (! ($node instanceof Emoji) || $node->hexcode === null) { |
||
| 47 | continue; |
||
| 48 | } |
||
| 49 | |||
| 50 | $parsedType = $node->getParsedType(); |
||
| 51 | $configPath = 'convert.' . (EmojiConverterInterface::TYPES[$parsedType] ?? ''); |
||
| 52 | $conversionType = null; |
||
| 53 | |||
| 54 | if ($this->config->exists($configPath)) { |
||
| 55 | $conversionType = (string) ($this->config->get($configPath) ?? ''); |
||
| 56 | } |
||
| 57 | |||
| 58 | // Only convert types that are set to "twemoji". |
||
| 59 | if ($conversionType !== self::CONVERSION_TYPE) { |
||
| 60 | continue; |
||
| 61 | } |
||
| 62 | |||
| 63 | $url = \sprintf( |
||
| 64 | '%s/%s/%s.%s', |
||
| 65 | $urlBase, |
||
| 66 | $type === 'png' ? '72x72' : 'svg', |
||
| 67 | \strtolower($node->hexcode), |
||
| 68 | $type |
||
| 69 | ); |
||
| 70 | |||
| 71 | $image = new Image($node->getParsedValue(), $node, $url, $node->annotation); |
||
| 72 | |||
| 73 | /** @var string[] $classes */ |
||
| 74 | $classes = (array) $this->config->get('twemoji.classes'); |
||
| 75 | $image->addClass(...$classes); |
||
| 76 | |||
| 77 | if ($node->annotation !== null) { |
||
| 78 | $image->addClass(HtmlElement::cleanCssIdentifier($classPrefix |
||
| 79 | ? $classPrefix . $node->annotation |
||
| 80 | : $node->annotation)); |
||
| 81 | } |
||
| 82 | |||
| 83 | switch ($type) { |
||
| 84 | case 'png': |
||
| 85 | // Set default size for PNG images. |
||
| 86 | if ($size === null) { |
||
| 87 | $size = 72; |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($size) { |
||
| 91 | $image->attributes->set('height', (string) $size); |
||
| 92 | $image->attributes->set('width', (string) $size); |
||
| 93 | } |
||
| 94 | |||
| 95 | break; |
||
| 96 | |||
| 97 | case 'svg': |
||
| 98 | // Ensure SVG images aren't massive and relative to its surroundings. |
||
| 99 | if ($inlineSvg) { |
||
| 100 | $image->attributes->set('style', 'width: 1em; height: 1em;'); |
||
| 101 | } |
||
| 102 | |||
| 103 | break; |
||
| 104 | } |
||
| 105 | |||
| 106 | $node->replaceWith($image); |
||
| 107 | } |
||
| 115 |