| Conditions | 25 |
| Paths | 1356 |
| Total Lines | 131 |
| Code Lines | 85 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 7 | function eco_mrk_trader($user, $planetrow = null) { |
||
| 8 | global $mode; |
||
| 9 | |||
| 10 | $template = gettemplate('market_trader', true); |
||
| 11 | |||
| 12 | $intError = MARKET_DEAL; |
||
| 13 | $planetrow = isset($planetrow) ? $planetrow : $user; |
||
| 14 | |||
| 15 | global $page_title; |
||
| 16 | $classLocale = classLocale::$lang; |
||
| 17 | |||
| 18 | lng_include('market'); |
||
| 19 | |||
| 20 | $rates = array( |
||
| 21 | RES_METAL => classSupernova::$config->rpg_exchange_metal, |
||
| 22 | RES_CRYSTAL => classSupernova::$config->rpg_exchange_crystal, |
||
| 23 | RES_DEUTERIUM => classSupernova::$config->rpg_exchange_deuterium, |
||
| 24 | RES_DARK_MATTER => classSupernova::$config->rpg_exchange_darkMatter |
||
| 25 | ); |
||
| 26 | |||
| 27 | // $dm_db_name = pname_resource_name(RES_DARK_MATTER); |
||
| 28 | $exchangeTo = in_array($exchangeTo = sys_get_param_int('exchangeTo'), sn_get_groups('resources_trader')) ? $exchangeTo : 0; |
||
| 29 | if($exchangeTo && is_array($tradeList = $_POST['spend'])) { |
||
| 30 | $value = 0; |
||
| 31 | $qry = array(); |
||
| 32 | |||
| 33 | sn_db_transaction_start(); |
||
| 34 | if($planetrow['id_owner']) { |
||
| 35 | $global_data = sys_o_get_updated($user, $planetrow, SN_TIME_NOW); |
||
| 36 | $planetrow = $global_data['planet']; |
||
| 37 | } |
||
| 38 | else { |
||
| 39 | // Locking user record |
||
| 40 | $user = db_user_by_id($user['id'], true); |
||
| 41 | } |
||
| 42 | |||
| 43 | foreach(sn_get_groups('resources_trader') as $resource_id) { |
||
| 44 | $amount = floatval($tradeList[$resource_id]); |
||
| 45 | if($amount < 0) { |
||
| 46 | classSupernova::$debug->error('Trying to supply negative resource amount on Black Market Page', 'Hack Attempt', 305); |
||
| 47 | } |
||
| 48 | |||
| 49 | if($resource_id == RES_DARK_MATTER && $exchangeTo == RES_DARK_MATTER) { |
||
| 50 | continue; |
||
| 51 | } |
||
| 52 | |||
| 53 | $resource_db_name = pname_resource_name($resource_id); |
||
| 54 | if($exchangeTo == RES_DARK_MATTER) { |
||
| 55 | $sign = '+'; |
||
| 56 | $amount = floor($tradeList[RES_DARK_MATTER] / 3 * $rates[RES_DARK_MATTER] / $rates[$resource_id]); |
||
| 57 | $value += $amount; |
||
| 58 | } else { |
||
| 59 | $value += floor($amount * $rates[$resource_id] / $rates[$exchangeTo]); |
||
| 60 | if($resource_id == RES_DARK_MATTER) { |
||
| 61 | $amount = 0; |
||
| 62 | } else { |
||
| 63 | if(mrc_get_level($user, $planetrow, $resource_id, true) < $amount) { |
||
| 64 | $intError = MARKET_NO_RESOURCES; |
||
| 65 | break; |
||
| 66 | } |
||
| 67 | |||
| 68 | $sign = '-'; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | if($amount) { |
||
| 73 | $qry[] = "`{$resource_db_name}` = `{$resource_db_name}` {$sign} {$amount}"; |
||
|
|
|||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | if($exchangeTo != RES_DARK_MATTER) { |
||
| 78 | $amount = floor($value); |
||
| 79 | $exchange_to_db_name = pname_resource_name($exchangeTo); |
||
| 80 | $qry[] = "`{$exchange_to_db_name}` = `{$exchange_to_db_name}` + {$amount}"; |
||
| 81 | } |
||
| 82 | |||
| 83 | $operation_cost = classSupernova::$config->rpg_cost_trader * ($exchangeTo == RES_DARK_MATTER ? 3 : 1) + $tradeList[RES_DARK_MATTER]; |
||
| 84 | |||
| 85 | $intError = $value <= 0 ? MARKET_ZERO_DEAL : $intError; |
||
| 86 | $intError = mrc_get_level($user, null, RES_DARK_MATTER) < $operation_cost ? MARKET_NO_DM : $intError; |
||
| 87 | |||
| 88 | if($intError == MARKET_DEAL) { |
||
| 89 | $qry = implode(', ', $qry); |
||
| 90 | $table = $planetrow['id_owner'] ? 'planets' : 'users'; |
||
| 91 | |||
| 92 | doquery("UPDATE {{{$table}}} SET {$qry} WHERE `id` = {$planetrow['id']} LIMIT 1;"); |
||
| 93 | rpg_points_change($user['id'], RPG_MARKET, -$operation_cost, "Using Black Market page {$classLocale['eco_mrk_trader']}"); |
||
| 94 | sn_db_transaction_commit(); |
||
| 95 | |||
| 96 | $intError = MARKET_DEAL_TRADE; |
||
| 97 | $_SERVER['REQUEST_URI'] = ($has_message = strpos($_SERVER['REQUEST_URI'], '&message=')) ? substr($_SERVER['REQUEST_URI'], 0, $has_message) : $_SERVER['REQUEST_URI']; |
||
| 98 | header("Location: {$_SERVER['REQUEST_URI']}&message={$intError}"); |
||
| 99 | ob_end_flush(); |
||
| 100 | die(); |
||
| 101 | } |
||
| 102 | sn_db_transaction_rollback(); |
||
| 103 | $template->assign_block_vars('result', array( |
||
| 104 | 'STATUS' => $intError == MARKET_DEAL ? ERR_NONE : ERR_ERROR, |
||
| 105 | 'MESSAGE' => classLocale::$lang['eco_mrk_errors'][$intError], |
||
| 106 | )); |
||
| 107 | } |
||
| 108 | |||
| 109 | $template->assign_vars(array( |
||
| 110 | 'EXCHANGE_TO_RESOURCE_ID' => $exchangeTo, |
||
| 111 | )); |
||
| 112 | |||
| 113 | foreach(sn_get_groups('resources_trader') as $resource_id) { |
||
| 114 | if($resource_id == RES_DARK_MATTER) { |
||
| 115 | $amount = floor(mrc_get_level($user, null, RES_DARK_MATTER) - classSupernova::$config->rpg_cost_trader); |
||
| 116 | } else { |
||
| 117 | $amount = floor(mrc_get_level($user, $planetrow, $resource_id)); |
||
| 118 | } |
||
| 119 | |||
| 120 | $template->assign_block_vars('resources', array( |
||
| 121 | 'ID' => $resource_id, |
||
| 122 | 'NAME' => classLocale::$lang['tech'][$resource_id], |
||
| 123 | 'AVAIL' => $amount, |
||
| 124 | 'AVAIL_TEXT' => pretty_number($amount), |
||
| 125 | 'SPENT' => ($intError == MARKET_DEAL) ? 0 : max(0, $tradeList[$resource_id]), |
||
| 126 | 'RATE' => $rates[$resource_id], |
||
| 127 | )); |
||
| 128 | } |
||
| 129 | |||
| 130 | $template->assign_vars(array( |
||
| 131 | 'rpg_cost_trader' => classSupernova::$config->rpg_cost_trader, |
||
| 132 | // 'message' => $message, |
||
| 133 | 'MODE' => $mode, |
||
| 134 | )); |
||
| 135 | |||
| 136 | display($template, $page_title); |
||
| 137 | } |
||
| 138 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: