| Conditions | 16 |
| Paths | 3456 |
| Total Lines | 100 |
| Code Lines | 52 |
| 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 |
||
| 259 | public function get_sql( $args = array() ) { |
||
| 260 | /* @var WPDB $wpdb */ |
||
| 261 | global $wpdb; |
||
| 262 | |||
| 263 | $defaults = array( |
||
| 264 | 'number' => 20, |
||
| 265 | 'offset' => 0, |
||
| 266 | 'paged' => 0, |
||
| 267 | 'orderby' => 'date', |
||
| 268 | 'order' => 'DESC', |
||
| 269 | 'fields' => 'all', |
||
| 270 | 'count' => false, |
||
| 271 | ); |
||
| 272 | |||
| 273 | $args = wp_parse_args( $args, $defaults ); |
||
| 274 | |||
| 275 | // validate params. |
||
| 276 | $this->validate_params( $args ); |
||
| 277 | |||
| 278 | if ( $args['number'] < 1 ) { |
||
| 279 | $args['number'] = 999999999999; |
||
| 280 | } |
||
| 281 | |||
| 282 | // Where clause for primary table. |
||
| 283 | $where = ''; |
||
| 284 | |||
| 285 | // Get sql query for meta. |
||
| 286 | if ( ! empty( $args['meta_query'] ) ) { |
||
| 287 | $meta_query_object = new WP_Meta_Query( $args['meta_query'] ); |
||
| 288 | $meta_query = $meta_query_object->get_sql( 'log', $this->table_name, 'id' ); |
||
| 289 | $where = implode( '', $meta_query ); |
||
| 290 | } |
||
| 291 | |||
| 292 | $where .= ' WHERE 1=1 '; |
||
| 293 | |||
| 294 | // Set offset. |
||
| 295 | if ( empty( $args['offset'] ) && ( 0 < $args['paged'] ) ) { |
||
| 296 | $args['offset'] = $args['number'] * ( $args['paged'] - 1 ); |
||
| 297 | } |
||
| 298 | |||
| 299 | // Set fields. |
||
| 300 | $fields = "{$this->table_name}.*"; |
||
| 301 | if ( is_string( $args['fields'] ) && ( 'all' !== $args['fields'] ) ) { |
||
| 302 | $fields = "{$this->table_name}.{$args['fields']}"; |
||
| 303 | } |
||
| 304 | |||
| 305 | // Set count. |
||
| 306 | if ( $args['count'] ) { |
||
| 307 | $fields = "COUNT({$fields})"; |
||
| 308 | } |
||
| 309 | |||
| 310 | // Specific logs. |
||
| 311 | if ( ! empty( $args['ID'] ) ) { |
||
| 312 | |||
| 313 | if ( ! is_array( $args['ID'] ) ) { |
||
| 314 | $args['ID'] = explode( ',', $args['ID'] ); |
||
| 315 | } |
||
| 316 | $log_ids = implode( ',', array_map( 'intval', $args['ID'] ) ); |
||
| 317 | |||
| 318 | $where .= " AND {$this->table_name}.ID IN( {$log_ids} ) "; |
||
| 319 | } |
||
| 320 | |||
| 321 | // Logs created for a specific date or in a date range |
||
| 322 | if ( ! empty( $args['date_query'] ) ) { |
||
| 323 | $date_query_object = new WP_Date_Query( $args['date_query'], "{$this->table_name}.date" ); |
||
| 324 | $where .= $date_query_object->get_sql(); |
||
| 325 | } |
||
| 326 | |||
| 327 | // Logs create for specific parent. |
||
| 328 | if ( ! empty( $args['log_parent'] ) ) { |
||
| 329 | if ( ! is_array( $args['log_parent'] ) ) { |
||
| 330 | $args['log_parent'] = explode( ',', $args['log_parent'] ); |
||
| 331 | } |
||
| 332 | $parent_ids = implode( ',', array_map( 'intval', $args['log_parent'] ) ); |
||
| 333 | |||
| 334 | $where .= " AND {$this->table_name}.log_parent IN( {$parent_ids} ) "; |
||
| 335 | } |
||
| 336 | |||
| 337 | // Logs create for specific type. |
||
| 338 | if ( ! empty( $args['log_type'] ) ) { |
||
| 339 | if ( ! is_array( $args['log_type'] ) ) { |
||
| 340 | $args['log_type'] = explode( ',', $args['log_type'] ); |
||
| 341 | } |
||
| 342 | |||
| 343 | $log_types = implode( '\',\'', array_map( 'trim', $args['log_type'] ) ); |
||
| 344 | |||
| 345 | $where .= " AND {$this->table_name}.log_type IN( '{$log_types}' ) "; |
||
| 346 | } |
||
| 347 | |||
| 348 | $args['orderby'] = ! array_key_exists( $args['orderby'], $this->get_columns() ) ? 'log_date' : $args['orderby']; |
||
| 349 | |||
| 350 | $args['orderby'] = esc_sql( $args['orderby'] ); |
||
| 351 | $args['order'] = esc_sql( $args['order'] ); |
||
| 352 | |||
| 353 | return $wpdb->prepare( |
||
| 354 | "SELECT {$fields} FROM {$this->table_name} {$where} ORDER BY {$this->table_name}.{$args['orderby']} {$args['order']} LIMIT %d,%d;", |
||
| 355 | absint( $args['offset'] ), |
||
| 356 | absint( $args['number'] ) |
||
| 357 | ); |
||
| 358 | } |
||
| 359 | |||
| 377 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.