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   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 128
Duplicated Lines 9.38 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 0
Metric Value
wmc 17
lcom 1
cbo 10
dl 12
loc 128
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 18 3
B run() 12 64 8
B getTree() 0 25 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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