Test Failed
Pull Request — master (#35)
by Wilmer
03:25 queued 01:10
created

Accordion::renderItem()   F

Complexity

Conditions 16
Paths 625

Size

Total Lines 86
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 25.4786

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 16
eloc 54
c 2
b 0
f 0
nc 625
nop 3
dl 0
loc 86
ccs 6
cts 9
cp 0.6667
crap 25.4786
rs 1.9208

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Bootstrap5;
6
7
use JsonException;
8
use RuntimeException;
9
use Yiisoft\Arrays\ArrayHelper;
10
use Yiisoft\Html\Html;
11
12
use function array_column;
13
use function array_key_exists;
14
use function array_merge;
15
use function array_search;
16
use function implode;
17
use function is_array;
18
use function is_int;
19
use function is_numeric;
20
use function is_object;
21
use function is_string;
22
23
/**
24
 * Accordion renders an accordion bootstrap javascript component.
25
 *
26
 * For example:
27
 *
28
 * ```php
29
 * echo Accordion::widget()
30
 *     ->witItems([
31
 *         [
32
 *             'label' => 'Collapsible Group Item #1',
33
 *             'content' => 'Anim pariatur cliche...',
34
 *             // open its content by default
35
 *             'contentOptions' => ['class' => 'show'],
36
 *         ],
37
 *         // another group item
38
 *         [
39
 *             'label' => 'Collapsible Group Item #2',
40
 *             'content' => 'Anim pariatur cliche...',
41
 *             'contentOptions' => [...],
42
 *             'options' => [...],
43
 *             'expand' => true,
44
 *         ],
45
 *         // if you want to swap out .accordion-body with .list-group, you may provide an array
46
 *         [
47
 *             'label' => 'Collapsible Group Item #3',
48
 *             'content' => [
49
 *                 'Anim pariatur cliche...',
50
 *                 'Anim pariatur cliche...',
51
 *             ],
52
 *             'contentOptions' => [...],
53
 *             'options' => [...],
54
 *         ],
55
 *     ]);
56
 * ```
57
 */
58
final class Accordion extends Widget
59
{
60
    private array $items = [];
61
    private bool $encodeLabels = true;
62
    private bool $encodeTags = false;
63
    private bool $autoCloseItems = true;
64
    private array $itemToggleOptions = [];
65
    private array $options = [];
66 7
67
    public function run(): string
68 7
    {
69 7
        if (!isset($this->options['id'])) {
70
            $this->options['id'] = "{$this->getId()}-accordion";
71
        }
72 7
73
        /** @psalm-suppress InvalidArgument */
74
        Html::addCssClass($this->options, 'accordion');
75 7
76
        if ($this->encodeTags === false) {
77 7
            $this->options = array_merge($this->options, ['encode' => false]);
78
        }
79
80
        return Html::div($this->renderItems(), $this->options);
81
    }
82
83
    /**
84
     * Whether to close other items if an item is opened. Defaults to `true` which causes an accordion effect.
85
     *
86
     * Set this to `false` to allow keeping multiple items open at once.
87 7
     *
88
     * @param bool $value
89 7
     *
90 7
     * @return $this
91 7
     */
92
    public function withoutAutoCloseItems(bool $value = false): self
93 7
    {
94 7
        $new = clone $this;
95 1
        $new->autoCloseItems = $value;
96
97
        return $new;
98 7
    }
99 6
100
    /**
101
     * Whether the labels for header items should be HTML-encoded.
102 7
     *
103 3
     * @param bool $value
104 3
     *
105
     * @return $this
106
     */
107
    public function withoutEncodeLabels(bool $value = false): self
108
    {
109
        $new = clone $this;
110 4
        $new->encodeLabels = $value;
111 4
112
        return $new;
113 4
    }
114
115 4
    /**
116
     * List of groups in the collapse widget. Each array element represents a single group with the following structure:
117
     *
118 4
     * - label: string, required, the group header label.
119
     * - encode: bool, optional, whether this label should be HTML-encoded. This param will override global
120
     *   `$this->encodeLabels` param.
121
     * - content: array|string|object, required, the content (HTML) of the group
122
     * - options: array, optional, the HTML attributes of the group
123
     * - contentOptions: optional, the HTML attributes of the group's content
124
     *
125
     * You may also specify this property as key-value pairs, where the key refers to the `label` and the value refers
126
     * to `content`. If value is a string it is interpreted as label. If it is an array, it is interpreted as explained
127
     * above.
128
     *
129
     * For example:
130
     *
131
     * ```php
132 4
     * echo Accordion::widget([
133
     *     'withItems' => [
134 4
     *       'Introduction' => 'This is the first collapsible menu',
135 4
     *       'Second panel' => [
136 4
     *           'content' => 'This is the second collapsible menu',
137 4
     *       ],
138 4
     *       [
139
     *           'label' => 'Third panel',
140 4
     *           'content' => 'This is the third collapsible menu',
141
     *       ],
142 4
     *   ]
143 4
     * ])
144
     * ```
145
     *
146 4
     * @param array $value
147 4
     *
148
     * @return $this
149
     */
150 4
    public function withItems(array $value): self
151
    {
152 4
        $new = clone $this;
153 4
        $new->items = $value;
154
155
        return $new;
156 4
    }
157 4
158 4
    /**
159 4
     * The HTML options for the item toggle tag. Key 'tag' might be used here for the tag name specification.
160 4
     *
161 4
     * For example:
162 4
     *
163 4
     * ```php
164
     * [
165
     *     'tag' => 'div',
166 4
     *     'class' => 'custom-toggle',
167 1
     * ]
168 1
     * ```
169
     *
170 3
     * @param array $value
171 3
     *
172 3
     * @return $this
173
     */
174 3
    public function withItemToggleOptions(array $value): self
175
    {
176
        $new = clone $this;
177 4
        $new->itemToggleOptions = $value;
178 4
179 1
        return $new;
180 1
    }
181 1
182
    /**
183
     * The HTML attributes for the widget container tag. The following special options are recognized.
184
     *
185
     * {@see Html::renderTagAttributes()} for details on how attributes are being rendered.
186 1
     *
187
     * @param array $value
188 4
     *
189
     * @return $this
190
     */
191
    public function withOptions(array $value): self
192
    {
193
        $new = clone $this;
194 4
        $new->options = $value;
195
196 4
        return $new;
197 4
    }
198
199
    /**
200
     * Allows you to enable or disable the encoding tags html.
201 4
     *
202 4
     * @param bool $value
203
     *
204 4
     * @return self
205
     */
206
    public function withEncodeTags(bool $value = true): self
207
    {
208
        $new = clone $this;
209
        $new->encodeTags = $value;
210
211
        return $new;
212
    }
213
214
    /**
215
     * Renders collapsible items as specified on {@see items}.
216 1
     *
217
     * @throws JsonException|RuntimeException
218 1
     *
219
     * @return string the rendering result
220 1
     */
221
    private function renderItems(): string
222
    {
223
        $items = [];
224
        $index = 0;
225
        $expanded = array_search(true, array_column($this->items, 'expand'), true);
226
227
        foreach ($this->items as $key => $item) {
228
            if (!is_array($item)) {
229
                $item = ['content' => $item];
230
            }
231
232
            if ($expanded === false && $index === 0) {
233
                $item['expand'] = true;
234
            }
235
236
            if (!array_key_exists('label', $item)) {
237
                throw new RuntimeException('The "label" option is required.');
238
            }
239
240
            $header = ArrayHelper::remove($item, 'label');
241
            $options = ArrayHelper::getValue($item, 'options', []);
242
243
            if ($this->encodeTags === false) {
244
                $options = array_merge($options, ['encode' => false]);
245
            }
246
247
            Html::addCssClass($options, ['panel' => 'accordion-item']);
248
249
            $items[] = Html::div($this->renderItem($header, $item, $index++), $options);
0 ignored issues
show
Bug introduced by
It seems like $item can also be of type object; however, parameter $item of Yiisoft\Yii\Bootstrap5\Accordion::renderItem() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

249
            $items[] = Html::div($this->renderItem($header, /** @scrutinizer ignore-type */ $item, $index++), $options);
Loading history...
Bug introduced by
It seems like $header can also be of type null; however, parameter $header of Yiisoft\Yii\Bootstrap5\Accordion::renderItem() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

249
            $items[] = Html::div($this->renderItem(/** @scrutinizer ignore-type */ $header, $item, $index++), $options);
Loading history...
250
        }
251
252
        return implode("\n", $items);
253
    }
254
255
    /**
256
     * Renders a single collapsible item group.
257
     *
258
     * @param string $header a label of the item group {@see items}
259
     * @param array $item a single item from {@see items}
260
     * @param int $index the item index as each item group content must have an id
261
     *
262
     * @throws JsonException|RuntimeException
263
     *
264
     * @return string the rendering result
265
     */
266
    private function renderItem(string $header, array $item, int $index): string
267
    {
268
        if (array_key_exists('content', $item)) {
269
            $id = $this->options['id'] . '-collapse' . $index;
270
            $expand = ArrayHelper::remove($item, 'expand', false);
271
            $options = ArrayHelper::getValue($item, 'contentOptions', []);
272 7
            $options['id'] = $id;
273
274 7
            Html::addCssClass($options, ['accordion-body', 'collapse']);
275
276 7
            if ($expand) {
277
                Html::addCssClass($options, 'show');
278
            }
279
280
            if (!isset($options['aria-label'], $options['aria-labelledby'])) {
281
                $options['aria-labelledby'] = $options['id'] . '-heading';
282
            }
283
284
            $encodeLabel = $item['encode'] ?? $this->encodeLabels;
285
286
            if ($encodeLabel) {
287
                $header = Html::encode($header);
288
            }
289
290
            $itemToggleOptions = array_merge([
291
                'tag' => 'button',
292
                'type' => 'button',
293
                'data-bs-toggle' => 'collapse',
294
                'data-bs-target' => '#' . $options['id'],
295 1
                'aria-expanded' => $expand ? 'true' : 'false',
296
            ], $this->itemToggleOptions);
297 1
298
            if ($this->encodeTags === false) {
299 1
                $itemToggleOptions = array_merge($itemToggleOptions, ['encode' => false]);
300
            }
301
302
            $itemToggleTag = ArrayHelper::remove($itemToggleOptions, 'tag', 'button');
303
304
            /** @psalm-suppress ConflictingReferenceConstraint */
305
            if ($itemToggleTag === 'a') {
306
                ArrayHelper::remove($itemToggleOptions, 'data-bs-target');
307
                $header = Html::a($header, '#' . $id, $itemToggleOptions) . "\n";
308
            } else {
309
                Html::addCssClass($itemToggleOptions, 'accordion-button');
310
                if (!$expand) {
311
                    Html::addCssClass($itemToggleOptions, 'collapsed');
312
                }
313
                $header = Html::button($header, $itemToggleOptions);
314
            }
315
316
            if (is_string($item['content']) || is_numeric($item['content']) || is_object($item['content'])) {
317
                $content = $item['content'];
318
            } elseif (is_array($item['content'])) {
319
                $ulOptions = ['class' => 'list-group'];
320
                $ulItemOptions = ['itemOptions' => ['class' => 'list-group-item']];
321
322
                if ($this->encodeTags === false) {
323
                    $ulOptions = array_merge($ulOptions, ['encode' => false]);
324
                    $ulItemOptions['itemOptions'] = array_merge($ulItemOptions['itemOptions'], ['encode' => false]);
325
                }
326
327
                $content = Html::ul($item['content'], array_merge($ulOptions, $ulItemOptions)) . "\n";
328
            } else {
329
                throw new RuntimeException('The "content" option should be a string, array or object.');
330
            }
331
        } else {
332
            throw new RuntimeException('The "content" option is required.');
333
        }
334
335
        $group = [];
336
337
        if ($this->autoCloseItems) {
338
            $options['data-bs-parent'] = '#' . $this->options['id'];
339
        }
340
341
        $groupOptions = ['class' => 'accordion-header', 'id' => $options['id'] . '-heading'];
342
343
        if ($this->encodeTags === false) {
344
            $options = array_merge($options, ['encode' => false]);
345
            $groupOptions = array_merge($groupOptions, ['encode' => false]);
346
        }
347
348
        $group[] = Html::tag('h2', $header, $groupOptions);
349
        $group[] = Html::div($content, $options);
350
351
        return implode("\n", $group);
352
    }
353
}
354