| Conditions | 13 |
| Paths | 1 |
| Total Lines | 46 |
| Code Lines | 18 |
| 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 |
||
| 106 | protected function getCurlCallback(&$content, &$thumbnail) |
||
| 107 | { |
||
| 108 | $isRedirected = false; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * cURL callback function for CURLOPT_WRITEFUNCTION (called during the download). |
||
| 112 | * |
||
| 113 | * While downloading the remote page, we check that the HTTP code is 200 and content type is 'html/text' |
||
| 114 | * Then we extract the title and the charset and stop the download when it's done. |
||
| 115 | * |
||
| 116 | * Note that when using CURLOPT_WRITEFUNCTION, we have to manually handle the content retrieved, |
||
| 117 | * hence the $content reference variable. |
||
| 118 | * |
||
| 119 | * @param resource $ch cURL resource |
||
| 120 | * @param string $data chunk of data being downloaded |
||
| 121 | * |
||
| 122 | * @return int|bool length of $data or false if we need to stop the download |
||
| 123 | */ |
||
| 124 | return function (&$ch, $data) use (&$content, &$thumbnail, &$isRedirected) { |
||
| 125 | $content .= $data; |
||
| 126 | $responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); |
||
| 127 | |||
| 128 | if (!empty($responseCode) && in_array($responseCode, [301, 302])) { |
||
| 129 | $isRedirected = true; |
||
| 130 | return strlen($data); |
||
| 131 | } |
||
| 132 | if (!empty($responseCode) && $responseCode !== 200) { |
||
| 133 | return false; |
||
| 134 | } |
||
| 135 | // After a redirection, the content type will keep the previous request value |
||
| 136 | // until it finds the next content-type header. |
||
| 137 | if (! $isRedirected || strpos(strtolower($data), 'content-type') !== false) { |
||
| 138 | $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
||
| 139 | } |
||
| 140 | if (!empty($contentType) && strpos($contentType, 'text/html') === false) { |
||
| 141 | return false; |
||
| 142 | } |
||
| 143 | if (empty($thumbnail)) { |
||
| 144 | $thumbnail = $this->extractThumbContent($data); |
||
| 145 | } |
||
| 146 | // We got everything we want, stop the download. |
||
| 147 | if (!empty($responseCode) && !empty($contentType) && !empty($thumbnail)) { |
||
| 148 | return false; |
||
| 149 | } |
||
| 150 | |||
| 151 | return strlen($data); |
||
| 152 | }; |
||
| 213 |