Lines 100.00% 26 / 26
Methods 100.00% 2 / 2
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 extractMatchParts 100.00% 19 / 19 100.00% 1 / 1 8
 formatTooltip 100.00% 7 / 7 100.00% 1 / 1 1
22final class GlossaryHelper
23{
24    /**
25     * Extract prefix, item, postfix from regex matches produced by pattern: /(^|\W)(item)(\W|$)/
26     * keeping backward-compatibility with older, more verbose patterns.
27     *
28     * @param array<int, string> $matches
29     * @return array{0:string,1:string,2:string} [prefix, item, postfix]
30     */
31    public function extractMatchParts(array $matches): array
32    {
33        $prefix = '';
34        $item = '';
35        $postfix = '';
36        $count = count($matches);
37
38        if ($count > 9) {
39            $prefix = $matches[9];
40            $item = $matches[10];
41        }
42
43        if ($item === '' && $count > 7) {
44            $item = $matches[7];
45            $postfix = $matches[8];
46        }
47
48        if ($item === '' && $count > 4) {
49            $prefix = $matches[4];
50            $item = $matches[5];
51            $postfix = $matches[6];
52        }
53
54        if ($item === '' && $count >= 3) {
55            $prefix = $matches[1] ?? '';
56            $item = $matches[2] ?? '';
57            $postfix = $matches[3] ?? '';
58        }
59
60        return [$prefix, $item, $postfix];
61    }
62
63    public function formatTooltip(string $definition, string $item, string $prefix = '', string $postfix = ''): string
64    {
65        return sprintf(
66            '%s<abbr data-bs-toggle="tooltip" data-bs-placement="bottom" title="%s" class="initialism">%s</abbr>%s',
67            $prefix,
68            htmlspecialchars($definition, ENT_QUOTES | ENT_HTML5, encoding: 'UTF-8'),
69            $item,
70            $postfix,
71        );
72    }
73}