| Conditions | 8 |
| Paths | 10 |
| Total Lines | 58 |
| Code Lines | 25 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 37 | public function hook_house_keeping() { |
||
| 38 | /* since HOOK_UPDATE_TASK is not available to user plugins, this hook is a next best thing */ |
||
| 39 | |||
| 40 | Debug::log("caching media of starred articles for user ".$this->host->get_owner_uid()."..."); |
||
| 41 | |||
| 42 | $sth = $this->pdo->prepare("SELECT content, ttrss_entries.title, |
||
| 43 | ttrss_user_entries.owner_uid, link, site_url, ttrss_entries.id, plugin_data |
||
| 44 | FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON |
||
| 45 | (ttrss_user_entries.feed_id = ttrss_feeds.id) |
||
| 46 | WHERE ref_id = ttrss_entries.id AND |
||
| 47 | marked = true AND |
||
| 48 | site_url != '' AND |
||
| 49 | ttrss_user_entries.owner_uid = ? AND |
||
| 50 | plugin_data NOT LIKE '%starred_cache_images%' |
||
| 51 | ORDER BY ".sql_random_function()." LIMIT 100"); |
||
| 52 | |||
| 53 | if ($sth->execute([$this->host->get_owner_uid()])) { |
||
| 54 | |||
| 55 | $usth = $this->pdo->prepare("UPDATE ttrss_entries SET plugin_data = ? WHERE id = ?"); |
||
| 56 | |||
| 57 | while ($line = $sth->fetch()) { |
||
| 58 | Debug::log("processing article ".$line["title"], Debug::$LOG_VERBOSE); |
||
| 59 | |||
| 60 | if ($line["site_url"]) { |
||
| 61 | $success = $this->cache_article_images($line["content"], $line["site_url"], $line["owner_uid"], $line["id"]); |
||
| 62 | |||
| 63 | if ($success) { |
||
| 64 | $plugin_data = "starred_cache_images,${line['owner_uid']}:".$line["plugin_data"]; |
||
| 65 | |||
| 66 | $usth->execute([$plugin_data, $line['id']]); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | /* actual housekeeping */ |
||
| 73 | |||
| 74 | Debug::log("expiring ".$this->cache->getDir()."..."); |
||
| 75 | |||
| 76 | $files = glob($this->cache->getDir()."/*.{png,mp4,status}", GLOB_BRACE); |
||
| 77 | |||
| 78 | $last_article_id = 0; |
||
| 79 | $article_exists = 1; |
||
| 80 | |||
| 81 | foreach ($files as $file) { |
||
| 82 | list ($article_id, $hash) = explode("-", basename($file)); |
||
| 83 | |||
| 84 | if ($article_id != $last_article_id) { |
||
| 85 | $last_article_id = $article_id; |
||
| 86 | |||
| 87 | $sth = $this->pdo->prepare("SELECT id FROM ttrss_entries WHERE id = ?"); |
||
| 88 | $sth->execute([$article_id]); |
||
| 89 | |||
| 90 | $article_exists = $sth->fetch(); |
||
| 91 | } |
||
| 92 | |||
| 93 | if (!$article_exists) { |
||
| 94 | unlink($file); |
||
| 95 | } |
||
| 230 |