| Conditions | 13 |
| Paths | 144 |
| Total Lines | 65 |
| Code Lines | 43 |
| 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 |
||
| 46 | public function check() |
||
| 47 | { |
||
| 48 | $urls = $this->getURLs(); |
||
| 49 | |||
| 50 | $chs = []; |
||
| 51 | foreach ($urls as $url) { |
||
| 52 | $ch = curl_init(); |
||
| 53 | $chs[] = $ch; |
||
| 54 | curl_setopt_array($ch, $this->getCurlOpts($url)); |
||
| 55 | } |
||
| 56 | // Parallel execution for faster performance |
||
| 57 | $mh = curl_multi_init(); |
||
| 58 | foreach ($chs as $ch) { |
||
| 59 | curl_multi_add_handle($mh, $ch); |
||
| 60 | } |
||
| 61 | |||
| 62 | $active = null; |
||
| 63 | // Execute the handles |
||
| 64 | do { |
||
| 65 | $mrc = curl_multi_exec($mh, $active); |
||
| 66 | curl_multi_select($mh); |
||
| 67 | } while ($active > 0); |
||
| 68 | |||
| 69 | while ($active && $mrc == CURLM_OK) { |
||
| 70 | if (curl_multi_select($mh) != -1) { |
||
| 71 | do { |
||
| 72 | $mrc = curl_multi_exec($mh, $active); |
||
| 73 | } while ($mrc == CURLM_CALL_MULTI_PERFORM); |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | $hasError = false; |
||
| 78 | $msgs = []; |
||
| 79 | foreach ($chs as $ch) { |
||
| 80 | $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL); |
||
| 81 | $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||
| 82 | if (curl_errno($ch) || $code >= 400) { |
||
| 83 | $hasError = true; |
||
| 84 | $msgs[] = sprintf( |
||
| 85 | 'Error retrieving "%s": %s (Code: %s)', |
||
| 86 | $url, |
||
| 87 | curl_error($ch), |
||
| 88 | $code |
||
| 89 | ); |
||
| 90 | } else { |
||
| 91 | $msgs[] = sprintf( |
||
| 92 | 'Success retrieving "%s" (Code: %s)', |
||
| 93 | $url, |
||
| 94 | $code |
||
| 95 | ); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | // Close the handles |
||
| 100 | foreach ($chs as $ch) { |
||
| 101 | curl_multi_remove_handle($mh, $ch); |
||
| 102 | } |
||
| 103 | curl_multi_close($mh); |
||
| 104 | |||
| 105 | if ($hasError) { |
||
| 106 | return [EnvironmentCheck::ERROR, implode(', ', $msgs)]; |
||
| 107 | } |
||
| 108 | |||
| 109 | return [EnvironmentCheck::OK, implode(', ', $msgs)]; |
||
| 110 | } |
||
| 111 | |||
| 134 |