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.

TbBaseMenu::renderMenuItem()   D
last analyzed

Complexity

Conditions 11
Paths 120

Size

Total Lines 39
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 0
cts 33
cp 0
rs 4.9629
c 0
b 0
f 0
cc 11
eloc 24
nc 120
nop 1
crap 132

How to fix   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
 *## TbBaseMenu class file.
4
 *
5
 * @author Christoffer Niska <[email protected]>
6
 * @copyright Copyright &copy; Christoffer Niska 2012-
7
 * @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
8
 */
9
10
Yii::import('zii.widgets.CMenu');
11
12
/**
13
 *## Base class for menu in Booster
14
 *
15
 * @package booster.widgets.navigation
16
 */
17
abstract class TbBaseMenu extends CMenu {
18
	/**
19
	 *### .getDividerCssClass()
20
	 *
21
	 * Returns the divider css class.
22
	 * @return string the class name
23
	 */
24
	abstract public function getDividerCssClass();
25
26
	/**
27
	 *### .getDropdownCssClass()
28
	 *
29
	 * Returns the dropdown css class.
30
	 * @return string the class name
31
	 */
32
	abstract public function getDropdownCssClass();
33
34
	/**
35
	 *### .isVertical()
36
	 *
37
	 * Returns whether this is a vertical menu.
38
	 * @return boolean the result
39
	 */
40
	abstract public function isVertical();
41
42
	/**
43
	 *### .renderMenu()
44
	 *
45
	 * Renders the menu items.
46
	 *
47
	 * @param array $items menu items. Each menu item will be an array with at least two elements: 'label' and 'active'.
48
	 * It may have three other optional elements: 'items', 'linkOptions' and 'itemOptions'.
49
	 */
50
	protected function renderMenu($items) {
51
		
52
		$n = count($items);
53
54
		if ($n > 0) {
55
			echo CHtml::openTag('ul', $this->htmlOptions) . "\n";
56
57
			$count = 0;
58
			foreach ($items as $item) {
59
				$count++;
60
61
				if (isset($item['divider'])) {
62
					echo "<li class=\"{$this->getDividerCssClass()}\"></li>\n";
63
				} else {
64
					$options = isset($item['itemOptions']) ? $item['itemOptions'] : array();
65
					$classes = array();
66
67
					if ($item['active'] && $this->activeCssClass != '') {
68
						$classes[] = $this->activeCssClass;
69
					}
70
71
					if ($count === 1 && $this->firstItemCssClass !== null) {
72
						$classes[] = $this->firstItemCssClass;
73
					}
74
75
					if ($count === $n && $this->lastItemCssClass !== null) {
76
						$classes[] = $this->lastItemCssClass;
77
					}
78
79
					if ($this->itemCssClass !== null) {
80
						$classes[] = $this->itemCssClass;
81
					}
82
83
					if (isset($item['items'])) {
84
						$classes[] = $this->getDropdownCssClass();
85
					}
86
87
					if (isset($item['disabled'])) {
88
						$classes[] = 'disabled';
89
					}
90
91
					if (!empty($classes)) {
92
						$classes = implode(' ', $classes);
93
						if (!empty($options['class'])) {
94
							$options['class'] .= ' ' . $classes;
95
						} else {
96
							$options['class'] = $classes;
97
						}
98
					}
99
100
					echo CHtml::openTag('li', $options) . "\n";
101
102
					$menu = $this->renderMenuItem($item);
103
104
					if (isset($this->itemTemplate) || isset($item['template'])) {
105
						$template = isset($item['template']) ? $item['template'] : $this->itemTemplate;
106
						echo strtr($template, array('{menu}' => $menu));
107
					} else {
108
						echo $menu;
109
					}
110
111
					if (isset($item['items']) && !empty($item['items'])) {
112
						$dropdownOptions = array(
113
							'encodeLabel' => false,
114
							'htmlOptions' => isset($item['submenuOptions']) ? $item['submenuOptions']
115
								: $this->submenuHtmlOptions,
116
							'items' => $item['items'],
117
						);
118
						$dropdownOptions['id'] = isset($dropdownOptions['htmlOptions']['id']) ? 
119
							$dropdownOptions['htmlOptions']['id'] : null;
120
						$this->controller->widget('booster.widgets.TbDropdown', $dropdownOptions);
121
					}
122
123
					echo "</li>\n";
124
				}
125
			}
126
127
			echo "</ul>\n";
128
		}
129
	}
130
131
	/**
132
	 *### .renderMenuItem()
133
	 *
134
	 * Renders the content of a menu item.
135
	 * Note that the container and the sub-menus are not rendered here.
136
	 *
137
	 * @param array $item the menu item to be rendered. Please see {@link items} on what data might be in the item.
138
	 *
139
	 * @return string the rendered item
140
	 */
141
	protected function renderMenuItem($item) {
142
		
143
		if($this->linkLabelWrapper !== null) {
144
            		$item['label'] = CHtml::tag($this->linkLabelWrapper, $this->linkLabelWrapperHtmlOptions, $item['label']);
145
        	}
146
		if (isset($item['icon'])) {
147
			if (strpos($item['icon'], 'icon') === false && strpos($item['icon'], 'fa') === false) {
148
				$item['icon'] = 'glyphicon glyphicon-' . implode(' glyphicon-', explode(' ', $item['icon']));
149
				$item['label'] = "<span class='" . $item['icon'] . "'></span>\r\n" . $item['label'];
150
			} else {
151
				$item['label'] = "<i class='" . $item['icon'] . "'></i>\r\n" . $item['label'];
152
			}
153
		}
154
155
		if (!isset($item['linkOptions'])) {
156
			$item['linkOptions'] = array();
157
		}
158
159
		if (isset($item['items']) && !empty($item['items'])) {
160
			if (empty($item['url'])) {
161
				$item['url'] = '#';
162
			}
163
164
			if (isset($item['linkOptions']['class'])) {
165
				$item['linkOptions']['class'] .= ' dropdown-toggle';
166
			} else {
167
				$item['linkOptions']['class'] = 'dropdown-toggle';
168
			}
169
170
			$item['linkOptions']['data-toggle'] = 'dropdown';
171
			$item['label'] .= ' <span class="caret"></span>';
172
		}
173
174
		if (isset($item['url'])) {
175
			return CHtml::link($item['label'], $item['url'], $item['linkOptions']);
176
		} else {
177
			return $item['label'];
178
		}
179
	}
180
181
	/**
182
	 *### .normalizeItems()
183
	 *
184
	 * Normalizes the {@link items} property so that the 'active' state is properly identified for every menu item.
185
	 *
186
	 * @param array $items the items to be normalized.
187
	 * @param string $route the route of the current request.
188
	 * @param boolean $active whether there is an active child menu item.
189
	 *
190
	 * @return array the normalized menu items
191
	 */
192
	protected function normalizeItems($items, $route, &$active)
193
	{
194
		foreach ($items as $i => $item) {
195
			if (!is_array($item)) {
196
				$item = array('divider' => true);
197
			} else {
198
				if (!isset($item['itemOptions'])) {
199
					$item['itemOptions'] = array();
200
				}
201
202
				$classes = array();
203
204
				if (!isset($item['url']) && !isset($item['items']) && $this->isVertical()) {
205
					$item['header'] = true;
206
					$classes[] = 'nav-header';
207
				}
208
209
				if (!empty($classes)) {
210
					$classes = implode($classes, ' ');
211
					if (isset($item['itemOptions']['class'])) {
212
						$item['itemOptions']['class'] .= ' ' . $classes;
213
					} else {
214
						$item['itemOptions']['class'] = $classes;
215
					}
216
				}
217
			}
218
219
			$items[$i] = $item;
220
		}
221
222
		return parent::normalizeItems($items, $route, $active);
223
	}
224
}
225