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