| Conditions | 14 |
| Paths | 21 |
| Total Lines | 70 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 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 |
||
| 154 | private function cache_article_images($content, $site_url, $owner_uid, $article_id) { |
||
| 155 | $status_filename = $article_id."-".sha1($site_url).".status"; |
||
| 156 | |||
| 157 | /* housekeeping might run as a separate user, in this case status/media might not be writable */ |
||
| 158 | if (!$this->cache->isWritable($status_filename)) { |
||
| 159 | Debug::log("status not writable: $status_filename", Debug::$LOG_VERBOSE); |
||
| 160 | return false; |
||
| 161 | } |
||
| 162 | |||
| 163 | Debug::log("status: $status_filename", Debug::$LOG_VERBOSE); |
||
| 164 | |||
| 165 | if ($this->cache->exists($status_filename)) { |
||
| 166 | $status = json_decode($this->cache->get($status_filename), true); |
||
| 167 | } else { |
||
| 168 | $status = []; |
||
| 169 | } |
||
| 170 | |||
| 171 | $status["attempt"] += 1; |
||
| 172 | |||
| 173 | // only allow several download attempts for article |
||
| 174 | if ($status["attempt"] > $this->max_cache_attempts) { |
||
| 175 | Debug::log("too many attempts for $site_url", Debug::$LOG_VERBOSE); |
||
| 176 | return false; |
||
| 177 | } |
||
| 178 | |||
| 179 | if (!$this->cache->put($status_filename, json_encode($status))) { |
||
| 180 | user_error("unable to write status file: $status_filename", E_USER_WARNING); |
||
| 181 | return false; |
||
| 182 | } |
||
| 183 | |||
| 184 | $doc = new DOMDocument(); |
||
| 185 | |||
| 186 | $has_images = false; |
||
| 187 | $success = false; |
||
| 188 | |||
| 189 | if ($doc->loadHTML('<?xml encoding="UTF-8">'.$content)) { |
||
| 190 | $xpath = new DOMXPath($doc); |
||
| 191 | $entries = $xpath->query('(//img[@src])|(//video/source[@src])'); |
||
| 192 | |||
| 193 | foreach ($entries as $entry) { |
||
| 194 | |||
| 195 | if ($entry->hasAttribute('src') && strpos($entry->getAttribute('src'), "data:") !== 0) { |
||
| 196 | |||
| 197 | $has_images = true; |
||
| 198 | |||
| 199 | $src = rewrite_relative_url($site_url, $entry->getAttribute('src')); |
||
| 200 | |||
| 201 | if ($this->cache_url($article_id, $src)) { |
||
| 202 | $success = true; |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | $esth = $this->pdo->prepare("SELECT content_url FROM ttrss_enclosures WHERE post_id = ? AND |
||
| 209 | (content_type LIKE '%image%' OR content_type LIKE '%video%')"); |
||
| 210 | |||
| 211 | if ($esth->execute([$article_id])) { |
||
| 212 | while ($enc = $esth->fetch()) { |
||
| 213 | |||
| 214 | $has_images = true; |
||
| 215 | $url = rewrite_relative_url($site_url, $enc["content_url"]); |
||
| 216 | |||
| 217 | if ($this->cache_url($article_id, $url)) { |
||
| 218 | $success = true; |
||
| 219 | } |
||
| 220 | } |
||
| 221 | } |
||
| 222 | |||
| 223 | return $success || !$has_images; |
||
| 224 | } |
||
| 230 |