p_master::assign_tpl_vars()   F
last analyzed

Complexity

Conditions 32
Paths 15382

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 148
Code Lines 64

Importance

Changes 0
Metric Value
dl 0
loc 148
rs 2
c 0
b 0
f 0
cc 32
eloc 64
nc 15382
nop 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
* This file is part of the VinaBB.vn package.
4
*
5
* @copyright (c) VinaBB <vinabb.vn>
6
* @license GNU General Public License, version 2 (GPL-2.0)
7
*/
8
9
namespace vinabb\web\includes;
10
11
class p_master extends \p_master
12
{
13
	/**
14
	* Build navigation structure
15
	*
16
	* Copied from phpBB 3.2.0-RC2 with 2 changes:
17
	*	$u_title = append_sid($u_title);
18
	*
19
	* 2 times:
20
	*	'MODULE'	=> $item_ary['name'],
21
	*	'MODE'		=> $item_ary['mode'],
22
	*
23
	* REMEMBER TO UPDATE CODE CHANGES FOR LATER PHPBB VERSIONS IF NEEDED
24
	*
25
	* @param $module_url
26
	*/
27
	public function assign_tpl_vars($module_url)
28
	{
29
		global $template;
30
31
		$current_id = $right_id = false;
32
33
		// Make sure the module_url has a question mark set, effectively determining the delimiter to use
34
		$delim = (strpos($module_url, '?') === false) ? '?' : '&amp;';
35
36
		$current_depth = 0;
37
		$linear_offset 	= 'l_block1';
38
		$tabular_offset = 't_block2';
39
40
		// Generate the list of modules, we'll do this in two ways ...
41
		// 1) In a linear fashion
42
		// 2) In a combined tabbed + linear fashion ... tabs for the categories
43
		//    and a linear list for subcategories/items
44
		foreach ($this->module_ary as $row_id => $item_ary)
45
		{
46
			// Skip hidden modules
47
			if (!$item_ary['display'])
48
			{
49
				continue;
50
			}
51
52
			// Skip branch
53
			if ($right_id !== false)
54
			{
55
				if ($item_ary['left'] < $right_id)
56
				{
57
					continue;
58
				}
59
60
				$right_id = false;
61
			}
62
63
			// Category with no members on their way down (we have to check every level)
64
			if (!$item_ary['name'])
65
			{
66
				$empty_category = true;
67
68
				// We go through the branch and look for an activated module
69
				foreach (array_slice($this->module_ary, $row_id + 1) as $temp_row)
70
				{
71
					if ($temp_row['left'] > $item_ary['left'] && $temp_row['left'] < $item_ary['right'])
72
					{
73
						// Module there and displayed?
74
						if ($temp_row['name'] && $temp_row['display'])
75
						{
76
							$empty_category = false;
77
							break;
78
						}
79
80
						continue;
81
					}
82
83
					break;
84
				}
85
86
				// Skip the branch
87
				if ($empty_category)
88
				{
89
					$right_id = $item_ary['right'];
90
					continue;
91
				}
92
			}
93
94
			// Select first id we can get
95
			if (!$current_id && (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id))
96
			{
97
				$current_id = $item_ary['id'];
98
			}
99
100
			$depth = $item_ary['depth'];
101
102
			if ($depth > $current_depth)
103
			{
104
				$linear_offset = $linear_offset . '.l_block' . ($depth + 1);
105
				$tabular_offset = ($depth + 1 > 2) ? $tabular_offset . '.t_block' . ($depth + 1) : $tabular_offset;
106
			}
107
			else if ($depth < $current_depth)
108
			{
109
				for ($i = $current_depth - $depth; $i > 0; $i--)
110
				{
111
					$linear_offset = substr($linear_offset, 0, strrpos($linear_offset, '.'));
112
					$tabular_offset = ($i + $depth > 1) ? substr($tabular_offset, 0, strrpos($tabular_offset, '.')) : $tabular_offset;
113
				}
114
			}
115
116
			$u_title = $module_url . $delim . 'i=';
117
118
			// If the item has a name use it, else use its id
119
			if (empty($item_ary['name']))
120
			{
121
				$u_title .= $item_ary['id'];
122
			}
123
			else
124
			{
125
				// If the category has a name, then use it.
126
				$u_title .= $this->get_module_identifier($item_ary['name']);
127
			}
128
129
			// If the item is not a category append the mode
130
			if (!$item_ary['cat'])
131
			{
132
				if ($item_ary['is_duplicate'])
133
				{
134
					$u_title .= '&amp;icat=' . $current_id;
135
				}
136
137
				$u_title .= '&amp;mode=' . $item_ary['mode'];
138
			}
139
140
			// Was not allowed in categories before - /*!$item_ary['cat'] && */
141
			$u_title .= (isset($item_ary['url_extra'])) ? $item_ary['url_extra'] : '';
142
143
			// Use append_sid() here to modify URL via the event core.append_sid
144
			$u_title = append_sid($u_title);
145
146
			// Only output a categories items if it's currently selected
147
			if (!$depth || ($depth && (in_array($item_ary['parent'], array_values($this->module_cache['parents'])) || $item_ary['parent'] == $this->p_parent)))
148
			{
149
				$use_tabular_offset = (!$depth) ? 't_block1' : $tabular_offset;
150
151
				$tpl_ary = [
152
					'MODULE'		=> $item_ary['name'],
153
					'MODE'			=> $item_ary['mode'],
154
					'L_TITLE'		=> $item_ary['lang'],
155
					'S_SELECTED'	=> (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id),
156
					'U_TITLE'		=> $u_title
157
				];
158
159
				$template->assign_block_vars($use_tabular_offset, array_merge($tpl_ary, array_change_key_case($item_ary, CASE_UPPER)));
160
			}
161
162
			$tpl_ary = [
163
				'MODULE'		=> $item_ary['name'],
164
				'MODE'			=> $item_ary['mode'],
165
				'L_TITLE'		=> $item_ary['lang'],
166
				'S_SELECTED'	=> (isset($this->module_cache['parents'][$item_ary['id']]) || $item_ary['id'] == $this->p_id),
167
				'U_TITLE'		=> $u_title
168
			];
169
170
			$template->assign_block_vars($linear_offset, array_merge($tpl_ary, array_change_key_case($item_ary, CASE_UPPER)));
171
172
			$current_depth = $depth;
173
		}
174
	}
175
}
176