| Conditions | 17 |
| Paths | 1 |
| Total Lines | 60 |
| Code Lines | 28 |
| 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 |
||
| 83 | protected function getCurlCallback(?string &$content, ?string &$thumbnail): callable |
||
| 84 | { |
||
| 85 | $url = $this->url; |
||
| 86 | $isRedirected = false; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * cURL callback function for CURLOPT_WRITEFUNCTION (called during the download). |
||
| 90 | * |
||
| 91 | * While downloading the remote page, we check that the HTTP code is 200 and content type is 'html/text' |
||
| 92 | * Then we extract the title and the charset and stop the download when it's done. |
||
| 93 | * |
||
| 94 | * Note that when using CURLOPT_WRITEFUNCTION, we have to manually handle the content retrieved, |
||
| 95 | * hence the $content reference variable. |
||
| 96 | * |
||
| 97 | * @param resource $ch cURL resource |
||
| 98 | * @param string $data chunk of data being downloaded |
||
| 99 | * |
||
| 100 | * @return int|false length of $data or false if we need to stop the download |
||
| 101 | */ |
||
| 102 | return function ($ch, $data) use ($url, &$content, &$thumbnail, &$isRedirected) { |
||
| 103 | $content .= $data; |
||
| 104 | $responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); |
||
| 105 | if (!empty($responseCode) && in_array($responseCode, [301, 302])) { |
||
| 106 | $isRedirected = true; |
||
| 107 | return strlen($data); |
||
| 108 | } |
||
| 109 | if (!empty($responseCode) && $responseCode !== 200) { |
||
| 110 | return false; |
||
| 111 | } |
||
| 112 | // After a redirection, the content type will keep the previous request value |
||
| 113 | // until it finds the next content-type header. |
||
| 114 | if (! $isRedirected || strpos(strtolower($data), 'content-type') !== false) { |
||
| 115 | $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
||
| 116 | } |
||
| 117 | // we look for image, and ignore application/octet-stream, |
||
| 118 | // which is a the default content type for any binary |
||
| 119 | // @see https://developer.mozilla.org/fr/docs/Web/HTTP/Basics_of_HTTP/MIME_types |
||
| 120 | if ( |
||
| 121 | !empty($contentType) |
||
| 122 | && strpos($contentType, 'image/') !== false |
||
| 123 | && strpos($contentType, 'application/octet-stream') === false |
||
| 124 | ) { |
||
| 125 | $thumbnail = $url; |
||
| 126 | return false; |
||
| 127 | } elseif ( |
||
| 128 | !empty($contentType) |
||
| 129 | && strpos($contentType, 'text/html') === false |
||
| 130 | && strpos($contentType, 'application/octet-stream') === false |
||
| 131 | ) { |
||
| 132 | return false; |
||
| 133 | } |
||
| 134 | if (empty($thumbnail)) { |
||
| 135 | $thumbnail = DefaultFinder::extractMetaTag($data); |
||
| 136 | } |
||
| 137 | // We got everything we want, stop the download. |
||
| 138 | if (!empty($responseCode) && !empty($contentType) && !empty($thumbnail)) { |
||
| 139 | return false; |
||
| 140 | } |
||
| 141 | |||
| 142 | return strlen($data); |
||
| 143 | }; |
||
| 196 |