Conditions | 6 |
Paths | 24 |
Total Lines | 86 |
Lines | 8 |
Ratio | 9.3 % |
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 |
||
123 | function apiadmin_get_usage_log($by_key = '', $handler = '', $request = '', $method = '', $remote_address = '', |
||
124 | $limit = 10, $offset = 0, $count = false, $timebefore = 0, $timeafter = 0, $object_id = 0 |
||
125 | ) { |
||
126 | |||
127 | global $CONFIG; |
||
128 | |||
129 | $limit = (int)$limit; |
||
130 | $offset = (int)$offset; |
||
131 | |||
132 | $where = array(); |
||
133 | |||
134 | /* |
||
135 | $by_user_orig = $by_user; |
||
136 | if (is_array($by_user) && sizeof($by_user) > 0) { |
||
137 | foreach ($by_user as $key => $val) { |
||
138 | $by_user[$key] = (int) $val; |
||
139 | } |
||
140 | } else { |
||
141 | $by_user = (int)$by_user; |
||
142 | } |
||
143 | |||
144 | $event = sanitise_string($event); |
||
145 | $class = sanitise_string($class); |
||
146 | $type = sanitise_string($type); |
||
147 | $subtype = sanitise_string($subtype); |
||
148 | $ip_address = sanitise_string($ip_address); |
||
149 | |||
150 | if ($by_user_orig !== "" && $by_user_orig !== false && $by_user_orig !== null) { |
||
151 | if (is_int($by_user)) { |
||
152 | $where[] = "performed_by_guid=$by_user"; |
||
153 | } else if (is_array($by_user)) { |
||
154 | $where [] = "performed_by_guid in (" . implode(",", $by_user) . ")"; |
||
155 | } |
||
156 | } |
||
157 | if ($event != "") { |
||
158 | $where[] = "event='$event'"; |
||
159 | } |
||
160 | if ($class !== "") { |
||
161 | $where[] = "object_class='$class'"; |
||
162 | } |
||
163 | if ($type != "") { |
||
164 | $where[] = "object_type='$type'"; |
||
165 | } |
||
166 | if ($subtype !== "") { |
||
167 | $where[] = "object_subtype='$subtype'"; |
||
168 | } |
||
169 | |||
170 | if ($timebefore) { |
||
171 | $where[] = "time_created < " . ((int) $timebefore); |
||
172 | } |
||
173 | if ($timeafter) { |
||
174 | $where[] = "time_created > " . ((int) $timeafter); |
||
175 | } |
||
176 | if ($object_id) { |
||
177 | $where[] = "object_id = " . ((int) $object_id); |
||
178 | } |
||
179 | if ($ip_address) { |
||
180 | $where[] = "ip_address = '$ip_address'"; |
||
181 | } |
||
182 | */ |
||
183 | |||
184 | $select = '*'; |
||
185 | if ( $count ) { |
||
186 | $select = 'count(*) as count'; |
||
187 | } |
||
188 | $query = "SELECT $select FROM {$CONFIG->dbprefix}apiadmin_log WHERE 1 "; |
||
189 | foreach ( $where as $w ) { |
||
190 | $query .= " AND $w"; |
||
191 | } |
||
192 | |||
193 | if ( !$count ) { |
||
194 | $query .= ' ORDER BY time_created DESC'; |
||
195 | $query .= " LIMIT $offset, $limit"; // Add order and limit |
||
196 | } |
||
197 | |||
198 | View Code Duplication | if ( $count ) { |
|
199 | $numrows = get_data_row($query); |
||
200 | if ( $numrows ) { |
||
201 | return $numrows->count; |
||
202 | } |
||
203 | } else { |
||
204 | return get_data($query); |
||
205 | } |
||
206 | |||
207 | return false; |
||
208 | } |
||
209 |
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.