Passed
Push — master ( 1778f3...9e4eed )
by John
01:26
created

um_load_menu()   F

Complexity

Conditions 18
Paths 224

Size

Total Lines 57
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 18
eloc 32
nc 224
nop 1
dl 0
loc 57
rs 3.7333
c 1
b 0
f 0

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');
0 ignored issues
show
Bug introduced by
The function remove_integration_function was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
		/** @scrutinizer ignore-call */ 
22
  remove_integration_function('integrate_menu_buttons', 'um_load_menu');
Loading history...
22
		add_integration_function('integrate_menu_buttons', 'um_load_menu');
0 ignored issues
show
Bug introduced by
The function add_integration_function was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

22
		/** @scrutinizer ignore-call */ 
23
  add_integration_function('integrate_menu_buttons', 'um_load_menu');
Loading history...
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',
0 ignored issues
show
Bug introduced by
The function allowedTo was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

39
			'show' => (/** @scrutinizer ignore-call */ allowedTo('admin_forum') || count(array_intersect($user_info['groups'], explode(',', $row['permissions']))) >= 1) && $row['status'] == 'active',
Loading history...
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
/**
78
 * Gets all membergroups and filters them according to the parameters.
79
 *
80
 * @param string $checked comma-seperated list of all id_groups to be checked (have a mark in the checkbox). Default is an empty array.
81
 * @param string $disallowed comma-seperated list of all id_groups that are skipped. Default is an empty array.
82
 * @param bool $inherited whether or not to filter out the inherited groups. Default is false.
83
 * @return array all the membergroups filtered according to the parameters; empty array if something went wrong.
84
 * @since 1.0
85
 */
86
function list_groups($checked, $disallowed = '', $inherited = false, $permission = null, $board_id = null)
87
{
88
	global $context, $modSettings, $smcFunc, $sourcedir, $txt;
89
90
	// We'll need this for loading up the names of each group.
91
	if (!loadLanguage('ManageBoards'))
0 ignored issues
show
Bug introduced by
The function loadLanguage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

91
	if (!/** @scrutinizer ignore-call */ loadLanguage('ManageBoards'))
Loading history...
92
		loadLanguage('ManageBoards');
93
94
	$checked = explode(',', $checked);
95
	$disallowed = explode(',', $disallowed);
96
97
	// Are we also looking up permissions?
98
	if ($permission !== null)
99
	{
100
		require_once($sourcedir . '/Subs-Members.php');
101
		$member_groups = groupsAllowedTo($permission, $board_id);
0 ignored issues
show
Bug introduced by
The function groupsAllowedTo was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
		$member_groups = /** @scrutinizer ignore-call */ groupsAllowedTo($permission, $board_id);
Loading history...
102
		$disallowed = array_diff(array_keys(list_groups(-3)), $member_groups['allowed']);
103
	}
104
105
	$groups = array();
106
107
	if (!in_array(-1, $disallowed))
108
		// Guests
109
		$groups[-1] = array(
110
			'id' => -1,
111
			'name' => $txt['parent_guests_only'],
112
			'checked' => in_array(-1, $checked) || in_array(-3, $checked),
113
			'is_post_group' => false,
114
		);
115
116
	if (!in_array(0, $disallowed))
117
		// Regular Members
118
		$groups[0] = array(
119
			'id' => 0,
120
			'name' => $txt['parent_members_only'],
121
			'checked' => in_array(0, $checked) || in_array(-3, $checked),
122
			'is_post_group' => false,
123
		);
124
125
	// Load membergroups.
126
	$request = $smcFunc['db_query']('', '
127
		SELECT group_name, id_group, min_posts
128
		FROM {db_prefix}membergroups
129
		WHERE id_group > {int:is_zero}' . (!$inherited ? '
130
			AND id_parent = {int:not_inherited}' : '') . (!$inherited && empty($modSettings['permission_enable_postgroups']) ? '
131
			AND min_posts = {int:min_posts}' : ''),
132
		array(
133
			'is_zero' => 0,
134
			'not_inherited' => -2,
135
			'min_posts' => -1,
136
		)
137
	);
138
	while ($row = $smcFunc['db_fetch_assoc']($request))
139
		if (!in_array($row['id_group'], $disallowed))
140
			$groups[(int) $row['id_group']] = array(
141
				'id' => $row['id_group'],
142
				'name' => trim($row['group_name']),
143
				'checked' => in_array($row['id_group'], $checked) || in_array(-3, $checked),
144
				'is_post_group' => $row['min_posts'] != -1,
145
			);
146
	$smcFunc['db_free_result']($request);
147
148
	asort($groups);
149
150
	return $groups;
151
}
152
153
function insert_button($needle, &$haystack, $insertion_point, $where = 'after')
154
{
155
	if (array_key_exists($insertion_point, $haystack))
156
	{
157
		$offset = 0;
158
159
		foreach ($haystack as $area => $dummy)
160
			if (++$offset && $area == $insertion_point)
161
				break;
162
163
		if ($where == 'before')
164
			$offset--;
165
166
		$haystack = array_slice($haystack, 0, $offset, true) + $needle + array_slice($haystack, $offset, null, true);
167
	}
168
	else
169
		foreach ($haystack as $stack)
170
			if (array_key_exists($insertion_point, $haystack[$stack]))
171
			{
172
				$offset = 0;
173
174
				foreach ($haystack[$stack] as $area => $dummy)
175
					if (++$offset && $area == $insertion_point)
176
						break;
177
178
				if ($where == 'before')
179
					$offset--;
180
181
				$haystack[$stack] = array_slice($haystack[$stack], 0, $offset, true) + $needle + array_slice($haystack[$stack], $offset, null, true);
182
				break;
183
			}
184
}
185
186
function um_admin_areas(&$admin_areas)
187
{
188
	global $txt;
189
190
	loadLanguage('ManageUltimateMenu');
0 ignored issues
show
Bug introduced by
The function loadLanguage was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

190
	/** @scrutinizer ignore-call */ 
191
 loadLanguage('ManageUltimateMenu');
Loading history...
191
	$admin_areas['config']['areas']['umen'] = array(
192
		'label' => $txt['um_admin_menu'],
193
		'file' => 'ManageUltimateMenu.php',
194
		'function' => function () {
195
			new ManageUltimateMenu;
196
		},
197
		'icon' => 'umen.png',
198
		'subsections' => array(
199
			'manmenu' => array($txt['um_admin_manage_menu'], ''),
200
			'addbutton' => array($txt['um_admin_add_button'], ''),
201
		),
202
	);
203
}
204
205
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
206