| Conditions | 10 |
| Paths | 66 |
| Total Lines | 53 |
| Code Lines | 31 |
| Lines | 0 |
| Ratio | 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 |
||
| 65 | public function execute($parameters) |
||
| 66 | {
|
||
| 67 | // Generate an accepted parameters array. |
||
| 68 | $acceptedParameters = self::getAcceptedParameters(); |
||
| 69 | foreach($acceptedParameters["parameters"] as $key => $value) |
||
| 70 | $acceptedParameters[] = $key; |
||
| 71 | |||
| 72 | // Remove unwanted parameters |
||
| 73 | foreach($parameters as $key => $value) |
||
| 74 | if(!in_array($key, $acceptedParameters)) |
||
| 75 | unset($parameters[$key]); |
||
| 76 | |||
| 77 | // It's the losses endpoint, so it has to be true.. |
||
| 78 | $parameters["losses"] = true; |
||
| 79 | |||
| 80 | // If there aren't enough parameters being passed, throw an error. |
||
| 81 | if(count($parameters) < 2) |
||
| 82 | return array( |
||
| 83 | "type" => "error", |
||
| 84 | "message" => "Invalid request. Atleast two parameters are required." |
||
| 85 | ); |
||
| 86 | |||
| 87 | // API is true |
||
| 88 | $paramters["api"] = true; |
||
|
|
|||
| 89 | |||
| 90 | // At least one of these parameters is required |
||
| 91 | $requiredM = array("characterID", "corporationID", "allianceID", "factionID", "shipTypeID", "solarSystemID", "regionID", "w-space");
|
||
| 92 | $hasRequired = false; |
||
| 93 | foreach($requiredM as $required) |
||
| 94 | $hasRequired |= array_key_exists($required, $parameters); |
||
| 95 | |||
| 96 | if (!isset($parameters["killID"]) && !$hasRequired) |
||
| 97 | return array( |
||
| 98 | "type" => "error", |
||
| 99 | "message" => "Error, must pass atleast one required parameters." |
||
| 100 | ); |
||
| 101 | |||
| 102 | $requestURI = $_SERVER["REQUEST_URI"]; |
||
| 103 | $key = md5("lossesApi:$requestURI");
|
||
| 104 | |||
| 105 | $data = Cache::get($key); |
||
| 106 | if(empty($data)) |
||
| 107 | {
|
||
| 108 | $data = Feed::getKills($parameters); |
||
| 109 | Cache::set($key, $data, 3600); |
||
| 110 | } |
||
| 111 | |||
| 112 | $return = array(); |
||
| 113 | foreach($data as $kill) |
||
| 114 | $return[] = json_decode($kill, true); |
||
| 115 | |||
| 116 | return $return; |
||
| 117 | } |
||
| 118 | } |
||
| 119 |
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.