| Conditions | 6 |
| Paths | 8 |
| Total Lines | 117 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 102 | function chess_ratings($start = 0, $msg = '', $msg_class = 'errorMsg') |
||
| 103 | { |
||
| 104 | global $xoopsDB, $xoopsUser, $xoopsModule, $xoopsTpl; |
||
| 105 | |||
| 106 | // Display ratings. |
||
| 107 | |||
| 108 | // Get maximum number of items to display on a page, and constrain it to a reasonable value. |
||
| 109 | |||
| 110 | $max_items_to_display = chess_moduleConfig('max_items'); |
||
| 111 | |||
| 112 | $max_items_to_display = min(max($max_items_to_display, 1), 1000); |
||
| 113 | |||
| 114 | $games_table = $xoopsDB->prefix('chess_games'); |
||
|
|
|||
| 115 | |||
| 116 | $ratings_table = $xoopsDB->prefix('chess_ratings'); |
||
| 117 | |||
| 118 | // Two queries are performed, one without a limit clause to count the total number of rows for the page navigator, |
||
| 119 | |||
| 120 | // and one with a limit clause to get the data for display on the current page. |
||
| 121 | |||
| 122 | // SQL_CALC_FOUND_ROWS and FOUND_ROWS(), available in MySQL 4.0.0, provide a more efficient way of doing this. |
||
| 123 | |||
| 124 | $result = $xoopsDB->query( |
||
| 125 | " |
||
| 126 | SELECT COUNT(*) |
||
| 127 | FROM $ratings_table AS p |
||
| 128 | " |
||
| 129 | ); |
||
| 130 | |||
| 131 | [$num_items] = $xoopsDB->fetchRow($result); |
||
| 132 | |||
| 133 | $xoopsDB->freeRecordSet($result); |
||
| 134 | |||
| 135 | $pagenav = new XoopsPageNav($num_items, $max_items_to_display, $start, 'start'); |
||
| 136 | |||
| 137 | $result = $xoopsDB->query( |
||
| 138 | " |
||
| 139 | SELECT player_uid, rating, (games_won+games_lost+games_drawn) AS games_played |
||
| 140 | FROM $ratings_table |
||
| 141 | ORDER BY rating DESC, player_uid ASC |
||
| 142 | LIMIT $start, $max_items_to_display |
||
| 143 | " |
||
| 144 | ); |
||
| 145 | |||
| 146 | // user IDs that will require mapping to usernames |
||
| 147 | |||
| 148 | $userids = []; |
||
| 149 | |||
| 150 | $players = []; |
||
| 151 | |||
| 152 | while (false !== ($row = $xoopsDB->fetchArray($result))) { |
||
| 153 | // save user IDs that will require mapping to usernames |
||
| 154 | |||
| 155 | $userids[] = $row['player_uid']; |
||
| 156 | |||
| 157 | $players[] = [ |
||
| 158 | 'player_uid' => $row['player_uid'], |
||
| 159 | 'rating' => $row['rating'], |
||
| 160 | 'games_played' => $row['games_played'], |
||
| 161 | ]; |
||
| 162 | } |
||
| 163 | |||
| 164 | $xoopsDB->freeRecordSet($result); |
||
| 165 | |||
| 166 | // get mapping of user IDs to usernames |
||
| 167 | |||
| 168 | $memberHandler = xoops_getHandler('member'); |
||
| 169 | |||
| 170 | $criteria = new \Criteria('uid', '(' . implode(',', $userids) . ')', 'IN'); |
||
| 171 | |||
| 172 | $usernames = $memberHandler->getUserList($criteria); |
||
| 173 | |||
| 174 | // add usernames to $players |
||
| 175 | |||
| 176 | foreach ($players as $k => $player) { |
||
| 177 | $players[$k]['player_uname'] = $usernames[$player['player_uid']] ?? '?'; |
||
| 178 | } |
||
| 179 | |||
| 180 | // Display form for arbiter to recalculate ratings. |
||
| 181 | |||
| 182 | if (is_object($xoopsUser) && $xoopsUser->isAdmin($xoopsModule->getVar('mid'))) { |
||
| 183 | // security token added below |
||
| 184 | |||
| 185 | $form = new XoopsThemeForm(_MD_CHESS_RECALC_RATINGS, 'form1', 'ratings.php'); |
||
| 186 | |||
| 187 | // checkbox (initially unchecked) |
||
| 188 | |||
| 189 | $checked_value = 1; |
||
| 190 | |||
| 191 | $checkbox_confirm_recalc_ratings = new XoopsFormCheckBox('', 'confirm_recalc_ratings', !$checked_value); |
||
| 192 | |||
| 193 | $checkbox_confirm_recalc_ratings->addOption($checked_value, _MD_CHESS_RECALC_CONFIRM); |
||
| 194 | |||
| 195 | $form->addElement($checkbox_confirm_recalc_ratings); |
||
| 196 | |||
| 197 | $form->addElement(new XoopsFormButton('', 'submit_recalc_ratings', _MD_CHESS_SUBMIT_BUTTON, 'submit')); |
||
| 198 | |||
| 199 | $form->assign($xoopsTpl); |
||
| 200 | |||
| 201 | // security token |
||
| 202 | |||
| 203 | // This method is used because form is templated. |
||
| 204 | |||
| 205 | $xoopsTpl->assign('chess_xoops_request_token', is_object($GLOBALS['xoopsSecurity']) ? $GLOBALS['xoopsSecurity']->getTokenHTML() : ''); |
||
| 206 | } |
||
| 207 | |||
| 208 | // Template variables |
||
| 209 | |||
| 210 | $xoopsTpl->assign('chess_provisional_games', chess_ratings_num_provisional_games()); |
||
| 211 | |||
| 212 | $xoopsTpl->assign('chess_msg', $msg); |
||
| 213 | |||
| 214 | $xoopsTpl->assign('chess_msg_class', $msg_class); |
||
| 215 | |||
| 216 | $xoopsTpl->assign('chess_players_pagenav', $pagenav->renderNav()); |
||
| 217 | |||
| 218 | $xoopsTpl->assign('chess_players', $players); |
||
| 219 | } |
||
| 220 |