| Conditions | 15 |
| Paths | 24 |
| Total Lines | 66 |
| Code Lines | 38 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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|float|string|null $size */ |
||
| 37 | $size = $this->config->get('twemoji.size'); |
||
| 38 | |||
| 39 | $inline = (bool) $this->config->get('twemoji.inline'); |
||
| 40 | |||
| 41 | $type = (string) $this->config->get('twemoji.type'); |
||
| 42 | |||
| 43 | foreach ($e->getDocument()->getNodes() as $node) { |
||
| 44 | if (! ($node instanceof Emoji) || $node->hexcode === null) { |
||
| 45 | continue; |
||
| 46 | } |
||
| 47 | |||
| 48 | $parsedType = $node->getParsedType(); |
||
| 49 | $configPath = 'convert.' . (EmojiConverterInterface::TYPES[$parsedType] ?? ''); |
||
| 50 | $conversionType = null; |
||
| 51 | |||
| 52 | if ($this->config->exists($configPath)) { |
||
| 53 | $conversionType = (string) ($this->config->get($configPath) ?? ''); |
||
| 54 | } |
||
| 55 | |||
| 56 | // Only convert types that are set to "twemoji". |
||
| 57 | if ($conversionType !== self::CONVERSION_TYPE) { |
||
| 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, $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 | // Ensure image isn't massive and relative to its surroundings by inlining it. |
||
| 82 | if ($inline && $size === null) { |
||
| 83 | $image->setAttribute('style', 'width: 1em; height: 1em; vertical-align: middle;'); |
||
| 84 | } elseif ($inline && $size !== null) { |
||
| 85 | if (! \is_string($size)) { |
||
| 86 | $size .= 'em'; |
||
| 87 | } |
||
| 88 | |||
| 89 | $image->setAttribute('style', \sprintf('width: %s; height: %s; vertical-align: middle;', $size, $size)); |
||
| 90 | } elseif ($size !== null) { |
||
| 91 | $image->setAttribute('height', (string) $size); |
||
| 92 | $image->setAttribute('width', (string) $size); |
||
| 93 | } |
||
| 94 | |||
| 95 | $node->replaceWith($image); |
||
| 96 | } |
||
| 104 |