| Conditions | 9 |
| Paths | 54 |
| Total Lines | 125 |
| Code Lines | 69 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 91 | public static function prepare_headlines_digest($user_id, $days = 1, $limit = 1000) { |
||
| 92 | |||
| 93 | require_once "lib/MiniTemplator.class.php"; |
||
| 94 | |||
| 95 | $tpl = new MiniTemplator; |
||
| 96 | $tpl_t = new MiniTemplator; |
||
| 97 | |||
| 98 | $tpl->readTemplateFromFile("templates/digest_template_html.txt"); |
||
| 99 | $tpl_t->readTemplateFromFile("templates/digest_template.txt"); |
||
| 100 | |||
| 101 | $user_tz_string = get_pref('USER_TIMEZONE', $user_id); |
||
| 102 | $local_ts = convert_timestamp(time(), 'UTC', $user_tz_string); |
||
| 103 | |||
| 104 | $tpl->setVariable('CUR_DATE', date('Y/m/d', $local_ts)); |
||
| 105 | $tpl->setVariable('CUR_TIME', date('G:i', $local_ts)); |
||
| 106 | $tpl->setVariable('TTRSS_HOST', SELF_URL_PATH); |
||
| 107 | |||
| 108 | $tpl_t->setVariable('CUR_DATE', date('Y/m/d', $local_ts)); |
||
| 109 | $tpl_t->setVariable('CUR_TIME', date('G:i', $local_ts)); |
||
| 110 | $tpl_t->setVariable('TTRSS_HOST', SELF_URL_PATH); |
||
| 111 | |||
| 112 | $affected_ids = array(); |
||
| 113 | |||
| 114 | $days = (int) $days; |
||
| 115 | |||
| 116 | if (DB_TYPE == "pgsql") { |
||
| 117 | $interval_qpart = "ttrss_entries.date_updated > NOW() - INTERVAL '$days days'"; |
||
| 118 | } else if (DB_TYPE == "mysql") { |
||
| 119 | $interval_qpart = "ttrss_entries.date_updated > DATE_SUB(NOW(), INTERVAL $days DAY)"; |
||
| 120 | } |
||
| 121 | |||
| 122 | $pdo = Db::pdo(); |
||
| 123 | |||
| 124 | $sth = $pdo->prepare("SELECT ttrss_entries.title, |
||
| 125 | ttrss_feeds.title AS feed_title, |
||
| 126 | COALESCE(ttrss_feed_categories.title, '" . __('Uncategorized')."') AS cat_title, |
||
| 127 | date_updated, |
||
| 128 | ttrss_user_entries.ref_id, |
||
| 129 | link, |
||
| 130 | score, |
||
| 131 | content, |
||
| 132 | " . SUBSTRING_FOR_DATE."(last_updated,1,19) AS last_updated |
||
| 133 | FROM |
||
| 134 | ttrss_user_entries,ttrss_entries,ttrss_feeds |
||
| 135 | LEFT JOIN |
||
| 136 | ttrss_feed_categories ON (cat_id = ttrss_feed_categories.id) |
||
| 137 | WHERE |
||
| 138 | ref_id = ttrss_entries.id AND feed_id = ttrss_feeds.id |
||
| 139 | AND include_in_digest = true |
||
| 140 | AND $interval_qpart |
||
| 141 | AND ttrss_user_entries.owner_uid = :user_id |
||
| 142 | AND unread = true |
||
| 143 | AND score >= 0 |
||
| 144 | ORDER BY ttrss_feed_categories.title, ttrss_feeds.title, score DESC, date_updated DESC |
||
| 145 | LIMIT :limit"); |
||
| 146 | $sth->bindParam(':user_id', intval($user_id, 10), PDO::PARAM_INT); |
||
| 147 | $sth->bindParam(':limit', intval($limit, 10), PDO::PARAM_INT); |
||
| 148 | $sth->execute(); |
||
| 149 | |||
| 150 | $headlines_count = 0; |
||
| 151 | $headlines = array(); |
||
| 152 | |||
| 153 | while ($line = $sth->fetch()) { |
||
| 154 | array_push($headlines, $line); |
||
| 155 | $headlines_count++; |
||
| 156 | } |
||
| 157 | |||
| 158 | for ($i = 0; $i < sizeof($headlines); $i++) { |
||
| 159 | |||
| 160 | $line = $headlines[$i]; |
||
| 161 | |||
| 162 | array_push($affected_ids, $line["ref_id"]); |
||
| 163 | |||
| 164 | $updated = make_local_datetime($line['last_updated'], false, |
||
| 165 | $user_id); |
||
| 166 | |||
| 167 | if (get_pref('ENABLE_FEED_CATS', $user_id)) { |
||
| 168 | $line['feed_title'] = $line['cat_title']." / ".$line['feed_title']; |
||
| 169 | } |
||
| 170 | |||
| 171 | $article_labels = Article::get_article_labels($line["ref_id"], $user_id); |
||
| 172 | $article_labels_formatted = ""; |
||
| 173 | |||
| 174 | if (is_array($article_labels) && count($article_labels) > 0) { |
||
| 175 | $article_labels_formatted = implode(", ", array_map(function($a) { |
||
| 176 | return $a[1]; |
||
| 177 | }, $article_labels)); |
||
| 178 | } |
||
| 179 | |||
| 180 | $tpl->setVariable('FEED_TITLE', $line["feed_title"]); |
||
| 181 | $tpl->setVariable('ARTICLE_TITLE', $line["title"]); |
||
| 182 | $tpl->setVariable('ARTICLE_LINK', $line["link"]); |
||
| 183 | $tpl->setVariable('ARTICLE_UPDATED', $updated); |
||
| 184 | $tpl->setVariable('ARTICLE_EXCERPT', |
||
| 185 | truncate_string(strip_tags($line["content"]), 300)); |
||
| 186 | // $tpl->setVariable('ARTICLE_CONTENT', |
||
| 187 | // strip_tags($article_content)); |
||
| 188 | $tpl->setVariable('ARTICLE_LABELS', $article_labels_formatted, true); |
||
| 189 | |||
| 190 | $tpl->addBlock('article'); |
||
| 191 | |||
| 192 | $tpl_t->setVariable('FEED_TITLE', $line["feed_title"]); |
||
| 193 | $tpl_t->setVariable('ARTICLE_TITLE', $line["title"]); |
||
| 194 | $tpl_t->setVariable('ARTICLE_LINK', $line["link"]); |
||
| 195 | $tpl_t->setVariable('ARTICLE_UPDATED', $updated); |
||
| 196 | $tpl_t->setVariable('ARTICLE_LABELS', $article_labels_formatted, true); |
||
| 197 | $tpl_t->setVariable('ARTICLE_EXCERPT', |
||
| 198 | truncate_string(strip_tags($line["content"]), 300, "..."), true); |
||
| 199 | |||
| 200 | $tpl_t->addBlock('article'); |
||
| 201 | |||
| 202 | if ($headlines[$i]['feed_title'] != $headlines[$i + 1]['feed_title']) { |
||
| 203 | $tpl->addBlock('feed'); |
||
| 204 | $tpl_t->addBlock('feed'); |
||
| 205 | } |
||
| 206 | |||
| 207 | } |
||
| 208 | |||
| 209 | $tpl->addBlock('digest'); |
||
| 210 | $tpl->generateOutputToString($tmp); |
||
| 211 | |||
| 212 | $tpl_t->addBlock('digest'); |
||
| 213 | $tpl_t->generateOutputToString($tmp_t); |
||
| 214 | |||
| 215 | return array($tmp, $headlines_count, $affected_ids, $tmp_t); |
||
| 216 | } |
||
| 219 |