Conditions | 18 |
Paths | 9218 |
Total Lines | 140 |
Code Lines | 86 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
49 | public function attribute_search($attribute_id, $type, $terms, $sort_by_sql, $sort_key, $sort_dir, $sort_days, $ex_fid_ary, $post_visibility, $topic_id, $author_ary, $author_name, &$id_ary, &$start, $per_page) |
||
50 | { |
||
51 | // generate a search_key from all the options to identify the results |
||
52 | $search_key_array = array( |
||
53 | $attribute_id, |
||
54 | $type, |
||
55 | 'firstpost', |
||
56 | '', |
||
57 | '', |
||
58 | $sort_days, |
||
59 | $sort_key, |
||
60 | $topic_id, |
||
61 | implode(',', $ex_fid_ary), |
||
62 | $post_visibility, |
||
63 | implode(',', $author_ary), |
||
64 | $author_name, |
||
65 | ); |
||
66 | |||
67 | $search_key = md5(implode('#', $search_key_array)); |
||
68 | |||
69 | if ($start < 0) |
||
70 | { |
||
71 | $start = 0; |
||
72 | } |
||
73 | |||
74 | // try reading the results from cache |
||
75 | $result_count = 0; |
||
76 | if ($this->obtain_ids($search_key, $result_count, $id_ary, $start, $per_page, $sort_dir) == SEARCH_RESULT_IN_CACHE) |
||
77 | { |
||
78 | return $result_count; |
||
79 | } |
||
80 | |||
81 | $id_ary = array(); |
||
82 | |||
83 | // Create some display specific sql strings |
||
84 | $sql_attribute = 't.topic_attr_id = ' . (int) $attribute_id; |
||
85 | $sql_fora = (sizeof($ex_fid_ary)) ? ' AND ' . $this->db->sql_in_set('p.forum_id', $ex_fid_ary, true) : ''; |
||
86 | $sql_topic_id = ($topic_id) ? ' AND p.topic_id = ' . (int) $topic_id : ''; |
||
87 | $sql_time = ($sort_days) ? ' AND p.post_time >= ' . (time() - ($sort_days * 86400)) : ''; |
||
88 | $sql_firstpost = ' AND p.post_id = t.topic_first_post_id'; |
||
89 | |||
90 | // Build sql strings for sorting |
||
91 | $sql_sort = $sort_by_sql[$sort_key] . (($sort_dir == 'a') ? ' ASC' : ' DESC'); |
||
92 | $sql_sort_table = $sql_sort_join = ''; |
||
93 | switch ($sql_sort[0]) |
||
94 | { |
||
95 | case 'u': |
||
96 | $sql_sort_table = USERS_TABLE . ' u, '; |
||
97 | $sql_sort_join = ($type == 'posts') ? ' AND u.user_id = p.poster_id ' : ' AND u.user_id = t.topic_poster '; |
||
98 | break; |
||
99 | |||
100 | case 'f': |
||
101 | $sql_sort_table = FORUMS_TABLE . ' f, '; |
||
102 | $sql_sort_join = ' AND f.forum_id = p.forum_id '; |
||
103 | break; |
||
104 | } |
||
105 | |||
106 | $m_approve_fid_sql = ' AND ' . $post_visibility; |
||
107 | |||
108 | // If the cache was completely empty count the results |
||
109 | $calc_results = ($result_count) ? '' : 'SQL_CALC_FOUND_ROWS '; |
||
110 | |||
111 | // Build the query for really selecting the post_ids |
||
112 | if ($type == 'posts') |
||
113 | { |
||
114 | $sql = "SELECT {$calc_results}p.post_id |
||
115 | FROM " . $sql_sort_table . POSTS_TABLE . ' p, ' . TOPICS_TABLE . ' t ' . " |
||
116 | WHERE $sql_attribute |
||
117 | $sql_topic_id |
||
118 | $sql_firstpost |
||
119 | $m_approve_fid_sql |
||
120 | $sql_fora |
||
121 | $sql_sort_join |
||
122 | $sql_time |
||
123 | ORDER BY $sql_sort"; |
||
124 | $field = 'post_id'; |
||
125 | } |
||
126 | else |
||
127 | { |
||
128 | $sql = "SELECT {$calc_results}t.topic_id |
||
129 | FROM " . $sql_sort_table . TOPICS_TABLE . ' t, ' . POSTS_TABLE . " p |
||
130 | WHERE $sql_attribute |
||
131 | $sql_topic_id |
||
132 | $sql_firstpost |
||
133 | $m_approve_fid_sql |
||
134 | $sql_fora |
||
135 | AND t.topic_id = p.topic_id |
||
136 | $sql_sort_join |
||
137 | $sql_time |
||
138 | GROUP BY t.topic_id |
||
139 | ORDER BY $sql_sort"; |
||
140 | $field = 'topic_id'; |
||
141 | } |
||
142 | |||
143 | // Only read one block of posts from the db and then cache it |
||
144 | $result = $this->db->sql_query_limit($sql, $this->config['search_block_size'], $start); |
||
145 | |||
146 | while ($row = $this->db->sql_fetchrow($result)) |
||
147 | { |
||
148 | $id_ary[] = (int) $row[$field]; |
||
149 | } |
||
150 | $this->db->sql_freeresult($result); |
||
151 | |||
152 | // retrieve the total result count if needed |
||
153 | if (!$result_count) |
||
154 | { |
||
155 | $sql_found_rows = 'SELECT FOUND_ROWS() as result_count'; |
||
156 | $result = $this->db->sql_query($sql_found_rows); |
||
157 | $result_count = (int) $this->db->sql_fetchfield('result_count'); |
||
158 | $this->db->sql_freeresult($result); |
||
159 | |||
160 | if (!$result_count) |
||
161 | { |
||
162 | return false; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | if ($start >= $result_count) |
||
167 | { |
||
168 | $start = floor(($result_count - 1) / $per_page) * $per_page; |
||
169 | |||
170 | $result = $this->db->sql_query_limit($sql, $this->config['search_block_size'], $start); |
||
171 | while ($row = $this->db->sql_fetchrow($result)) |
||
172 | { |
||
173 | $id_ary[] = (int) $row[$field]; |
||
174 | } |
||
175 | $this->db->sql_freeresult($result); |
||
176 | |||
177 | $id_ary = array_unique($id_ary); |
||
178 | } |
||
179 | |||
180 | if (sizeof($id_ary)) |
||
181 | { |
||
182 | $this->save_ids($search_key, '', $author_ary, $result_count, $id_ary, $start, $sort_dir); |
||
183 | $id_ary = array_slice($id_ary, 0, $per_page); |
||
184 | |||
185 | return $result_count; |
||
186 | } |
||
187 | return false; |
||
188 | } |
||
189 | } |
||
190 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.