Passed
Push — master ( da58ee...4c81df )
by John
02:17
created

list_groups()   C

Complexity

Conditions 13
Paths 144

Size

Total Lines 65
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 40
nc 144
nop 5
dl 0
loc 65
rs 6.25
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A um_admin_areas() 0 15 1

How to fix   Long Method    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
 * @package Ultimate Menu mod
5
 * @version 1.0.3
6
 * @author John Rayes <[email protected]>
7
 * @copyright Copyright (c) 2014, John Rayes
8
 * @license http://opensource.org/licenses/MIT MIT
9
 */
10
11
if (!defined('SMF'))
12
	die('Hacking attempt...');
13
14
function um_load_menu(&$menu_buttons)
15
{
16
	global $smcFunc, $user_info, $scripturl, $context, $modSettings;
17
18
	// Make damn sure we ALWAYS load last. Priority: 100!
19
	if (substr($modSettings['integrate_menu_buttons'], -12) === 'um_load_menu')
20
	{
21
		remove_integration_function('integrate_menu_buttons', 'um_load_menu');
22
		add_integration_function('integrate_menu_buttons', 'um_load_menu');
23
	}
24
25
	$num_buttons = isset($modSettings['um_count'])
26
		? $modSettings['um_count']
27
		: 0;
28
29
	for ($i = 1; $i <= $num_buttons; $i++)
30
	{
31
		$key = 'um_button_' . $i;
32
		if (!isset($modSettings[$key]))
33
			break;
34
		$row = json_decode($modSettings[$key], true);
35
		$temp_menu = array(
36
			'title' => $row['name'],
37
			'href' => ($row['type'] == 'forum' ? $scripturl . '?' : '') . $row['link'],
38
			'target' => $row['target'],
39
			'show' => (allowedTo('admin_forum') || count(array_intersect($user_info['groups'], explode(',', $row['permissions']))) >= 1) && $row['status'] == 'active',
40
		);
41
42
		foreach ($menu_buttons as $area => &$info)
43
		{
44
			if ($area == $row['parent'])
45
			{
46
				if ($row['position'] == 'before' || $row['position'] == 'after')
47
				{
48
					if (array_key_exists($row['parent'], $menu_buttons))
49
					{
50
						insert_button(array($key => $temp_menu), $menu_buttons, $row['parent'], $row['position']);
51
						break;
52
					}
53
				}
54
				elseif ($row['position'] == 'child_of')
55
				{
56
					$info['sub_buttons'][$key] = $temp_menu;
57
					break;
58
				}
59
			}
60
			elseif (isset($info['sub_buttons'][$row['parent']]))
61
			{
62
				if ($row['position'] == 'before' || $row['position'] == 'after')
63
				{
64
					insert_button(array($key => $temp_menu), $info['sub_buttons'], $row['parent'], $row['position']);
65
					break;
66
				}
67
				elseif ($row['position'] == 'child_of')
68
				{
69
					$info['sub_buttons'][$row['parent']]['sub_buttons'][$key] = $temp_menu;
70
					break;
71
				}
72
			}
73
		}
74
	}
75
}
76
77
function insert_button($needle, &$haystack, $insertion_point, $where = 'after')
78
{
79
	if (array_key_exists($insertion_point, $haystack))
80
	{
81
		$offset = 0;
82
83
		foreach ($haystack as $area => $dummy)
84
			if (++$offset && $area == $insertion_point)
85
				break;
86
87
		if ($where == 'before')
88
			$offset--;
89
90
		$haystack = array_slice($haystack, 0, $offset, true) + $needle + array_slice($haystack, $offset, null, true);
91
	}
92
	else
93
		foreach ($haystack as $stack)
94
			if (array_key_exists($insertion_point, $haystack[$stack]))
95
			{
96
				$offset = 0;
97
98
				foreach ($haystack[$stack] as $area => $dummy)
99
					if (++$offset && $area == $insertion_point)
100
						break;
101
102
				if ($where == 'before')
103
					$offset--;
104
105
				$haystack[$stack] = array_slice($haystack[$stack], 0, $offset, true) + $needle + array_slice($haystack[$stack], $offset, null, true);
106
				break;
107
			}
108
}
109
110
function um_admin_areas(&$admin_areas)
111
{
112
	global $txt;
113
114
	loadLanguage('ManageUltimateMenu');
115
	$admin_areas['config']['areas']['umen'] = array(
116
		'label' => $txt['um_admin_menu'],
117
		'file' => 'ManageUltimateMenu.php',
118
		'function' => function () {
119
			new ManageUltimateMenu;
120
		},
121
		'icon' => 'umen.png',
122
		'subsections' => array(
123
			'manmenu' => array($txt['um_admin_manage_menu'], ''),
124
			'addbutton' => array($txt['um_admin_add_button'], ''),
125
		),
126
	);
127
}
128
129
?>
130