| Conditions | 5 |
| Paths | 5 |
| Total Lines | 62 |
| Code Lines | 32 |
| 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 |
||
| 21 | public function fetch_items() { |
||
| 22 | global $wpdb; |
||
| 23 | |||
| 24 | $vars = array( |
||
| 25 | WPSC_Purchase_Log::ACCEPTED_PAYMENT, |
||
| 26 | WPSC_Purchase_Log::JOB_DISPATCHED, |
||
| 27 | WPSC_Purchase_Log::CLOSED_ORDER, |
||
| 28 | get_current_user_id(), |
||
| 29 | ); |
||
| 30 | |||
| 31 | $sql = $wpdb->prepare( " |
||
| 32 | SELECT |
||
| 33 | d.* |
||
| 34 | FROM " . WPSC_TABLE_DOWNLOAD_STATUS . " AS d |
||
| 35 | INNER JOIN " . WPSC_TABLE_PURCHASE_LOGS . " AS p |
||
| 36 | ON |
||
| 37 | d.purchid = p.id |
||
| 38 | WHERE |
||
| 39 | d.active = 1 AND |
||
| 40 | p.processed IN (%d, %d, %d) AND |
||
| 41 | p.user_ID = %d |
||
| 42 | ORDER BY p.id DESC |
||
| 43 | ", $vars ); |
||
| 44 | |||
| 45 | $downloadables = $wpdb->get_results( $sql ); |
||
| 46 | |||
| 47 | if ( empty( $downloadables ) ) { |
||
| 48 | $this->digital_items = array(); |
||
| 49 | return; |
||
| 50 | } |
||
| 51 | |||
| 52 | $product_ids = wp_list_pluck( $downloadables, 'product_id' ); |
||
| 53 | $product_ids = array_unique( array_map( 'absint', $product_ids ) ); |
||
| 54 | $this->items = get_posts( array( |
||
| 55 | 'post_type' => 'wpsc-product', |
||
| 56 | 'post__in' => $product_ids ) |
||
| 57 | ); |
||
| 58 | $this->total_items = count( $this->items ); |
||
| 59 | |||
| 60 | $this->digital_items = array(); |
||
| 61 | |||
| 62 | foreach ( $downloadables as $file ) { |
||
| 63 | if ( ! in_array( $file->product_id, $product_ids ) ) { |
||
| 64 | continue; |
||
| 65 | } |
||
| 66 | |||
| 67 | if ( ! array_key_exists( $file->product_id, $this->digital_items ) ) { |
||
| 68 | $this->digital_items[ $file->product_id ] = array(); |
||
| 69 | } |
||
| 70 | |||
| 71 | $this->digital_items[ $file->product_id ][] = $file; |
||
| 72 | } |
||
| 73 | |||
| 74 | // cache files |
||
| 75 | $files = wp_list_pluck( $downloadables, 'fileid' ); |
||
| 76 | |||
| 77 | get_posts( array( |
||
| 78 | 'post_type' => 'wpsc-product-file', |
||
| 79 | 'post__in' => $files, |
||
| 80 | ) ); |
||
| 81 | |||
| 82 | } |
||
| 83 | |||
| 136 |