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