Conditions | 22 |
Paths | 2688 |
Total Lines | 164 |
Code Lines | 90 |
Lines | 30 |
Ratio | 18.29 % |
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 |
||
267 | public function apply_block() |
||
268 | { |
||
269 | global $xoopsDB; |
||
270 | $xoopsDB->queryF('UPDATE ' . $xoopsDB->prefix('block_module_link') . ' SET module_id = -1, pageid = 0 WHERE module_id < 2 AND pageid = 1'); |
||
271 | |||
272 | //Change block module link to remove pages |
||
273 | //Remove page links for module subpages |
||
274 | $xoopsDB->queryF('DELETE FROM ' . $xoopsDB->prefix('block_module_link') . ' WHERE pageid > 0'); |
||
275 | |||
276 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('block_module_link') . '` DROP PRIMARY KEY'; |
||
277 | $xoopsDB->queryF($sql); |
||
278 | $sql = 'ALTER TABLE `' . $xoopsDB->prefix('block_module_link') . '` DROP pageid'; |
||
279 | $xoopsDB->queryF($sql); |
||
280 | $sql = 'ALTER IGNORE TABLE `' . $xoopsDB->prefix('block_module_link') . '` ADD PRIMARY KEY (`block_id` , `module_id`)'; |
||
281 | $xoopsDB->queryF($sql); |
||
282 | |||
283 | $xoopsDB->queryF('RENAME TABLE `' . $xoopsDB->prefix('newblocks') . '` TO `' . $xoopsDB->prefix('newblocks_bak') . '`'); |
||
284 | |||
285 | // Create new block table |
||
286 | $sql = 'CREATE TABLE ' . $xoopsDB->prefix('newblocks') . " ( |
||
287 | bid mediumint(8) unsigned NOT NULL auto_increment, |
||
288 | mid smallint(5) unsigned NOT NULL default '0', |
||
289 | func_num tinyint(3) unsigned NOT NULL default '0', |
||
290 | options varchar(255) NOT NULL default '', |
||
291 | name varchar(150) NOT NULL default '', |
||
292 | title varchar(255) NOT NULL default '', |
||
293 | content text, |
||
294 | side tinyint(1) unsigned NOT NULL default '0', |
||
295 | weight smallint(5) unsigned NOT NULL default '0', |
||
296 | visible tinyint(1) unsigned NOT NULL default '0', |
||
297 | block_type char(1) NOT NULL default '', |
||
298 | c_type char(1) NOT NULL default '', |
||
299 | isactive tinyint(1) unsigned NOT NULL default '0', |
||
300 | dirname varchar(50) NOT NULL default '', |
||
301 | func_file varchar(50) NOT NULL default '', |
||
302 | show_func varchar(50) NOT NULL default '', |
||
303 | edit_func varchar(50) NOT NULL default '', |
||
304 | template varchar(50) NOT NULL default '', |
||
305 | bcachetime int(10) unsigned NOT NULL default '0', |
||
306 | last_modified int(10) unsigned NOT NULL default '0', |
||
307 | PRIMARY KEY (bid), |
||
308 | KEY mid (mid), |
||
309 | KEY visible (visible), |
||
310 | KEY isactive_visible_mid (isactive,visible,mid), |
||
311 | KEY mid_funcnum (mid,func_num) |
||
312 | ) TYPE=MyISAM; |
||
313 | "; |
||
314 | $xoopsDB->queryF($sql); |
||
315 | |||
316 | $sql = ' SELECT MAX(instanceid) FROM ' . $xoopsDB->prefix('block_instance'); |
||
317 | $result = $xoopsDB->query($sql); |
||
318 | list($MaxInstanceId) = $xoopsDB->fetchRow($result); |
||
319 | |||
320 | // Change custom block mid from 1 to 0 |
||
321 | $sql = 'UPDATE `' . $xoopsDB->prefix('newblocks_bak') . "` SET mid = 0 WHERE show_func = 'b_system_custom_show'"; |
||
322 | $result = $xoopsDB->queryF($sql); |
||
323 | |||
324 | $sql = ' SELECT b.*, i.instanceid ' . ' FROM ' . $xoopsDB->prefix('block_instance') . ' AS i LEFT JOIN ' . $xoopsDB->prefix('newblocks_bak') . ' AS b ON b.bid = i.bid ' . ' GROUP BY b.dirname, b.bid, i.instanceid'; |
||
325 | $result = $xoopsDB->query($sql); |
||
326 | $dirname = ''; |
||
327 | $bid = 0; |
||
328 | $block_key = null; |
||
329 | while ($row = $xoopsDB->fetchArray($result)) { |
||
330 | View Code Duplication | if ($row['dirname'] != $dirname) { |
|
331 | $dirname = $row['dirname']; |
||
332 | $modversion = array(); |
||
333 | if (!@include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/xoops_version.php') { |
||
334 | continue; |
||
335 | } |
||
336 | } |
||
337 | if (empty($modversion['blocks']) && $dirname !== 'system') { |
||
338 | continue; |
||
339 | } |
||
340 | |||
341 | $isClone = true; |
||
342 | View Code Duplication | if ($row['bid'] != $bid) { |
|
343 | $bid = $row['bid']; |
||
344 | $isClone = false; |
||
345 | $block_key = null; |
||
346 | $block_key = @$this->_block_lookup($row, $modversion['blocks']); |
||
347 | } |
||
348 | if ($block_key === null) { |
||
349 | continue; |
||
350 | } |
||
351 | |||
352 | // Copy data from block instance table and blocks table |
||
353 | $sql = ' INSERT INTO ' . $xoopsDB->prefix('newblocks') . ' (bid, mid, options, name, title, side, weight, visible, ' . ' func_num, ' . ' block_type, ' . ' c_type, ' . ' isactive, dirname, func_file,' . ' show_func, edit_func, template, bcachetime, last_modified)' . ' SELECT ' . ' i.instanceid, c.mid, i.options, c.name, i.title, i.side, i.weight, i.visible, ' . " {$block_key}, " . ($isClone ? " CASE WHEN c.show_func='b_system_custom_show' THEN 'C' ELSE 'D' END," : " CASE WHEN c.show_func='b_system_custom_show' THEN 'C' WHEN c.mid = 1 THEN 'S' ELSE 'M' END,") . " CASE WHEN c.c_type='' THEN 'H' ELSE c.c_type END," . ' c.isactive, c.dirname, c.func_file,' . ' c.show_func, c.edit_func, c.template, i.bcachetime, c.last_modified' . ' FROM ' . $xoopsDB->prefix('block_instance') . ' AS i,' . ' ' . $xoopsDB->prefix('newblocks_bak') . ' AS c' . ' WHERE i.bid = c.bid' . ' AND i.instanceid = ' . $row['instanceid']; |
||
354 | $xoopsDB->queryF($sql); |
||
355 | } |
||
356 | |||
357 | $sql = ' SELECT b.* ' . ' FROM ' . $xoopsDB->prefix('newblocks_bak') . ' AS b LEFT JOIN ' . $xoopsDB->prefix('block_instance') . ' AS i ON b.bid = i.bid ' . ' WHERE i.instanceid IS NULL'; |
||
358 | ' GROUP BY b.dirname, b.bid'; |
||
359 | $result = $xoopsDB->query($sql); |
||
360 | $dirname = ''; |
||
361 | $bid = 0; |
||
362 | $block_key = null; |
||
363 | while ($row = $xoopsDB->fetchArray($result)) { |
||
364 | View Code Duplication | if ($row['dirname'] != $dirname) { |
|
365 | $dirname = $row['dirname']; |
||
366 | $modversion = array(); |
||
367 | if (!@include XOOPS_ROOT_PATH . '/modules/' . $dirname . '/xoops_version.php') { |
||
368 | continue; |
||
369 | } |
||
370 | } |
||
371 | if (empty($modversion['blocks']) && $dirname !== 'system') { |
||
372 | continue; |
||
373 | } |
||
374 | |||
375 | View Code Duplication | if ($row['bid'] != $bid) { |
|
376 | $bid = $row['bid']; |
||
377 | $block_key = null; |
||
378 | $block_key = @$this->_block_lookup($row, $modversion['blocks']); |
||
379 | } |
||
380 | if ($block_key === null) { |
||
381 | continue; |
||
382 | } |
||
383 | |||
384 | // Copy data from blocks table |
||
385 | $sql = ' INSERT INTO ' . $xoopsDB->prefix('newblocks') . ' (bid, mid, options, name, title, side, weight, visible, ' . ' func_num, ' . ' block_type, ' . ' c_type, ' . ' isactive, dirname, func_file,' . ' show_func, edit_func, template, bcachetime, last_modified)' . ' SELECT ' . " bid + {$MaxInstanceId}, mid, options, name, name, 0, 0, 0, " . " {$block_key}, " . " CASE WHEN show_func='b_system_custom_show' THEN 'C' WHEN mid = 1 THEN 'S' ELSE 'M' END," . " CASE WHEN c_type='' THEN 'H' ELSE c_type END," . ' isactive, dirname, func_file,' . ' show_func, edit_func, template, 0, last_modified' . ' FROM ' . $xoopsDB->prefix('newblocks_bak') . ' WHERE bid = ' . $row['bid']; |
||
386 | $xoopsDB->queryF($sql); |
||
387 | |||
388 | // Build block-module link |
||
389 | $sql = ' INSERT INTO ' . $xoopsDB->prefix('block_module_link') . ' (block_id, module_id)' . ' SELECT ' . " bid + {$MaxInstanceId}, -1" . ' FROM ' . $xoopsDB->prefix('newblocks_bak') . ' WHERE bid = ' . $row['bid']; |
||
390 | $xoopsDB->queryF($sql); |
||
391 | } |
||
392 | |||
393 | // Dealing with tables |
||
394 | $xoopsDB->queryF('DROP TABLE `' . $xoopsDB->prefix('block_instance') . '`;'); |
||
395 | $xoopsDB->queryF('DROP TABLE `' . $xoopsDB->prefix('newblocks_bak') . '`;'); |
||
396 | |||
397 | // Deal with custom blocks, convert options to type and content |
||
398 | $sql = 'SELECT bid, options FROM `' . $xoopsDB->prefix('newblocks') . "` WHERE show_func='b_system_custom_show'"; |
||
399 | $result = $xoopsDB->query($sql); |
||
400 | while (list($bid, $options) = $xoopsDB->fetchRow($result)) { |
||
401 | $_options = unserialize($options); |
||
402 | $content = $_options[0]; |
||
403 | $type = $_options[1]; |
||
404 | $xoopsDB->queryF('UPDATE `' . $xoopsDB->prefix('newblocks') . "` SET c_type = '{$type}', options = '', content = " . $xoopsDB->quote($content) . " WHERE bid = {$bid}"); |
||
405 | } |
||
406 | |||
407 | // Deal with block options, convert array values to "," and "|" delimited |
||
408 | $sql = 'UPDATE `' . $xoopsDB->prefix('newblocks') . "` SET options = '' WHERE show_func <> 'b_system_custom_show' AND ( options = 'a:1:{i:0;s:0:\"\";}' OR options = 'a:0:{}' )"; |
||
409 | $result = $xoopsDB->queryF($sql); |
||
410 | $sql = 'SELECT bid, options FROM `' . $xoopsDB->prefix('newblocks') . "` WHERE show_func <> 'b_system_custom_show' AND options <> ''"; |
||
411 | $result = $xoopsDB->query($sql); |
||
412 | while (list($bid, $_options) = $xoopsDB->fetchRow($result)) { |
||
413 | $options = unserialize($_options); |
||
414 | if (empty($options) || !is_array($options)) { |
||
415 | $options = array(); |
||
416 | } |
||
417 | $count = count($options); |
||
418 | //Convert array values to comma-separated |
||
419 | View Code Duplication | for ($i = 0; $i < $count; ++$i) { |
|
420 | if (is_array($options[$i])) { |
||
421 | $options[$i] = implode(',', $options[$i]); |
||
422 | } |
||
423 | } |
||
424 | $options = implode('|', $options); |
||
425 | $sql = 'UPDATE `' . $xoopsDB->prefix('newblocks') . '` SET options = ' . $xoopsDB->quote($options) . " WHERE bid = {$bid}"; |
||
426 | $xoopsDB->queryF($sql); |
||
427 | } |
||
428 | |||
429 | return true; |
||
430 | } |
||
431 | } |
||
435 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.