Conditions | 17 |
Paths | 3456 |
Total Lines | 101 |
Lines | 26 |
Ratio | 25.74 % |
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 |
||
273 | public function get_sql( $args = array() ) { |
||
274 | /* @var WPDB $wpdb */ |
||
275 | global $wpdb; |
||
276 | |||
277 | $defaults = array( |
||
278 | 'number' => 20, |
||
279 | 'offset' => 0, |
||
280 | 'paged' => 0, |
||
281 | 'orderby' => 'date', |
||
282 | 'order' => 'DESC', |
||
283 | 'fields' => 'all', |
||
284 | 'count' => false, |
||
285 | ); |
||
286 | |||
287 | $args = wp_parse_args( $args, $defaults ); |
||
288 | |||
289 | // validate params. |
||
290 | $this->validate_params( $args ); |
||
291 | |||
292 | if ( $args['number'] < 1 ) { |
||
293 | $args['number'] = 99999999999; |
||
294 | } |
||
295 | |||
296 | // Where clause for primary table. |
||
297 | $where = ''; |
||
298 | |||
299 | // Get sql query for meta. |
||
300 | if ( ! empty( $args['meta_query'] ) ) { |
||
301 | $meta_query_object = new WP_Meta_Query( $args['meta_query'] ); |
||
302 | $meta_query = $meta_query_object->get_sql( 'log', $this->table_name, 'id' ); |
||
303 | $where = implode( '', $meta_query ); |
||
304 | } |
||
305 | |||
306 | $where .= ' WHERE 1=1 '; |
||
307 | |||
308 | // Set offset. |
||
309 | if ( empty( $args['offset'] ) && ( 0 < $args['paged'] ) ) { |
||
310 | $args['offset'] = $args['number'] * ( $args['paged'] - 1 ); |
||
311 | } |
||
312 | |||
313 | // Set fields. |
||
314 | $fields = "{$this->table_name}.*"; |
||
315 | if ( is_string( $args['fields'] ) && ( 'all' !== $args['fields'] ) ) { |
||
316 | $fields = "{$this->table_name}.{$args['fields']}"; |
||
317 | } |
||
318 | |||
319 | // Set count. |
||
320 | if ( $args['count'] ) { |
||
321 | $fields = "COUNT({$fields})"; |
||
322 | } |
||
323 | |||
324 | // Specific logs. |
||
325 | View Code Duplication | if ( ! empty( $args['ID'] ) ) { |
|
326 | |||
327 | if ( ! is_array( $args['ID'] ) ) { |
||
328 | $args['ID'] = explode( ',', $args['ID'] ); |
||
329 | } |
||
330 | $log_ids = implode( ',', array_map( 'intval', $args['ID'] ) ); |
||
331 | |||
332 | $where .= " AND {$this->table_name}.ID IN( {$log_ids} ) "; |
||
333 | } |
||
334 | |||
335 | // Logs created for a specific date or in a date range |
||
336 | if ( ! empty( $args['date_query'] ) ) { |
||
337 | $date_query_object = new WP_Date_Query( $args['date_query'], "{$this->table_name}.log_date" ); |
||
338 | $where .= $date_query_object->get_sql(); |
||
339 | } |
||
340 | |||
341 | // Logs create for specific parent. |
||
342 | View Code Duplication | if ( ! empty( $args['log_parent'] ) ) { |
|
343 | if ( ! is_array( $args['log_parent'] ) ) { |
||
344 | $args['log_parent'] = explode( ',', $args['log_parent'] ); |
||
345 | } |
||
346 | $parent_ids = implode( ',', array_map( 'intval', $args['log_parent'] ) ); |
||
347 | |||
348 | $where .= " AND {$this->table_name}.log_parent IN( {$parent_ids} ) "; |
||
349 | } |
||
350 | |||
351 | // Logs create for specific type. |
||
352 | // is_array check is for backward compatibility. |
||
353 | View Code Duplication | if ( ! empty( $args['log_type'] ) && ! is_array( $args['log_type'] ) ) { |
|
354 | if ( ! is_array( $args['log_type'] ) ) { |
||
355 | $args['log_type'] = explode( ',', $args['log_type'] ); |
||
356 | } |
||
357 | |||
358 | $log_types = implode( '\',\'', array_map( 'trim', $args['log_type'] ) ); |
||
359 | |||
360 | $where .= " AND {$this->table_name}.log_type IN( '{$log_types}' ) "; |
||
361 | } |
||
362 | |||
363 | $args['orderby'] = ! array_key_exists( $args['orderby'], $this->get_columns() ) ? 'log_date' : $args['orderby']; |
||
364 | |||
365 | $args['orderby'] = esc_sql( $args['orderby'] ); |
||
366 | $args['order'] = esc_sql( $args['order'] ); |
||
367 | |||
368 | return $wpdb->prepare( |
||
369 | "SELECT {$fields} FROM {$this->table_name} {$where} ORDER BY {$this->table_name}.{$args['orderby']} {$args['order']} LIMIT %d,%d;", |
||
370 | absint( $args['offset'] ), |
||
371 | absint( $args['number'] ) |
||
372 | ); |
||
373 | } |
||
374 | |||
396 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.