| Conditions | 31 |
| Paths | > 20000 |
| Total Lines | 105 |
| Code Lines | 76 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 54 | function eco_get_build_data(&$user, $planet, $unit_id, $unit_level = 0, $only_cost = false) { |
||
| 55 | $rpg_exchange_deuterium = classSupernova::$config->rpg_exchange_deuterium; |
||
| 56 | |||
| 57 | $unit_data = get_unit_param($unit_id); |
||
| 58 | // $unit_db_name = &$unit_data[P_NAME]; |
||
| 59 | |||
| 60 | $unit_factor = $unit_data[P_COST][P_FACTOR] ? $unit_data[P_COST][P_FACTOR] : 1; |
||
| 61 | $price_increase = pow($unit_factor, $unit_level); |
||
| 62 | |||
| 63 | $can_build = isset($unit_data[P_MAX_STACK]) && $unit_data[P_MAX_STACK] ? $unit_data[P_MAX_STACK] : 1000000000000; |
||
| 64 | $can_destroy = 1000000000000; |
||
| 65 | $time = 0; |
||
| 66 | $only_dark_matter = 0; |
||
| 67 | $cost_in_metal = 0; |
||
| 68 | $cost = array(); |
||
| 69 | foreach ($unit_data[P_COST] as $resource_id => $resource_amount) { |
||
| 70 | if ($resource_id === P_FACTOR || !($resource_cost = $resource_amount * $price_increase)) { |
||
| 71 | continue; |
||
| 72 | } |
||
| 73 | |||
| 74 | $cost[BUILD_CREATE][$resource_id] = round($resource_cost); |
||
| 75 | $cost[BUILD_DESTROY][$resource_id] = round($resource_cost / 2); |
||
| 76 | |||
| 77 | $resource_db_name = pname_resource_name($resource_id); |
||
| 78 | $cost_in_metal += $cost[BUILD_CREATE][$resource_id] * classSupernova::$config->__get("rpg_exchange_{$resource_db_name}"); |
||
| 79 | if (in_array($resource_id, sn_get_groups('resources_loot'))) { |
||
| 80 | $time += $resource_cost * classSupernova::$config->__get("rpg_exchange_{$resource_db_name}") / $rpg_exchange_deuterium; |
||
| 81 | $resource_got = mrc_get_level($user, $planet, $resource_id); |
||
| 82 | } elseif ($resource_id == RES_DARK_MATTER) { |
||
| 83 | $resource_got = mrc_get_level($user, null, $resource_id); |
||
| 84 | } elseif ($resource_id == RES_ENERGY) { |
||
| 85 | $resource_got = max(0, $planet['energy_max'] - $planet['energy_used']); |
||
| 86 | } else { |
||
| 87 | $resource_got = 0; |
||
| 88 | } |
||
| 89 | $only_dark_matter = $only_dark_matter ? $only_dark_matter : $resource_id; |
||
| 90 | |||
| 91 | $can_build = min($can_build, $resource_got / $cost[BUILD_CREATE][$resource_id]); |
||
| 92 | $can_destroy = min($can_destroy, $resource_got / $cost[BUILD_DESTROY][$resource_id]); |
||
| 93 | } |
||
| 94 | |||
| 95 | $resources_normalized = 0; |
||
| 96 | $resources_loot = sn_get_groups('resources_loot'); |
||
| 97 | foreach ($resources_loot as $resource_id) { |
||
| 98 | $resource_db_name = pname_resource_name($resource_id); |
||
| 99 | $resource_got = mrc_get_level($user, $planet, $resource_id); |
||
| 100 | $resources_normalized += floor($resource_got) * classSupernova::$config->__get("rpg_exchange_{$resource_db_name}"); |
||
| 101 | } |
||
| 102 | |||
| 103 | $cost[BUILD_AUTOCONVERT] = $only_dark_matter != RES_DARK_MATTER ? max(!empty($unit_data[P_MAX_STACK]) ? $unit_data[P_MAX_STACK] : 0, floor($resources_normalized / $cost_in_metal)) : 0; |
||
| 104 | |||
| 105 | $can_build = $can_build > 0 ? floor($can_build) : 0; |
||
| 106 | $cost['CAN'][BUILD_CREATE] = $can_build; |
||
| 107 | |||
| 108 | $can_destroy = $can_destroy > 0 ? floor($can_destroy) : 0; |
||
| 109 | $cost['CAN'][BUILD_DESTROY] = $can_destroy; |
||
| 110 | |||
| 111 | $cost[P_OPTIONS][P_ONLY_DARK_MATTER] = $only_dark_matter = $only_dark_matter == RES_DARK_MATTER; |
||
| 112 | $cost[P_OPTIONS][P_TIME_RAW] = $time = $time * 60 * 60 / get_game_speed() / 2500; |
||
| 113 | |||
| 114 | // TODO - Вынести в отдельную процедуру расчёт стоимости |
||
| 115 | if ($only_cost) { |
||
| 116 | return $cost; |
||
| 117 | } |
||
| 118 | |||
| 119 | $cost['RESULT'][BUILD_CREATE] = eco_can_build_unit($user, $planet, $unit_id); |
||
| 120 | $cost['RESULT'][BUILD_CREATE] = $cost['RESULT'][BUILD_CREATE] == BUILD_ALLOWED ? ($cost['CAN'][BUILD_CREATE] ? BUILD_ALLOWED : BUILD_NO_RESOURCES) : $cost['RESULT'][BUILD_CREATE]; |
||
| 121 | |||
| 122 | $mercenary = 0; |
||
| 123 | $cost['RESULT'][BUILD_DESTROY] = BUILD_INDESTRUCTABLE; |
||
| 124 | if (in_array($unit_id, sn_get_groups('structures'))) { |
||
| 125 | $time = $time * pow(0.5, mrc_get_level($user, $planet, STRUC_FACTORY_NANO)) / (mrc_get_level($user, $planet, STRUC_FACTORY_ROBOT) + 1); |
||
| 126 | $mercenary = MRC_ENGINEER; |
||
| 127 | $cost['RESULT'][BUILD_DESTROY] = |
||
| 128 | mrc_get_level($user, $planet, $unit_id, null, true) |
||
| 129 | ? ($cost['CAN'][BUILD_DESTROY] |
||
| 130 | ? ($cost['RESULT'][BUILD_CREATE] == BUILD_UNIT_BUSY ? BUILD_UNIT_BUSY : BUILD_ALLOWED) |
||
| 131 | : BUILD_NO_RESOURCES |
||
| 132 | ) |
||
| 133 | : BUILD_NO_UNITS; |
||
| 134 | } elseif (in_array($unit_id, sn_get_groups('tech'))) { |
||
| 135 | $lab_level = eco_get_lab_max_effective_level($user, intval($unit_data['require'][STRUC_LABORATORY])); |
||
| 136 | $time = $time / $lab_level; |
||
| 137 | $mercenary = MRC_ACADEMIC; |
||
| 138 | } elseif (in_array($unit_id, sn_get_groups('defense'))) { |
||
| 139 | $time = $time * pow(0.5, mrc_get_level($user, $planet, STRUC_FACTORY_NANO)) / (mrc_get_level($user, $planet, STRUC_FACTORY_HANGAR) + 1); |
||
| 140 | $mercenary = MRC_FORTIFIER; |
||
| 141 | } elseif (in_array($unit_id, sn_get_groups('fleet'))) { |
||
| 142 | $time = $time * pow(0.5, mrc_get_level($user, $planet, STRUC_FACTORY_NANO)) / (mrc_get_level($user, $planet, STRUC_FACTORY_HANGAR) + 1); |
||
| 143 | $mercenary = MRC_ENGINEER; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($mercenary) { |
||
| 147 | $time = $time / mrc_modify_value($user, $planet, $mercenary, 1); |
||
| 148 | } |
||
| 149 | |||
| 150 | if (in_array($unit_id, sn_get_groups('governors')) || $only_dark_matter) { |
||
| 151 | $cost[RES_TIME][BUILD_CREATE] = $cost[RES_TIME][BUILD_DESTROY] = 0; |
||
| 152 | } else { |
||
| 153 | $cost[RES_TIME][BUILD_CREATE] = round($time >= 1 ? $time : 1); |
||
| 154 | $cost[RES_TIME][BUILD_DESTROY] = round($time / 2 <= 1 ? 1 : $time / 2); |
||
| 155 | } |
||
| 156 | |||
| 157 | return $cost; |
||
| 158 | } |
||
| 159 | |||
| 232 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.