| Conditions | 13 |
| Paths | 312 |
| Total Lines | 113 |
| Code Lines | 86 |
| Lines | 12 |
| Ratio | 10.62 % |
| Changes | 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 |
||
| 14 | function ShowProductionTable($CurrentUser, $CurrentPlanet, $BuildID, $Template) { |
||
| 15 | $config_resource_multiplier = game_resource_multiplier(); |
||
| 16 | $config_resource_multiplier_plain = game_resource_multiplier(true); |
||
| 17 | |||
| 18 | $CurrentBuildtLvl = mrc_get_level($CurrentUser, $CurrentPlanet, $BuildID); |
||
| 19 | |||
| 20 | $BuildLevel = ($CurrentBuildtLvl > 0) ? $CurrentBuildtLvl : 1; |
||
| 21 | |||
| 22 | $modifiers = sn_get_groups('modifiers'); |
||
| 23 | |||
| 24 | $Prod[STRUC_MINE_METAL] = floor(mrc_modify_value( |
||
|
|
|||
| 25 | $CurrentUser, |
||
| 26 | $CurrentPlanet, |
||
| 27 | $modifiers[MODIFIER_RESOURCE_PRODUCTION], |
||
| 28 | $config_resource_multiplier * $unit_data[P_UNIT_PRODUCTION][RES_METAL]($BuildLevel, 100, $CurrentUser, $CurrentPlanet) |
||
| 29 | )); |
||
| 30 | $Prod[STRUC_MINE_CRYSTAL] = floor(mrc_modify_value( |
||
| 31 | $CurrentUser, |
||
| 32 | $CurrentPlanet, |
||
| 33 | $modifiers[MODIFIER_RESOURCE_PRODUCTION], |
||
| 34 | $config_resource_multiplier * $unit_data[P_UNIT_PRODUCTION][RES_CRYSTAL]($BuildLevel, 100, $CurrentUser, $CurrentPlanet) |
||
| 35 | )); |
||
| 36 | $Prod[STRUC_MINE_DEUTERIUM] = floor(mrc_modify_value( |
||
| 37 | $CurrentUser, |
||
| 38 | $CurrentPlanet, |
||
| 39 | $modifiers[MODIFIER_RESOURCE_PRODUCTION], |
||
| 40 | $config_resource_multiplier * $unit_data[P_UNIT_PRODUCTION][RES_DEUTERIUM]($BuildLevel, 100, $CurrentUser, $CurrentPlanet) |
||
| 41 | )); |
||
| 42 | $Prod[STRUC_MINE_SOLAR] = floor(mrc_modify_value( |
||
| 43 | $CurrentUser, |
||
| 44 | $CurrentPlanet, |
||
| 45 | $modifiers[MODIFIER_RESOURCE_PRODUCTION], |
||
| 46 | $config_resource_multiplier_plain * $unit_data[P_UNIT_PRODUCTION][RES_ENERGY]($BuildLevel, 100, $CurrentUser, $CurrentPlanet) |
||
| 47 | )); |
||
| 48 | |||
| 49 | $ActualProd = floor($Prod[$BuildID]); |
||
| 50 | View Code Duplication | if($BuildID != STRUC_MINE_FUSION) { |
|
| 51 | $ActualNeed = floor($Prod[STRUC_MINE_SOLAR]); |
||
| 52 | } else { |
||
| 53 | $ActualNeed = floor($Prod[STRUC_MINE_DEUTERIUM]); |
||
| 54 | } |
||
| 55 | |||
| 56 | $BuildStartLvl = $CurrentBuildtLvl - 2; |
||
| 57 | if($BuildStartLvl < 1) { |
||
| 58 | $BuildStartLvl = 1; |
||
| 59 | } |
||
| 60 | $Table = ''; |
||
| 61 | $ProdFirst = 0; |
||
| 62 | for($BuildLevel = $BuildStartLvl; $BuildLevel < $BuildStartLvl + 10; $BuildLevel++) { |
||
| 63 | if($BuildID != STRUC_MOON_PHALANX) { |
||
| 64 | $Prod[STRUC_MINE_METAL] = floor(mrc_modify_value( |
||
| 65 | $CurrentUser, |
||
| 66 | $CurrentPlanet, |
||
| 67 | $modifiers[MODIFIER_RESOURCE_PRODUCTION], |
||
| 68 | $config_resource_multiplier * $unit_data[P_UNIT_PRODUCTION][RES_METAL]($BuildLevel, 100, $CurrentUser, $CurrentPlanet) |
||
| 69 | )); |
||
| 70 | $Prod[STRUC_MINE_CRYSTAL] = floor(mrc_modify_value( |
||
| 71 | $CurrentUser, |
||
| 72 | $CurrentPlanet, |
||
| 73 | $modifiers[MODIFIER_RESOURCE_PRODUCTION], |
||
| 74 | $config_resource_multiplier * $unit_data[P_UNIT_PRODUCTION][RES_CRYSTAL]($BuildLevel, 100, $CurrentUser, $CurrentPlanet) |
||
| 75 | )); |
||
| 76 | $Prod[STRUC_MINE_DEUTERIUM] = floor(mrc_modify_value( |
||
| 77 | $CurrentUser, |
||
| 78 | $CurrentPlanet, |
||
| 79 | $modifiers[MODIFIER_RESOURCE_PRODUCTION], |
||
| 80 | $config_resource_multiplier * $unit_data[P_UNIT_PRODUCTION][RES_DEUTERIUM]($BuildLevel, 100, $CurrentUser, $CurrentPlanet) |
||
| 81 | )); |
||
| 82 | $Prod[STRUC_MINE_SOLAR] = floor(mrc_modify_value( |
||
| 83 | $CurrentUser, |
||
| 84 | $CurrentPlanet, |
||
| 85 | $modifiers[MODIFIER_RESOURCE_PRODUCTION], |
||
| 86 | $config_resource_multiplier_plain * $unit_data[P_UNIT_PRODUCTION][RES_ENERGY]($BuildLevel, 100, $CurrentUser, $CurrentPlanet) |
||
| 87 | )); |
||
| 88 | |||
| 89 | $bloc['build_lvl'] = ($CurrentBuildtLvl == $BuildLevel) ? "<font color=\"#ff0000\">" . $BuildLevel . "</font>" : $BuildLevel; |
||
| 90 | if($ProdFirst > 0) { |
||
| 91 | if($BuildID != STRUC_MINE_FUSION) { |
||
| 92 | $bloc['build_gain'] = "<font color=\"lime\">(" . pretty_number(floor($Prod[$BuildID] - $ProdFirst)) . ")</font>"; |
||
| 93 | } else { |
||
| 94 | $bloc['build_gain'] = "<font color=\"lime\">(" . pretty_number(floor($Prod[STRUC_MINE_SOLAR] - $ProdFirst)) . ")</font>"; |
||
| 95 | } |
||
| 96 | } else { |
||
| 97 | $bloc['build_gain'] = ''; |
||
| 98 | } |
||
| 99 | if($BuildID != STRUC_MINE_FUSION) { |
||
| 100 | $bloc['build_prod'] = pretty_number(floor($Prod[$BuildID])); |
||
| 101 | $bloc['build_prod_diff'] = pretty_number(floor($Prod[$BuildID] - $ActualProd), true, true); |
||
| 102 | $bloc['build_need'] = pretty_number(floor($Prod[STRUC_MINE_SOLAR]), true, true); |
||
| 103 | $bloc['build_need_diff'] = pretty_number(floor($Prod[STRUC_MINE_SOLAR] - $ActualNeed), true, true); |
||
| 104 | } else { |
||
| 105 | $bloc['build_prod'] = pretty_number(floor($Prod[STRUC_MINE_SOLAR])); |
||
| 106 | $bloc['build_prod_diff'] = pretty_number(floor($Prod[STRUC_MINE_SOLAR] - $ActualProd), true, true); |
||
| 107 | $bloc['build_need'] = pretty_number(floor($Prod[STRUC_MINE_DEUTERIUM]), true, true); |
||
| 108 | $bloc['build_need_diff'] = pretty_number(floor($Prod[STRUC_MINE_DEUTERIUM] - $ActualNeed), true, true); |
||
| 109 | } |
||
| 110 | View Code Duplication | if($ProdFirst == 0) { |
|
| 111 | if($BuildID != STRUC_MINE_FUSION) { |
||
| 112 | $ProdFirst = floor($Prod[$BuildID]); |
||
| 113 | } else { |
||
| 114 | $ProdFirst = floor($Prod[STRUC_MINE_SOLAR]); |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } else { |
||
| 118 | // Cas particulier de la phalange |
||
| 119 | $bloc['build_lvl'] = ($CurrentBuildtLvl == $BuildLevel) ? "<font color=\"#ff0000\">" . $BuildLevel . "</font>" : $BuildLevel; |
||
| 120 | $bloc['build_range'] = ($BuildLevel * $BuildLevel) - 1; |
||
| 121 | } |
||
| 122 | $Table .= parsetemplate($Template, $bloc); |
||
| 123 | } |
||
| 124 | |||
| 125 | return $Table; |
||
| 126 | } |
||
| 127 | |||
| 277 |
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.