GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

NavigationWidget::getTree()   B
last analyzed

Complexity

Conditions 6
Paths 12

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 8.8977
c 0
b 0
f 0
cc 6
nc 12
nop 2
1
<?php
2
3
namespace app\widgets\navigation;
4
5
use app\widgets\navigation\models\Navigation;
6
use Yii;
7
use yii\base\Widget;
8
use yii\caching\TagDependency;
9
use yii\helpers\ArrayHelper;
10
use yii\helpers\Html;
11
use yii\helpers\Json;
12
use app\components\Menu;
13
14
class NavigationWidget extends Widget
15
{
16
    public $prependItems;
17
    public $appendItems;
18
    public $options;
19
    public $rootId = 1;
20
    public $depth = 99;
21
    public $useCache = true;
22
    public $viewFile = 'navigation';
23
    public $widget = '';
24
    public $linkTemplate = '<a href="{url}" title="{label}" itemprop="url"><span itemprop="name">{label}</span></a>';
25
    public $submenuTemplate = "\n<ul>\n{items}\n</ul>\n";
26
27
    public function init()
28
    {
29
        $schema = [
30
            'role'=> "navigation",
31
            'itemscope' => '',
32
            'itemtype' => "http://schema.org/SiteNavigationElement",
33
34
        ];
35
        if (!trim($this->widget)) {
36
            $this->widget = Menu::className();
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
37
        }
38
        if (!is_array($this->options)) {
39
            $this->options = $schema;
40
        } else {
41
            $this->options = ArrayHelper::merge($schema, $this->options);
42
        }
43
        Html::addCssClass($this->options, 'navigation-widget');
44
    }
45
46
    public function run()
47
    {
48
        Yii::beginProfile("NavigationWidget for ".$this->rootId);
49
        $items = null;
50
        $cacheKey = implode(
51
            ':',
52
            [
53
                'Navigation',
54
                $this->rootId,
55
                $this->depth,
56
                $this->viewFile
57
            ]
58
        );
59
        if ($this->useCache) {
60
            if (false === $items = \Yii::$app->cache->get($cacheKey)) {
61
                $items = null;
62
            }
63
        }
64
        if (null === $items) {
65
            $root = Navigation::find()
66
                ->where(['id' => $this->rootId])
67
                ->with('children')
68
                ->orderBy(['sort_order' => SORT_ASC])
69
                ->one();
70
            $items = [];
71
            if ($this->depth > 0) {
72
                foreach ($root->children as $child) {
73
                    $items[] = self::getTree($child, $this->depth - 1);
74
                }
75
            }
76 View Code Duplication
            if (count($items) > 0) {
77
                \Yii::$app->cache->set(
78
                    $cacheKey,
79
                    $items,
80
                    86400,
81
                    new TagDependency([
82
                        'tags' => [
83
                            \devgroup\TagDependencyHelper\ActiveRecordHelper::getCommonTag(Navigation::className())
0 ignored issues
show
Deprecated Code introduced by
The method yii\base\BaseObject::className() has been deprecated with message: since 2.0.14. On PHP >=5.5, use `::class` instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
84
                        ]
85
                    ])
86
                );
87
            }
88
        }
89
        $items = ArrayHelper::merge((array) $this->prependItems, $items, (array) $this->appendItems);
90
        $currentUri = Yii::$app->request->url;
91
        array_walk($items, function(&$item) use ($currentUri) {
92
            if ($item['url'] === $currentUri) {
93
                $item['active'] = true;
94
            }
95
        });
96
97
        $result = $this->render(
98
            $this->viewFile,
99
            [
100
                'widget' => $this->widget,
101
                'items' => $items,
102
                'options' => $this->options,
103
                'linkTemplate' => $this->linkTemplate,
104
                'submenuTemplate' => $this->submenuTemplate,
105
            ]
106
        );
107
        Yii::endProfile("NavigationWidget for ".$this->rootId);
108
        return $result;
109
    }
110
111
    /**
112
     * @param Navigation $model
113
     * @param int $depth
114
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,Navigation|string|array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
115
     */
116
    private static function getTree($model, $depth)
117
    {
118
        if (trim($model->url)) {
119
            $url = trim($model->url);
120
        } else {
121
            $params = (trim($model->route_params)) ? Json::decode($model->route_params) : [];
122
            $url = ArrayHelper::merge([$model->route], $params);
123
        }
124
        $tree = [
125
            'model' => $model,
126
            'label' => $model->name,
127
            'url' => $url,
128
            'items' => [],
129
            'options' => [],
130
        ];
131
        if (!empty($model->advanced_css_class)) {
132
            $tree['options'] = ['class' => $model->advanced_css_class];
133
        }
134
        if ($depth > 0) {
135
            foreach ($model->children as $child) {
136
                $tree['items'][] = self::getTree($child, $depth - 1);
137
            }
138
        }
139
        return $tree;
140
    }
141
}
142