Conditions | 5 |
Paths | 16 |
Total Lines | 52 |
Code Lines | 13 |
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 |
||
18 | private function getLatestNewsQuery($user_id = null, $last_timestamp = null, $words = null) { |
||
19 | // DISTINCT is currently needed because there may |
||
20 | // be multiple matches for a word in news_search_wordlist, one for |
||
21 | // the word is in the title and one for the word in the content. |
||
22 | // That result in the same news being returned twice. It would be |
||
23 | // preferable to fix the DB structure, but we have other priorities |
||
24 | // for now... |
||
25 | $query = "SELECT |
||
26 | DISTINCT(news.news_id), |
||
27 | news.news_headline, |
||
28 | news.news_text, |
||
29 | news.news_date, |
||
30 | news.user_id, |
||
31 | CONCAT(news_image.news_image_id, '.', news_image.news_image_ext) as news_image, |
||
32 | news_image.news_image_id, |
||
33 | users.user_id, |
||
34 | users.userid, |
||
35 | users.email, |
||
36 | users.join_date, |
||
37 | users.karma, |
||
38 | users.show_email, |
||
39 | users.avatar_ext, |
||
40 | (SELECT COUNT(*) |
||
41 | FROM news |
||
42 | WHERE news.user_id = users.user_id) AS user_news_count |
||
43 | FROM news |
||
44 | LEFT JOIN news_image ON news.news_image_id = news_image.news_image_id |
||
45 | LEFT JOIN users ON news.user_id = users.user_id"; |
||
46 | |||
47 | if ($words != null) { |
||
48 | $query .= " LEFT JOIN news_search_wordmatch |
||
49 | ON news_search_wordmatch.news_id = news.news_id |
||
50 | LEFT JOIN news_search_wordlist |
||
51 | ON news_search_wordlist.news_word_id = news_search_wordmatch.news_word_id"; |
||
52 | } |
||
53 | |||
54 | $constraints = []; |
||
55 | if ($user_id != null) { |
||
56 | $constraints[] = "news.user_id = ?"; |
||
57 | } |
||
58 | if ($last_timestamp != null) { |
||
59 | $constraints[] = "news.news_date < ?"; |
||
60 | } |
||
61 | if ($words != null) { |
||
62 | $constraints[] = "news_search_wordlist.news_word_text = ?"; |
||
63 | } |
||
64 | |||
65 | $query .= \AL\Db\assemble_constraints($constraints); |
||
66 | |||
67 | $query .= " ORDER BY news_date DESC LIMIT ?"; |
||
68 | |||
69 | return $query; |
||
70 | } |
||
266 |