Conditions | 10 |
Paths | 48 |
Total Lines | 58 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
33 | function mylinks_notify_iteminfo($category, $item_id) |
||
34 | { |
||
35 | global $xoopsModule, $xoopsModuleConfig, $xoopsConfig, $xoopsDB; |
||
36 | $dirname = basename(dirname(__DIR__)); |
||
37 | |||
38 | $item_id = (isset($item_id) && ((int)$item_id > 0)) ? (int)$item_id : 0; |
||
39 | |||
40 | if (empty($xoopsModule) || $xoopsModule->getVar('dirname') != $dirname) { |
||
41 | $moduleHandler = xoops_getHandler('module'); |
||
42 | $module = $moduleHandler->getByDirname($dirname); |
||
43 | $configHandler = xoops_getHandler('config'); |
||
44 | $config = $configHandler->getConfigsByCat(0, $module->getVar('mid')); |
||
|
|||
45 | } else { |
||
46 | $module = $xoopsModule; |
||
47 | $config = $xoopsModuleConfig; |
||
48 | } |
||
49 | |||
50 | switch ($category) { |
||
51 | case 'category': |
||
52 | // Assume we have a valid category id |
||
53 | $mylinksCatHandler = xoops_getModuleHandler('category', $dirname); |
||
54 | $catObj = $mylinksCatHandler->get($item_id); |
||
55 | if ($catObj) { |
||
56 | $item['name'] = $catObj->getVar('title'); |
||
57 | $item['url'] = XOOPS_URL . "/modules/{$dirname}/viewcat.php?cid={$item_id}"; |
||
58 | /* |
||
59 | $sql = "SELECT title FROM " . $xoopsDB->prefix('mylinks_cat') . " WHERE cid={$item_id}"; |
||
60 | $result = $xoopsDB->query($sql); // TODO: error check |
||
61 | $result_array = $xoopsDB->fetchArray($result); |
||
62 | $item['name'] = $result_array['title']; |
||
63 | $item['url'] = XOOPS_URL . "/modules/" . $module->getVar('dirname') . "/viewcat.php?cid={$item_id}"; |
||
64 | */ |
||
65 | } else { |
||
66 | $item['name'] = ''; |
||
67 | $item['url'] = ''; |
||
68 | } |
||
69 | break; |
||
70 | case 'link': |
||
71 | $sql = 'SELECT cid,title FROM ' . $xoopsDB->prefix('mylinks_links') . " WHERE lid={$item_id}"; |
||
72 | $result = $xoopsDB->query($sql); // TODO: error check |
||
73 | $result_array = $xoopsDB->fetchArray($result); |
||
74 | if (!empty($result_array['title'])) { |
||
75 | $item['name'] = $result_array['title']; |
||
76 | $item['url'] = XOOPS_URL . "/modules/{$dirname}/singlelink.php?cid={$result_array['cid']}&lid={$item_id}"; |
||
77 | } else { |
||
78 | $item['name'] = ''; |
||
79 | $item['url'] = ''; |
||
80 | } |
||
81 | break; |
||
82 | case 'global': |
||
83 | default: |
||
84 | $item['name'] = ''; |
||
85 | $item['url'] = ''; |
||
86 | break; |
||
87 | } |
||
88 | |||
89 | return $item; |
||
90 | } |
||
91 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.