| Conditions | 3 |
| Paths | 3 |
| Total Lines | 58 |
| Code Lines | 48 |
| 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 |
||
| 79 | public function track() { |
||
| 80 | $cart = WC()->cart->get_cart(); |
||
| 81 | $cart_ids = array_map( array( $this, 'get_cart_ids' ), $cart ); |
||
| 82 | $cart_quantities = array_map( array( $this, 'get_cart_quantities' ), $cart ); |
||
| 83 | // Is this the right id? Is WooCommerce id different? |
||
| 84 | $store_id = Jetpack::get_option( 'id' ); |
||
| 85 | $post = get_post(); |
||
| 86 | $post_id = $post->ID; |
||
| 87 | $post_type = get_post_type( $post_id ); |
||
| 88 | $post_name = $post->post_name; |
||
| 89 | $uncachable_page_type = $this->get_uncachable_page_type( $post_id ); |
||
| 90 | if ( $uncachable_page_type ) { |
||
|
|
|||
| 91 | echo "<pre style=\"font-size: 12px;\">"; |
||
| 92 | echo "Tracks properties sent by PHP:"; |
||
| 93 | echo "<br/>"; |
||
| 94 | echo "post type: "; |
||
| 95 | print_r( $post_type ); |
||
| 96 | echo "<br/>"; |
||
| 97 | echo "post name: "; |
||
| 98 | print_r( $post_name ); |
||
| 99 | echo "<br/>"; |
||
| 100 | echo "post id: "; |
||
| 101 | print_r( $post_id ); |
||
| 102 | echo "<br/>"; |
||
| 103 | echo "store id: "; |
||
| 104 | print_r( $store_id ); |
||
| 105 | echo "<br/>"; |
||
| 106 | echo "page type: "; |
||
| 107 | print_r( $uncachable_page_type ); |
||
| 108 | echo "<br/>"; |
||
| 109 | echo "cart ids: "; |
||
| 110 | print_r( $cart_ids ); |
||
| 111 | echo "<br/>"; |
||
| 112 | echo "cart quantities: "; |
||
| 113 | print_r( $cart_quantities ); |
||
| 114 | if ( isset( $_GET[ 'key' ] ) ) { |
||
| 115 | echo "<br/>"; |
||
| 116 | echo "order number: "; |
||
| 117 | print_r( $_GET[ 'key' ] ); |
||
| 118 | } |
||
| 119 | echo "</pre>"; |
||
| 120 | } else { |
||
| 121 | wc_enqueue_js( |
||
| 122 | "var data = { |
||
| 123 | 'post_type': '" . $post_type . "', |
||
| 124 | 'post_id': " . $post_id . ", |
||
| 125 | 'post_name': '" . $post_name . "', |
||
| 126 | 'store_id': " . $store_id . ", |
||
| 127 | 'cart_ids': " . json_encode( $cart_ids ) . ", |
||
| 128 | 'cart_quantities': " . json_encode( $cart_quantities ) . " |
||
| 129 | }; |
||
| 130 | jQuery.post( 'wp-json/jetpack/v4/tracks', data, function(response) { |
||
| 131 | console.log( 'Tracks properties sent by PHP:' ); |
||
| 132 | console.log( JSON.parse( response ) ); |
||
| 133 | });" |
||
| 134 | ); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 140 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: