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