Conditions | 67 |
Paths | > 20000 |
Total Lines | 397 |
Code Lines | 235 |
Lines | 12 |
Ratio | 3.02 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
23 | public function main($id, $mode) |
||
24 | { |
||
25 | /** @var \phpbb\request\request $request */ |
||
26 | /** @var \phpbb\log\log $phpbb_log */ |
||
27 | global $phpbb_container, $db, $user, $phpbb_log, $template, $cache, $request, $table_prefix, $phpbb_root_path; |
||
28 | $this->qte = $phpbb_container->get('ernadoo.qte'); |
||
29 | |||
30 | $ext_root_path = $phpbb_root_path . 'ext/ernadoo/qte/'; |
||
31 | |||
32 | $action = $request->variable('action', ''); |
||
33 | $submit = $request->is_set_post('submit'); |
||
34 | $attr_id = $request->variable('id', 0); |
||
35 | $attr_auth_id = $request->variable('attr_auth_id', 0); |
||
36 | |||
37 | $error = array(); |
||
38 | |||
39 | $this->tpl_name = 'acp_attributes'; |
||
|
|||
40 | $this->page_title = 'QTE_MANAGE_TITLE'; |
||
41 | |||
42 | $user->add_lang_ext('ernadoo/qte', array('attributes', 'attributes_acp')); |
||
43 | |||
44 | // Display a warning when a development version is installed or if the database is outdated |
||
45 | $this->display_version_warning(); |
||
46 | |||
47 | add_form_key('acp_attributes'); |
||
48 | |||
49 | switch ($action) |
||
50 | { |
||
51 | case 'edit': |
||
52 | case 'add': |
||
53 | |||
54 | $attr_type = $request->variable('attr_type', 0); |
||
55 | $attr_name = $request->variable('attr_name', '', true); |
||
56 | $attr_img = $request->variable('attr_img', ''); |
||
57 | $attr_desc = $request->variable('attr_desc', '', true); |
||
58 | $attr_date = $request->variable('attr_date', ''); |
||
59 | $attr_colour = $request->variable('attr_colour', ''); |
||
60 | $attr_user_colour = $request->variable('attr_user_colour', 0); |
||
61 | |||
62 | // is it too complex for u ? pastisd has no limit :) |
||
63 | $attr_auths = array(array('forums_ids' => array(), 'groups_ids' => array(), 'author' => false)); |
||
64 | if ($request->is_set_post('attr_auths')) |
||
65 | { |
||
66 | $attr_auths = $request->variable('attr_auths', array(array('' => array(0)))); |
||
67 | foreach ($attr_auths as &$attr_auth) |
||
68 | { |
||
69 | $attr_auth = array( |
||
70 | 'forums_ids' => isset($attr_auth['forums_ids']) ? $attr_auth['forums_ids'] : array(), |
||
71 | 'groups_ids' => isset($attr_auth['groups_ids']) ? $attr_auth['groups_ids'] : array(), |
||
72 | 'author' => isset($attr_auth['author']) ? true : false, |
||
73 | ); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | if ($submit) |
||
78 | { |
||
79 | if (!check_form_key('acp_attributes')) |
||
80 | { |
||
81 | $error[] = $user->lang['FORM_INVALID']; |
||
82 | } |
||
83 | |||
84 | if (empty($attr_name)) |
||
85 | { |
||
86 | $error[] = $user->lang['QTE_NAME_ERROR']; |
||
87 | } |
||
88 | |||
89 | if (isset($attr_desc[60])) |
||
90 | { |
||
91 | $error[] = $user->lang['QTE_DESC_ERROR']; |
||
92 | } |
||
93 | |||
94 | // fully xhtml compatibility : no capital letters |
||
95 | if (!empty($attr_colour)) |
||
96 | { |
||
97 | $attr_colour = strtolower($attr_colour); |
||
98 | if (!preg_match('#^([a-f0-9]){6}#i', $attr_colour)) |
||
99 | { |
||
100 | $error[] = $user->lang['QTE_COLOUR_ERROR']; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | // we don't need user colour when an image is used as attribute |
||
105 | if ($attr_type && $attr_user_colour) |
||
106 | { |
||
107 | $attr_user_colour = false; |
||
108 | } |
||
109 | |||
110 | $attr_name_tmp = $this->qte->attr_lng_key($attr_name); |
||
111 | if ($attr_user_colour) |
||
112 | { |
||
113 | if (strpos($attr_name_tmp, '%mod%') === false) |
||
114 | { |
||
115 | $error[] = $user->lang['QTE_USER_COLOUR_ERROR']; |
||
116 | } |
||
117 | } |
||
118 | |||
119 | if (!empty($attr_date)) |
||
120 | { |
||
121 | if (strpos($attr_name_tmp, '%date%') === false) |
||
122 | { |
||
123 | $error[] = $user->lang['QTE_DATE_ARGUMENT_ERROR']; |
||
124 | } |
||
125 | } |
||
126 | else |
||
127 | { |
||
128 | if (strpos($attr_name_tmp, '%date%') !== false) |
||
129 | { |
||
130 | $error[] = $user->lang['QTE_DATE_FORMAT_ERROR']; |
||
131 | } |
||
132 | } |
||
133 | unset($attr_name_tmp); |
||
134 | |||
135 | if (!sizeof($error)) |
||
136 | { |
||
137 | $sql_ary = array( |
||
138 | 'attr_type' => $attr_type, |
||
139 | 'attr_name' => $attr_name, |
||
140 | 'attr_img' => $attr_img, |
||
141 | 'attr_desc' => $attr_desc, |
||
142 | 'attr_date' => $attr_date, |
||
143 | 'attr_colour' => $attr_colour, |
||
144 | 'attr_user_colour' => $attr_user_colour, |
||
145 | 'attr_auths' => sizeof($attr_auths) ? json_encode($attr_auths) : '', |
||
146 | ); |
||
147 | |||
148 | if ($attr_id) |
||
149 | { |
||
150 | $sql = 'UPDATE ' . $table_prefix . 'topics_attr |
||
151 | SET ' . $db->sql_build_array('UPDATE', $sql_ary) . ' |
||
152 | WHERE attr_id = ' . (int) $attr_id; |
||
153 | $db->sql_query($sql); |
||
154 | |||
155 | $message = 'UPDATED'; |
||
156 | } |
||
157 | else |
||
158 | { |
||
159 | $sql = 'SELECT MAX(right_id) AS right_id |
||
160 | FROM ' . $table_prefix . 'topics_attr'; |
||
161 | $result = $db->sql_query($sql); |
||
162 | $right_id = (int) $db->sql_fetchfield('right_id'); |
||
163 | $db->sql_freeresult($result); |
||
164 | |||
165 | $sql_ary['left_id'] = ($right_id + 1); |
||
166 | $sql_ary['right_id'] = ($right_id + 2); |
||
167 | |||
168 | $sql = 'INSERT INTO ' . $table_prefix . 'topics_attr ' . $db->sql_build_array('INSERT', $sql_ary); |
||
169 | $db->sql_query($sql); |
||
170 | |||
171 | $message = 'ADDED'; |
||
172 | } |
||
173 | |||
174 | $cache->destroy('_attr'); |
||
175 | |||
176 | $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTRIBUTE_' . $message, time(), array($attr_name)); |
||
177 | |||
178 | trigger_error($user->lang['QTE_' . $message] . adm_back_link($this->u_action)); |
||
179 | } |
||
180 | } |
||
181 | else if ($attr_id) |
||
182 | { |
||
183 | $set_permissions = $this->set_auths($attr_id); |
||
184 | $attr = $set_permissions['attr']; |
||
185 | $attr_auths = $set_permissions['attr_auths']; |
||
186 | } |
||
187 | |||
188 | if ($action == 'edit') |
||
189 | { |
||
190 | $template->assign_vars(array( |
||
191 | 'L_QTE_ADD_EDIT' => $user->lang['QTE_EDIT'], |
||
192 | 'L_QTE_ADD_EDIT_EXPLAIN' => $user->lang['QTE_EDIT_EXPLAIN'], |
||
193 | )); |
||
194 | } |
||
195 | else |
||
196 | { |
||
197 | $template->assign_vars(array( |
||
198 | 'L_QTE_ADD_EDIT' => $user->lang['QTE_ADD'], |
||
199 | 'L_QTE_ADD_EDIT_EXPLAIN' => $user->lang['QTE_ADD_EXPLAIN'], |
||
200 | )); |
||
201 | |||
202 | $attr_auths = array(array( |
||
203 | 'forums_ids' => array(), |
||
204 | 'groups_ids' => array(), |
||
205 | 'author' => false, |
||
206 | )); |
||
207 | } |
||
208 | |||
209 | $this->qte_attr_select($attr_id); |
||
210 | $this->add_auths($attr_auths); |
||
211 | |||
212 | if (sizeof($error)) |
||
213 | { |
||
214 | $template->assign_vars(array( |
||
215 | 'S_ERROR' => true, |
||
216 | 'ERROR_MSG' => implode('<br />', $error), |
||
217 | )); |
||
218 | } |
||
219 | |||
220 | $attr_type_state = ((isset($attr['attr_type']) && $attr['attr_type']) || (isset($attr_type) && $attr_type)); |
||
221 | $attr_user_colour_state = ((isset($attr['attr_user_colour']) && $attr['attr_user_colour']) || (isset($attr_user_colour) && $attr_user_colour)); |
||
222 | |||
223 | $template->assign_vars(array( |
||
224 | 'S_EDIT' => true, |
||
225 | |||
226 | 'U_ACTION' => $this->u_action . '&action=' . (($action == 'add') ? 'add' : 'edit&id=' . (int) $attr_id), |
||
227 | 'U_BACK' => $this->u_action, |
||
228 | 'U_AJAX' => str_replace('&', '&', $this->u_action), |
||
229 | |||
230 | 'L_QTE_NAME_EXPLAIN' => $user->lang('QTE_NAME_EXPLAIN', $user->data['username']), |
||
231 | |||
232 | 'ATTR_ID' => isset($attr['attr_id']) ? $attr['attr_id'] : $attr_id, |
||
233 | 'ATTR_NAME' => isset($attr['attr_name']) ? $attr['attr_name'] : $attr_name, |
||
234 | 'ATTR_IMG' => isset($attr['attr_img']) ? $attr['attr_img'] : $attr_img, |
||
235 | 'ATTR_DESC' => isset($attr['attr_desc']) ? $attr['attr_desc'] : $attr_desc, |
||
236 | 'ATTR_DATE' => isset($attr['attr_date']) ? $attr['attr_date'] : $attr_date, |
||
237 | 'ATTR_COLOUR' => isset($attr['attr_colour']) ? $attr['attr_colour'] : $attr_colour, |
||
238 | |||
239 | 'S_TEXT' => $attr_type_state ? true : false, |
||
240 | 'S_USER_COLOUR' => $attr_user_colour_state ? true : false, |
||
241 | |||
242 | 'ICON_ATTR_AUTH_ADD' => '<img src="' . $ext_root_path . 'adm/images/qte_auth_add.gif" alt="' . $user->lang['QTE_AUTH_ADD'] . '" title="' . $user->lang['QTE_AUTH_ADD'] . '" />', |
||
243 | 'ICON_ATTR_AUTH_REMOVE' => '<img src="' . $ext_root_path . 'adm/images/qte_auth_remove.gif" alt="' . $user->lang['QTE_AUTH_REMOVE'] . '" title="' . $user->lang['QTE_AUTH_REMOVE'] . '" />', |
||
244 | )); |
||
245 | |||
246 | return; |
||
247 | |||
248 | break; |
||
249 | |||
250 | case 'set_permissions': |
||
251 | |||
252 | $set_permissions = $this->set_auths($attr_auth_id); |
||
253 | $this->add_auths($set_permissions['attr_auths']); |
||
254 | |||
255 | $template->assign_vars(array( |
||
256 | 'ICON_ATTR_AUTH_ADD' => '<img src="' . $ext_root_path . 'adm/images/qte_auth_add.gif" alt="' . $user->lang['QTE_AUTH_ADD'] . '" title="' . $user->lang['QTE_AUTH_ADD'] . '" />', |
||
257 | 'ICON_ATTR_AUTH_REMOVE' => '<img src="' . $ext_root_path . 'adm/images/qte_auth_remove.gif" alt="' . $user->lang['QTE_AUTH_REMOVE'] . '" title="' . $user->lang['QTE_AUTH_REMOVE'] . '" />', |
||
258 | )); |
||
259 | $this->tpl_name = 'acp_attributes_auths'; |
||
260 | |||
261 | break; |
||
262 | |||
263 | case 'delete': |
||
264 | |||
265 | View Code Duplication | if (!$attr_id) |
|
1 ignored issue
–
show
|
|||
266 | { |
||
267 | trigger_error($user->lang['QTE_MUST_SELECT'] . adm_back_link($this->u_action), E_USER_WARNING); |
||
268 | } |
||
269 | |||
270 | if (confirm_box(true)) |
||
271 | { |
||
272 | $sql = 'SELECT topic_id, topic_attr_id |
||
273 | FROM ' . TOPICS_TABLE . ' |
||
274 | WHERE topic_attr_id = ' . (int) $attr_id; |
||
275 | $result = $db->sql_query($sql); |
||
276 | |||
277 | $topic_id_ary = array(); |
||
278 | while ($row = $db->sql_fetchrow($result)) |
||
279 | { |
||
280 | $topic_id_ary[] = (int) $row['topic_id']; |
||
281 | } |
||
282 | $db->sql_freeresult($result); |
||
283 | |||
284 | if (sizeof($topic_id_ary)) |
||
285 | { |
||
286 | $fields = array('topic_attr_id' => 0, 'topic_attr_user' => 0, 'topic_attr_time' => 0); |
||
287 | |||
288 | $sql = 'UPDATE ' . TOPICS_TABLE . ' |
||
289 | SET ' . $db->sql_build_array('UPDATE', $fields) . ' |
||
290 | WHERE ' . $db->sql_in_set('topic_id', array_map('intval', $topic_id_ary)); |
||
291 | $db->sql_query($sql); |
||
292 | } |
||
293 | |||
294 | $sql = 'SELECT attr_name |
||
295 | FROM ' . $table_prefix . 'topics_attr |
||
296 | WHERE attr_id = ' . (int) $attr_id; |
||
297 | $result = $db->sql_query($sql); |
||
298 | $attr_name = (string) $db->sql_fetchfield('attr_name'); |
||
299 | $db->sql_freeresult($result); |
||
300 | |||
301 | $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTRIBUTE_REMOVED', time(), array($attr_name)); |
||
302 | |||
303 | $sql = 'DELETE FROM ' . $table_prefix . 'topics_attr |
||
304 | WHERE attr_id = ' . (int) $attr_id; |
||
305 | $db->sql_query($sql); |
||
306 | |||
307 | $cache->destroy('_attr'); |
||
308 | |||
309 | if ($request->is_ajax()) |
||
310 | { |
||
311 | $json_response = new \phpbb\json_response; |
||
312 | $json_response->send(array( |
||
313 | 'success' => 'true', |
||
314 | 'MESSAGE_TITLE' => $user->lang['INFORMATION'], |
||
315 | 'MESSAGE_TEXT' => $user->lang['QTE_REMOVED'], |
||
316 | 'REFRESH_DATA' => array( |
||
317 | 'time' => 3, |
||
318 | ) |
||
319 | )); |
||
320 | } |
||
321 | else |
||
322 | { |
||
323 | trigger_error($user->lang['QTE_REMOVED'] . adm_back_link($this->u_action)); |
||
324 | } |
||
325 | } |
||
326 | else |
||
327 | { |
||
328 | confirm_box(false, $user->lang['CONFIRM_OPERATION'], build_hidden_fields(array( |
||
329 | 'i' => $id, |
||
330 | 'mode' => $mode, |
||
331 | 'attr_id' => $attr_id, |
||
332 | 'action' => 'delete', |
||
333 | ))); |
||
334 | } |
||
335 | |||
336 | break; |
||
337 | |||
338 | case 'move_up': |
||
339 | case 'move_down': |
||
340 | |||
341 | View Code Duplication | if (!$attr_id) |
|
1 ignored issue
–
show
|
|||
342 | { |
||
343 | trigger_error($user->lang['QTE_MUST_SELECT'] . adm_back_link($this->u_action), E_USER_WARNING); |
||
344 | } |
||
345 | |||
346 | $sql = 'SELECT * |
||
347 | FROM ' . $table_prefix . 'topics_attr |
||
348 | WHERE attr_id = ' . (int) $attr_id; |
||
349 | $result = $db->sql_query($sql); |
||
350 | $row = $db->sql_fetchrow($result); |
||
351 | $db->sql_freeresult($result); |
||
352 | |||
353 | View Code Duplication | if (!$row) |
|
1 ignored issue
–
show
|
|||
354 | { |
||
355 | trigger_error($user->lang['QTE_MUST_SELECT'] . adm_back_link($this->u_action), E_USER_WARNING); |
||
356 | } |
||
357 | |||
358 | $move_attr_name = $this->qte_move($row, $action, 1); |
||
359 | if ($move_attr_name !== false) |
||
360 | { |
||
361 | $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_ATTRIBUTE_' . strtoupper($action), time(), array($move_attr_name)); |
||
362 | } |
||
363 | |||
364 | if ($request->is_ajax()) |
||
365 | { |
||
366 | $json_response = new \phpbb\json_response; |
||
367 | $json_response->send(array('success' => true)); |
||
368 | } |
||
369 | |||
370 | break; |
||
371 | } |
||
372 | |||
373 | $template->assign_vars(array('U_ACTION' => $this->u_action)); |
||
374 | |||
375 | $sql = 'SELECT topic_attr_id, COUNT(topic_id) AS total_topics |
||
376 | FROM ' . TOPICS_TABLE . ' |
||
377 | GROUP BY topic_attr_id'; |
||
378 | $result = $db->sql_query($sql); |
||
379 | $stats = array(); |
||
380 | $total_topics = 0; |
||
381 | while ($row = $db->sql_fetchrow($result)) |
||
382 | { |
||
383 | $stats[$row['topic_attr_id']] = $row['total_topics']; |
||
384 | $total_topics += $row['total_topics']; |
||
385 | } |
||
386 | $db->sql_freeresult($result); |
||
387 | |||
388 | $sql = 'SELECT * FROM ' . $table_prefix . 'topics_attr ORDER BY left_id'; |
||
389 | $result = $db->sql_query($sql); |
||
390 | |||
391 | while ($row = $db->sql_fetchrow($result)) |
||
392 | { |
||
393 | $attribute_name = str_replace(array('%mod%', '%date%'), array($user->lang['QTE_KEY_USERNAME'], $user->lang['QTE_KEY_DATE']), $this->qte->attr_lng_key($row['attr_name'])); |
||
394 | $attribute_count = isset($stats[$row['attr_id']]) ? $stats[$row['attr_id']] : 0; |
||
395 | |||
396 | $template->assign_block_vars('row', array( |
||
397 | 'S_IMAGE' => $row['attr_type'] ? true : false, |
||
398 | 'S_COLOUR' => $row['attr_colour'] ? true : false, |
||
399 | 'S_DESC' => $row['attr_desc'] ? true : false, |
||
400 | 'S_DATE' => $row['attr_date'] ? true : false, |
||
401 | 'S_USER_COLOUR' => $row['attr_user_colour'] ? true : false, |
||
402 | 'S_CSS' => (!$row['attr_type'] && isset($user->lang[$row['attr_name']]) && empty($row['attr_colour'])) ? true : false, |
||
403 | |||
404 | 'QTE_TXT' => $attribute_name, |
||
405 | 'QTE_DESC' => $this->qte->attr_lng_key($row['attr_desc']), |
||
406 | 'QTE_IMG' => $this->qte->attr_img_key($row['attr_img'], $attribute_name), |
||
407 | 'QTE_COLOUR' => $row['attr_colour'], |
||
408 | 'QTE_DATE' => $row['attr_date'], |
||
409 | 'QTE_COUNT' => (int) $attribute_count, |
||
410 | 'QTE_PER_CENT' => empty($total_topics) ? 0 : round(intval($attribute_count) * 100 / $total_topics), |
||
411 | |||
412 | 'U_EDIT' => $this->u_action . '&action=edit&id=' . $row['attr_id'], |
||
413 | 'U_MOVE_UP' => $this->u_action . '&action=move_up&id=' . $row['attr_id'], |
||
414 | 'U_MOVE_DOWN' => $this->u_action . '&action=move_down&id=' . $row['attr_id'], |
||
415 | 'U_DELETE' => $this->u_action . '&action=delete&id=' . $row['attr_id'], |
||
416 | )); |
||
417 | } |
||
418 | $db->sql_freeresult($result); |
||
419 | } |
||
420 | |||
652 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: