Conditions | 15 |
Paths | 24 |
Total Lines | 68 |
Code Lines | 40 |
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|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 | $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, $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 | // Ensure image isn't massive and relative to its surroundings by inlining it. |
||
84 | if ($inline && $size === null) { |
||
85 | $image->attributes->set('style', 'width: 1em; height: 1em; vertical-align: middle;'); |
||
86 | } elseif ($inline && $size !== null) { |
||
87 | if (! \is_string($size)) { |
||
88 | $size .= 'em'; |
||
89 | } |
||
90 | |||
91 | $image->attributes->set('style', \sprintf('width: %s; height: %s; vertical-align: middle;', $size, $size)); |
||
92 | } elseif ($size !== null) { |
||
93 | $image->attributes->set('height', (string) $size); |
||
94 | $image->attributes->set('width', (string) $size); |
||
95 | } |
||
96 | |||
97 | $node->replaceWith($image); |
||
98 | } |
||
106 |