RenderMenu::renderRecursive()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
rs 9.2
cc 4
eloc 9
nc 3
nop 1
1
<?php
2
/**
3
 * @link http://www.writesdown.com/
4
 * @copyright Copyright (c) 2015 WritesDown
5
 * @license http://www.writesdown.com/license/
6
 */
7
8
namespace backend\widgets;
9
10
use common\models\MenuItem;
11
use Yii;
12
use yii\base\Widget;
13
use yii\helpers\Html;
14
15
/**
16
 * Class MenuRenderer to render menu item in admin menu.
17
 *
18
 * @author Agiel K. Saputra <[email protected]>
19
 * @since 0.1.0
20
 */
21
class RenderMenu extends Widget
22
{
23
    /**
24
     * @var array
25
     */
26
    public $items = [];
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function run()
32
    {
33
        echo Html::beginTag('ul', ['class' => 'list-menu dd-list']);
34
35
        if ($this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
36
            $this->renderRecursive($this->items);
37
        }
38
39
        echo Html::endTag('ul');
40
    }
41
42
    /**
43
     * Render menu item recursively.
44
     *
45
     * @param MenuItem[] $items
46
     */
47
    protected function renderRecursive($items)
48
    {
49
        foreach ($items as $item) {
50
            echo Html::beginTag('li', ['class' => 'dd-item', 'data-id' => $item->id]);
51
            echo $this->getView()->render('_render-item', ['item' => $item]);
52
            if (isset($item->items) && count($item->items)) {
53
                echo Html::beginTag('ul', ['class' => 'dd-list children']);
54
                $this->renderRecursive($item['items']);
55
                echo Html::endTag('ul');
56
            }
57
            echo Html::endTag('li');
58
        }
59
    }
60
}
61