| Conditions | 13 |
| Paths | 1 |
| Total Lines | 48 |
| Code Lines | 21 |
| 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 |
||
| 85 | protected function getCurlCallback(&$content, &$thumbnail) |
||
| 86 | { |
||
| 87 | $url = $this->url; |
||
| 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|bool length of $data or false if we need to stop the download |
||
| 101 | */ |
||
| 102 | return function(&$ch, $data) use ($url, &$content, &$thumbnail) { |
||
| 103 | $content .= $data; |
||
| 104 | $responseCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); |
||
| 105 | if (!empty($responseCode) && $responseCode != 200) { |
||
| 106 | return false; |
||
| 107 | } |
||
| 108 | $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); |
||
| 109 | // we look for image, and ignore application/octet-stream, |
||
| 110 | // which is a the default content type for any binary |
||
| 111 | // @see https://developer.mozilla.org/fr/docs/Web/HTTP/Basics_of_HTTP/MIME_types |
||
| 112 | if (!empty($contentType) |
||
| 113 | && strpos($contentType, 'image/') !== false |
||
| 114 | && strpos($contentType, 'application/octet-stream') === false |
||
| 115 | ) { |
||
| 116 | $thumbnail = $url; |
||
| 117 | return false; |
||
| 118 | } else if (!empty($contentType) |
||
| 119 | && strpos($contentType, 'text/html') === false |
||
| 120 | && strpos($contentType, 'application/octet-stream') === false |
||
| 121 | ) { |
||
| 122 | return false; |
||
| 123 | } |
||
| 124 | if (empty($thumbnail)) { |
||
| 125 | $thumbnail = DefaultFinder::extractMetaTag($data); |
||
| 126 | } |
||
| 127 | // We got everything we want, stop the download. |
||
| 128 | if (!empty($responseCode) && !empty($contentType) && !empty($thumbnail)) { |
||
| 129 | return false; |
||
| 130 | } |
||
| 131 | |||
| 132 | return strlen($data); |
||
| 133 | }; |
||
| 191 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.