Lines 100.00% 54 / 54
Methods 100.00% 15 / 15
Classes 100.00% 1 / 1
Covered by tests of size
Name Lines Methods CRAP
 renderTagList 100.00% 4 / 4 100.00% 1 / 1 2
 renderSearchTag 100.00% 18 / 18 100.00% 1 / 1 2
 getTaggingIds 100.00% 1 / 1 100.00% 1 / 1 1
 setTaggingIds 100.00% 7 / 7 100.00% 1 / 1 1
 renderRelatedTag 100.00% 8 / 8 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setCategory 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] getCategory 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] category 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] plurals 100.00% 1 / 1 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setCategoryRelation 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setTags 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setPlurals 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setSessionId 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] setConfiguration 100.00% 2 / 2 100.00% 1 / 1 1
 [phpMyFAQ\Helper\AbstractHelper] getConfiguration 100.00% 1 / 1 100.00% 1 / 1 1
29class TagsHelper extends AbstractHelper
30{
31    /**
32     * @var int[] Array of Tag IDs
33     */
34    private ?array $taggingIds = null;
35
36    /**
37     * Renders the tag list.
38     *
39     * @param array<int, string> $tags Array of tags.
40     */
41    public function renderTagList(array $tags): string
42    {
43        $tagList = '';
44        foreach ($tags as $tagId => $tagName) {
45            $tagList .= $this->renderSearchTag((int) $tagId, $tagName);
46        }
47
48        return $tagList;
49    }
50
51    /**
52     * Renders a search tag.
53     *
54     * @param int    $tagId   The ID of the tag
55     * @param string $tagName The tag name
56     */
57    public function renderSearchTag(int $tagId, string $tagName): string
58    {
59        $currentIds = array_map(static fn(int $taggingId): string => (string) $taggingId, $this->getTaggingIds());
60        $taggingIds = str_replace((string) $tagId, replace: '', subject: $currentIds);
61        $taggingIds = str_replace(' ', replace: '', subject: $taggingIds);
62        $taggingIds = str_replace(',,', replace: ',', subject: $taggingIds);
63        $taggingIds = trim(implode(separator: ',', array: $taggingIds), characters: ',');
64
65        return $taggingIds !== ''
66            ? sprintf(
67                '<a class="btn btn-outline-primary m-1" href="./search.html?tagging_id=%s">%s '
68                . '<i aria-hidden="true" class="bi bi-dash-square"></i></a> ',
69                $taggingIds,
70                Strings::htmlentities($tagName),
71            )
72            : sprintf(
73                '<a class="btn btn-outline-primary m-1" href="./search.html?search=%s">%s '
74                . '<i aria-hidden="true" class="bi bi-dash-square"></i></a> ',
75                Strings::htmlentities($tagName),
76                Strings::htmlentities($tagName),
77            );
78    }
79
80    /**
81     * Returns all tag IDs as an array.
82     *
83     * @return int[]
84     */
85    public function getTaggingIds(): array
86    {
87        return $this->taggingIds ?? [];
88    }
89
90    /**
91     * Sets the tag IDs.
92     *
93     * @param array<array-key, int|string> $taggingIds The tag IDs as array
94     */
95    public function setTaggingIds(array $taggingIds): void
96    {
97        $this->taggingIds = array_map(
98            static fn(int|string $tagId): int => (int) $tagId,
99            array_filter(
100                $taggingIds,
101                static fn(int|string $tagId): bool => (bool) Filter::filterVar($tagId, FILTER_VALIDATE_INT),
102            ),
103        );
104    }
105
106    /**
107     * Renders the related tag.
108     *
109     * @param int     $tagId     The given Tag ID.
110     * @param string  $tagName   The name of the tag.
111     * @param int     $relevance The relevance of the tag.
112     */
113    public function renderRelatedTag(int $tagId, string $tagName, int $relevance): string
114    {
115        return sprintf(
116            '<a class="btn btn-outline-primary m-1" href="./search.html?tagging_id=%s">%s %s '
117            . '<span class="badge bg-info">%d</span></a>',
118            implode(',', $this->getTaggingIds()) . ',' . $tagId,
119            '<i aria-hidden="true" class="bi bi-plus-square"></i> ',
120            Strings::htmlentities($tagName),
121            $relevance,
122        );
123    }
124}

Inherited from phpMyFAQ\Helper\AbstractHelper

47    public function setCategory(Category $Category): AbstractHelper
48    {
49        $this->Category = $Category;
50        return $this;
51    }
53    public function getCategory(): Category
54    {
55        return $this->category();
56    }
61    protected function category(): Category
62    {
63        return $this->Category ?? throw new \LogicException('setCategory() must be called before use.');
64    }
69    protected function plurals(): Plurals
70    {
71        return $this->plurals ?? throw new \LogicException('setPlurals() must be called before use.');
72    }
74    public function setCategoryRelation(Relation $categoryRelation): AbstractHelper
75    {
76        $this->categoryRelation = $categoryRelation;
77        return $this;
78    }
80    public function setTags(Tags $Tags): AbstractHelper
81    {
82        $this->Tags = $Tags;
83        return $this;
84    }
86    public function setPlurals(Plurals $plurals): AbstractHelper
87    {
88        $this->plurals = $plurals;
89        return $this;
90    }
92    public function setSessionId(int|string $sid): AbstractHelper
93    {
94        $this->sessionId = $sid;
95        return $this;
96    }
98    public function setConfiguration(Configuration $configuration): AbstractHelper
99    {
100        $this->configuration = $configuration;
101        return $this;
102    }
104    public function getConfiguration(): Configuration
105    {
106        return $this->configuration;
107    }