| Conditions | 18 |
| Paths | 290 |
| Total Lines | 112 |
| Code Lines | 49 |
| 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 |
||
| 101 | public static function update($feed_id, $owner_uid, $is_cat = false, |
||
| 102 | $update_pcat = true, $pcat_fast = false) { |
||
| 103 | |||
| 104 | // "" (null) is valid and should be cast to 0 (uncategorized) |
||
| 105 | // everything else i.e. tags are not |
||
| 106 | if (!is_numeric($feed_id) && $feed_id) { |
||
| 107 | return; |
||
| 108 | } |
||
| 109 | |||
| 110 | $feed_id = (int) $feed_id; |
||
| 111 | |||
| 112 | $prev_unread = CCache::find($feed_id, $owner_uid, $is_cat, true); |
||
| 113 | |||
| 114 | /* When updating a label, all we need to do is recalculate feed counters |
||
| 115 | * because labels are not cached */ |
||
| 116 | |||
| 117 | if ($feed_id < 0) { |
||
| 118 | CCache::update_all($owner_uid); |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | |||
| 122 | if (!$is_cat) { |
||
| 123 | $table = "ttrss_counters_cache"; |
||
| 124 | } else { |
||
| 125 | $table = "ttrss_cat_counters_cache"; |
||
| 126 | } |
||
| 127 | |||
| 128 | $pdo = Db::pdo(); |
||
| 129 | |||
| 130 | if ($is_cat && $feed_id >= 0) { |
||
| 131 | /* Recalculate counters for child feeds */ |
||
| 132 | |||
| 133 | if (!$pcat_fast) { |
||
| 134 | $sth = $pdo->prepare("SELECT id FROM ttrss_feeds |
||
| 135 | WHERE owner_uid = :uid AND |
||
| 136 | (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL))"); |
||
| 137 | $sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]); |
||
| 138 | |||
| 139 | while ($line = $sth->fetch()) { |
||
| 140 | CCache::update((int) $line["id"], $owner_uid, false, false); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | $sth = $pdo->prepare("SELECT SUM(value) AS sv |
||
| 145 | FROM ttrss_counters_cache, ttrss_feeds |
||
| 146 | WHERE ttrss_feeds.id = feed_id AND |
||
| 147 | (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)) AND |
||
| 148 | ttrss_counters_cache.owner_uid = :uid AND |
||
| 149 | ttrss_feeds.owner_uid = :uid"); |
||
| 150 | $sth->execute([":uid" => $owner_uid, ":cat" => $feed_id]); |
||
| 151 | $row = $sth->fetch(); |
||
| 152 | |||
| 153 | $unread = (int) $row["sv"]; |
||
| 154 | |||
| 155 | } else { |
||
| 156 | $unread = (int) Feeds::getFeedArticles($feed_id, $is_cat, true, $owner_uid); |
||
| 157 | } |
||
| 158 | |||
| 159 | $tr_in_progress = false; |
||
| 160 | |||
| 161 | try { |
||
| 162 | $pdo->beginTransaction(); |
||
| 163 | } catch (Exception $e) { |
||
| 164 | $tr_in_progress = true; |
||
| 165 | } |
||
| 166 | |||
| 167 | $sth = $pdo->prepare("SELECT feed_id FROM $table |
||
| 168 | WHERE owner_uid = ? AND feed_id = ? LIMIT 1"); |
||
| 169 | $sth->execute([$owner_uid, $feed_id]); |
||
| 170 | |||
| 171 | if ($sth->fetch()) { |
||
| 172 | |||
| 173 | $sth = $pdo->prepare("update $table SET |
||
| 174 | value = ?, updated = NOW() WHERE |
||
| 175 | feed_id = ? AND owner_uid = ?"); |
||
| 176 | |||
| 177 | $sth->execute([$unread, $feed_id, $owner_uid]); |
||
| 178 | |||
| 179 | } else { |
||
| 180 | $sth = $pdo->prepare("INSERT INTO $table |
||
| 181 | (feed_id, value, owner_uid, updated) |
||
| 182 | VALUES |
||
| 183 | (?, ?, ?, NOW())"); |
||
| 184 | $sth->execute([$feed_id, $unread, $owner_uid]); |
||
| 185 | } |
||
| 186 | |||
| 187 | if (!$tr_in_progress) { |
||
| 188 | $pdo->commit(); |
||
| 189 | } |
||
| 190 | |||
| 191 | if ($feed_id > 0 && $prev_unread != $unread) { |
||
| 192 | |||
| 193 | if (!$is_cat) { |
||
| 194 | |||
| 195 | /* Update parent category */ |
||
| 196 | |||
| 197 | if ($update_pcat) { |
||
| 198 | |||
| 199 | $sth = $pdo->prepare("SELECT cat_id FROM ttrss_feeds |
||
| 200 | WHERE owner_uid = ? AND id = ?"); |
||
| 201 | $sth->execute([$owner_uid, $feed_id]); |
||
| 202 | |||
| 203 | if ($row = $sth->fetch()) { |
||
| 204 | CCache::update((int) $row["cat_id"], $owner_uid, true, true, true); |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | } else if ($feed_id < 0) { |
||
| 209 | CCache::update_all($owner_uid); |
||
| 210 | } |
||
| 211 | |||
| 212 | return $unread; |
||
| 213 | } |
||
| 216 |