| Conditions | 23 |
| Paths | 15384 |
| Total Lines | 147 |
| Code Lines | 78 |
| 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 |
||
| 31 | function perform() |
||
| 32 | { |
||
| 33 | $challenges_table = 'chess_challenges'; |
||
| 34 | |||
| 35 | $games_table = 'chess_games'; |
||
| 36 | |||
| 37 | $ratings_table = 'chess_ratings'; |
||
| 38 | |||
| 39 | mysqli_connect(DBHOST, DBUSER, DBPASS) or trigger_error('[' . $GLOBALS['xoopsDB']->errno() . '] ' . $GLOBALS['xoopsDB']->error(), E_USER_ERROR); |
||
| 40 | |||
| 41 | mysqli_select_db($GLOBALS['xoopsDB']->conn, DBNAME) or trigger_error('[' . $GLOBALS['xoopsDB']->errno() . '] ' . $GLOBALS['xoopsDB']->error(), E_USER_ERROR); |
||
| 42 | |||
| 43 | // For safety, don't generate test data unless the tables are empty. |
||
| 44 | |||
| 45 | if (!table_empty($challenges_table) || !table_empty($games_table) || !table_empty($ratings_table)) { |
||
| 46 | echo "Tables already contain data - no action performed.\n"; |
||
| 47 | |||
| 48 | exit; |
||
|
|
|||
| 49 | } |
||
| 50 | |||
| 51 | // Generate the challenges table |
||
| 52 | |||
| 53 | $game_types = ['open', 'user']; |
||
| 54 | |||
| 55 | $color_options = ['player2', 'random', 'white', 'black']; |
||
| 56 | |||
| 57 | for ($i = 0; $i < NUM_CHALLENGES; ++$i) { |
||
| 58 | $game_type = rand_array_value($game_types); |
||
| 59 | |||
| 60 | $fen_index = random_int(1, 10); |
||
| 61 | |||
| 62 | $fen = 10 == $fen_index ? 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1' : ''; |
||
| 63 | |||
| 64 | $color_option = rand_array_value($color_options); |
||
| 65 | |||
| 66 | $notify_move_player1 = random_int(0, 1); |
||
| 67 | |||
| 68 | $player1_uid = random_int(1, NUM_USERS); |
||
| 69 | |||
| 70 | if ('open' == $game_type) { |
||
| 71 | $player2_uid = 0; |
||
| 72 | } else { |
||
| 73 | // select $player2_uid != $player1_uid |
||
| 74 | |||
| 75 | do { |
||
| 76 | $player2_uid = random_int(1, NUM_USERS); |
||
| 77 | } while ($player2_uid == $player1_uid); |
||
| 78 | } |
||
| 79 | |||
| 80 | $create_date_max = time(); |
||
| 81 | |||
| 82 | $create_date_min = $create_date_max - 30 * 24 * 3600; |
||
| 83 | |||
| 84 | $create_date = date('Y-m-d H:i:s', random_int($create_date_min, $create_date_max)); |
||
| 85 | |||
| 86 | $is_rated = random_int(0, 1); |
||
| 87 | |||
| 88 | do_query( |
||
| 89 | " |
||
| 90 | INSERT INTO $challenges_table |
||
| 91 | SET |
||
| 92 | game_type = '$game_type', |
||
| 93 | fen = '$fen', |
||
| 94 | color_option = '$color_option', |
||
| 95 | notify_move_player1 = '$notify_move_player1', |
||
| 96 | player1_uid = '$player1_uid', |
||
| 97 | player2_uid = '$player2_uid', |
||
| 98 | create_date = '$create_date', |
||
| 99 | is_rated = '$is_rated' |
||
| 100 | " |
||
| 101 | ); |
||
| 102 | } |
||
| 103 | |||
| 104 | // Generate the games table |
||
| 105 | |||
| 106 | $pgn_results = ['*', '0-1', '1-0', '1/2-1/2']; |
||
| 107 | |||
| 108 | $suspended_explains = ['foo', 'bar', 'baz', 'quux']; |
||
| 109 | |||
| 110 | for ($i = 0; $i < NUM_GAMES; ++$i) { |
||
| 111 | $white_uid = random_int(1, NUM_USERS); |
||
| 112 | |||
| 113 | $black_uid = random_int(1, NUM_USERS); |
||
| 114 | |||
| 115 | // Force some games to be self-play. |
||
| 116 | |||
| 117 | if (10 == random_int(1, 10)) { |
||
| 118 | $black_uid = $white_uid; |
||
| 119 | } |
||
| 120 | |||
| 121 | $create_date_max = time(); |
||
| 122 | |||
| 123 | $create_date_min = $create_date_max - 365 * 24 * 3600; |
||
| 124 | |||
| 125 | $create_date_sec = random_int($create_date_min, $create_date_max); |
||
| 126 | |||
| 127 | $create_date = date('Y-m-d H:i:s', $create_date_sec); |
||
| 128 | |||
| 129 | $is_started = random_int(1, 4) < 4; |
||
| 130 | |||
| 131 | $start_date_sec = $is_started ? $create_date_sec + random_int(3600, 3 * 24 * 3600) : 0; |
||
| 132 | |||
| 133 | $start_date = $is_started ? date('Y-m-d H:i:s', $start_date_sec) : '0000-00-00 00:00:00'; |
||
| 134 | |||
| 135 | $multiple_moves = $is_started && random_int(1, 10) < 10; |
||
| 136 | |||
| 137 | $last_date_sec = $multiple_moves ? $start_date_sec + random_int(3600, 90 * 24 * 3600) : 0; |
||
| 138 | |||
| 139 | $last_date = $multiple_moves ? date('Y-m-d H:i:s', $last_date_sec) : '0000-00-00 00:00:00'; |
||
| 140 | |||
| 141 | $pgn_result = $multiple_moves ? rand_array_value($pgn_results) : '*'; |
||
| 142 | |||
| 143 | if ($multiple_moves && '*' == $pgn_result && 5 == random_int(1, 5)) { |
||
| 144 | $suspended_date = date('Y-m-d H:i:s', $last_date_sec + random_int(60, 72 * 3600)); |
||
| 145 | |||
| 146 | $suspended_uids = [1, $white_uid, $black_uid]; |
||
| 147 | |||
| 148 | $suspended_uid = rand_array_value($suspended_uids); |
||
| 149 | |||
| 150 | $suspended_type = 1 == $suspended_uid ? 'arbiter_suspend' : 'want_arbitration'; |
||
| 151 | |||
| 152 | $suspended_explain = rand_array_value($suspended_explains); |
||
| 153 | |||
| 154 | $suspended = "$suspended_date|$suspended_uid|$suspended_type|$suspended_explain"; |
||
| 155 | } else { |
||
| 156 | $suspended = ''; |
||
| 157 | } |
||
| 158 | |||
| 159 | $is_rated = $white_uid != $black_uid ? random_int(0, 1) : 0; |
||
| 160 | |||
| 161 | do_query( |
||
| 162 | " |
||
| 163 | INSERT INTO $games_table |
||
| 164 | SET |
||
| 165 | white_uid = '$white_uid', |
||
| 166 | black_uid = '$black_uid', |
||
| 167 | create_date = '$create_date', |
||
| 168 | start_date = '$start_date', |
||
| 169 | last_date = '$last_date', |
||
| 170 | pgn_result = '$pgn_result', |
||
| 171 | suspended = '$suspended', |
||
| 172 | is_rated = '$is_rated' |
||
| 173 | " |
||
| 174 | ); |
||
| 175 | } |
||
| 176 | |||
| 177 | $GLOBALS['xoopsDB']->close(); |
||
| 178 | } |
||
| 228 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.