Sidebar::renderItem()   C
last analyzed

Complexity

Conditions 12
Paths 12

Size

Total Lines 22
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 22
rs 6.9666
c 0
b 0
f 0
cc 12
nc 12
nop 1

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
/**
4
 * (c) CJT TERABYTE INC
5
 * For the full copyright and license information, please view the LICENSE.md
6
 * file that was distributed with this source code.
7
 *
8
 *        @link: https://github.com/cjtterabytesoft/sidebar
9
 *      @author: Wilmer Arámbula <[email protected]>
10
 *   @copyright: (c) CJT TERABYTE INC
11
 *      @widget: [Sidebar]
12
 *       @since: 1.0
13
 *         @yii: 3.0
14
 **/
15
16
namespace cjtterabytesoft\widgets;
17
18
use yii\helpers\ArrayHelper;
19
use yii\helpers\Html;
20
use yii\helpers\Url;
21
use yii\widgets\Menu;
22
23
class Sidebar extends Menu
24
{
25
	/**
26
	 * @var string
27
	 */
28
	public $linkTemplate = '<a href="{url}" {linkOptions}>\n{icon}\n{label}\n{right-icon}\n{badge}</a>';
29
	/**
30
	 * @var string
31
	 */
32
	public $labelTemplate = '{icon}\n{label}\n{badge}';
33
	/**
34
	 * @var string
35
	 */
36
	public $submenuTemplate = "\n<ul class=\"dropdown-menu\">\n{items}\n</ul>\n";
37
	/**
38
	 * @var string
39
	 */
40
	public $badgeTag = 'small';
41
	/**
42
	 * @var string
43
	 */
44
	public $badgeClass = 'badge pull-right';
45
	/**
46
	 * @var string
47
	 */
48
	public $badgeBgClass = 'bg-green';
49
	/**
50
	 * @var string
51
	 */
52
	public $parentRightIcon = '<span class="arrow"><i class="ti-angle-right"></i></span>';
53
	/**
54
	 * @inheritdoc
55
	 */
56
	protected function renderItem($item)
57
	{
58
		$item['badgeOptions'] = isset($item['badgeOptions']) ? $item['badgeOptions'] : [];
59
		if (!ArrayHelper::getValue($item, 'badgeOptions.class')) {
60
			$bg = isset($item['badgeBgClass']) ? $item['badgeBgClass'] : $this->badgeBgClass;
61
			$item['badgeOptions']['class'] = $this->badgeClass . ' ' . $bg;
62
		}
63
		if (isset($item['items']) && !isset($item['right-icon'])) {
64
			$item['right-icon'] = $this->parentRightIcon;
65
		}
66
		$template = ArrayHelper::getValue(
67
			$item,
68
			'template',
69
			isset($item['url']) ? $this->linkTemplate : $this->labelTemplate
70
		);
71
		return strtr($template, [
72
			'{badge}' => isset($item['badge']) ? Html::tag('small', $item['badge'], $item['badgeOptions']) : '',
73
			'{icon}' => isset($item['icon']) ? $item['icon'] : '',
74
			'{right-icon}' => isset($item['right-icon']) ? $item['right-icon'] : '',
75
			'{url}' => isset($item['url']) ? Url::to($item['url']) : '',
76
			'{label}' => $item['label'],
77
			'{linkOptions}' => isset($item['linkOptions']) ? Html::renderTagAttributes($item['linkOptions']) : '',
78
		]);
79
	}
80
}
81