| Conditions | 8 |
| Paths | 36 |
| Total Lines | 89 |
| Code Lines | 53 |
| 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 namespace XoopsModules\Tdmcreate\Files\Includes; |
||
| 76 | public function getNotificationsFunction($moduleDirname) |
||
| 77 | { |
||
| 78 | $stuModuleDirname = mb_strtoupper($moduleDirname); |
||
| 79 | $table = $this->getTable(); |
||
| 80 | $tableName = $table->getVar('table_name'); |
||
| 81 | $tableFieldname = $table->getVar('table_fieldname'); |
||
|
|
|||
| 82 | $tableSoleName = $table->getVar('table_solename'); |
||
| 83 | $fields = $this->getTableFields($table->getVar('table_mid'), $table->getVar('table_id')); |
||
| 84 | $fieldParent = 'cid'; |
||
| 85 | foreach (array_keys($fields) as $f) { |
||
| 86 | $fieldName = $fields[$f]->getVar('field_name'); |
||
| 87 | if ((0 == $f) && (1 == $table->getVar('table_autoincrement'))) { |
||
| 88 | $fieldId = $fieldName; |
||
| 89 | } |
||
| 90 | if (1 == $fields[$f]->getVar('field_parent')) { |
||
| 91 | $fieldParent = $fieldName; |
||
| 92 | } |
||
| 93 | if (1 == $fields[$f]->getVar('field_main')) { |
||
| 94 | $fieldMain = $fieldName; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | if (1 == $table->getVar('table_single')) { |
||
| 98 | $tableSingle = 'single'; |
||
| 99 | } else { |
||
| 100 | $tableSingle = $tableName; |
||
| 101 | } |
||
| 102 | $ret = <<<EOT |
||
| 103 | \n/** |
||
| 104 | * comment callback functions |
||
| 105 | * |
||
| 106 | * @param \$category |
||
| 107 | * @param \$item_id |
||
| 108 | * @return array item|null |
||
| 109 | */ |
||
| 110 | function {$moduleDirname}_notify_iteminfo(\$category, \$item_id) |
||
| 111 | { |
||
| 112 | global \$xoopsModule, \$xoopsModuleConfig, \$xoopsDB; |
||
| 113 | // |
||
| 114 | if (empty(\$xoopsModule) || \$xoopsModule->getVar('dirname') != '{$moduleDirname}') |
||
| 115 | { |
||
| 116 | \$moduleHandler = xoops_getHandler('module'); |
||
| 117 | \$module = \$moduleHandler->getByDirname('{$moduleDirname}'); |
||
| 118 | \$configHandler = xoops_getHandler('config'); |
||
| 119 | \$config =& \$configHandler->getConfigsByCat(0, \$module->getVar('mid')); |
||
| 120 | } else { |
||
| 121 | \$module =& \$xoopsModule; |
||
| 122 | \$config =& \$xoopsModuleConfig; |
||
| 123 | } |
||
| 124 | // |
||
| 125 | switch(\$category) { |
||
| 126 | case 'global': |
||
| 127 | \$item['name'] = ''; |
||
| 128 | \$item['url'] = ''; |
||
| 129 | return \$item; |
||
| 130 | break; |
||
| 131 | case 'category': |
||
| 132 | // Assume we have a valid category id |
||
| 133 | \$sql = 'SELECT {$fieldMain} FROM ' . \$xoopsDB->prefix('{$moduleDirname}_{$tableName}') . ' WHERE {$fieldId} = '. \$item_id; |
||
| 134 | \$result = \$xoopsDB->query(\$sql); // TODO: error check |
||
| 135 | \$result_array = \$xoopsDB->fetchArray(\$result); |
||
| 136 | \$item['name'] = \$result_array['{$fieldMain}']; |
||
| 137 | \$item['url'] = {$stuModuleDirname}_URL . '/{$tableName}.php?{$fieldId}=' . \$item_id; |
||
| 138 | return \$item; |
||
| 139 | break; |
||
| 140 | case '{$tableSoleName}': |
||
| 141 | // Assume we have a valid link id |
||
| 142 | \$sql = 'SELECT {$fieldId}, {$fieldMain} FROM '.\$xoopsDB->prefix('{$moduleDirname}_{$tableName}') . ' WHERE {$fieldId} = ' . \$item_id; |
||
| 143 | \$result = \$xoopsDB->query(\$sql); // TODO: error check |
||
| 144 | \$result_array = \$xoopsDB->fetchArray(\$result); |
||
| 145 | \$item['name'] = \$result_array['{$fieldMain}'];\n |
||
| 146 | EOT; |
||
| 147 | if ($fieldParent) { |
||
| 148 | $ret .= <<<EOT |
||
| 149 | \$item['url'] = {$stuModuleDirname}_URL . '/{$tableSingle}.php?{$fieldParent}=' . \$result_array['{$fieldParent}'] . '&{$fieldId}=' . \$item_id;\n |
||
| 150 | EOT; |
||
| 151 | } else { |
||
| 152 | $ret .= <<<EOT |
||
| 153 | \$item['url'] = {$stuModuleDirname}_URL . '/{$tableSingle}.php?{$fieldId}=' . \$item_id;\n |
||
| 154 | EOT; |
||
| 155 | } |
||
| 156 | $ret .= <<<'EOT' |
||
| 157 | return $item; |
||
| 158 | break; |
||
| 159 | } |
||
| 160 | return null; |
||
| 161 | } |
||
| 162 | EOT; |
||
| 163 | |||
| 164 | return $ret; |
||
| 165 | } |
||
| 185 |