| Conditions | 8 |
| Paths | 10 |
| Total Lines | 52 |
| Code Lines | 30 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 49 | function check_the_ping($request_id) |
||
| 50 | { |
||
| 51 | if (empty($request_id)) { |
||
| 52 | return "Request ID not found!"; |
||
| 53 | } |
||
| 54 | |||
| 55 | $url = "https://check-host.net/check-result/" . $request_id; |
||
| 56 | |||
| 57 | $ch = curl_init(); |
||
| 58 | curl_setopt_array($ch, [ |
||
| 59 | CURLOPT_URL => $url, |
||
| 60 | CURLOPT_RETURNTRANSFER => true, |
||
| 61 | CURLOPT_HTTPHEADER => ["Accept: application/json"], |
||
| 62 | ]); |
||
| 63 | |||
| 64 | $response = curl_exec($ch); |
||
| 65 | |||
| 66 | if (curl_errno($ch)) { |
||
| 67 | echo "cURL error: " . curl_error($ch); |
||
| 68 | return null; |
||
| 69 | } |
||
| 70 | |||
| 71 | $decoded_response = json_decode($response, true); |
||
| 72 | |||
| 73 | $pings = []; |
||
| 74 | $nodes = ["ir1.node.check-host.net", "ir3.node.check-host.net", "ir4.node.check-host.net"]; |
||
| 75 | |||
| 76 | foreach ($nodes as $node) { |
||
| 77 | if (empty($decoded_response[$node])) { |
||
| 78 | continue; |
||
| 79 | } |
||
| 80 | |||
| 81 | $count = 0; |
||
| 82 | $ping_sum = 0; |
||
| 83 | foreach ($decoded_response[$node][0] as $value) { |
||
| 84 | if (@$value[0] == "OK") { |
||
| 85 | $count++; |
||
| 86 | $ping_sum += $value[1]; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | if ($count !== 0) { |
||
| 91 | $ping_avg = (@$ping_sum / $count) * 1000; |
||
| 92 | $pings[$node] = $ping_avg; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | |||
| 96 | $json_pings = json_encode($pings, JSON_PRETTY_PRINT); |
||
| 97 | |||
| 98 | curl_close($ch); |
||
| 99 | |||
| 100 | return $json_pings; |
||
| 101 | } |
||
| 130 |