| Conditions | 19 |
| Paths | 112 |
| Total Lines | 189 |
| Code Lines | 124 |
| Lines | 79 |
| Ratio | 41.8 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 90 | function wpbo_get_graph_data() { |
||
| 91 | |||
| 92 | $query = array( 'data_type' => 'any', 'limit' => - 1 ); |
||
| 93 | $timeframe = unserialize( stripslashes( $_POST['wpbo_analytics_time'] ) ); |
||
| 94 | $popup = isset( $_POST['wpbo_analytics_popup'] ) ? $_POST['wpbo_analytics_popup'] : 'all'; |
||
| 95 | $period = isset( $_POST['wpbo_analytics_period'] ) ? $_POST['wpbo_analytics_period'] : 'today'; |
||
| 96 | |||
| 97 | /* Set the period */ |
||
| 98 | $query['period'] = $timeframe; |
||
| 99 | |||
| 100 | /* Select the popup */ |
||
| 101 | if ( 'all' != $popup ) { |
||
| 102 | $query['popup_id'] = intval( $popup ); |
||
| 103 | } |
||
| 104 | |||
| 105 | /* Separate impressions and conversions */ |
||
| 106 | $query_i = $query; |
||
| 107 | $query_i['data_type'] = 'impression'; |
||
| 108 | |||
| 109 | $query_c = $query; |
||
| 110 | $query_c['data_type'] = 'conversion'; |
||
| 111 | |||
| 112 | /* Get the datas */ |
||
| 113 | $impressions = wpbo_db_get_datas( $query_i, 'OBJECT' ); |
||
| 114 | $conversions = wpbo_db_get_datas( $query_c, 'OBJECT' ); |
||
| 115 | |||
| 116 | /* Set the scale */ |
||
| 117 | $scale = date( 'Y-m-d' ); |
||
| 118 | |||
| 119 | switch ( $period ): |
||
| 120 | |||
| 121 | View Code Duplication | case 'today': |
|
|
|
|||
| 122 | $scale = 'Y-m-d H:00:00'; |
||
| 123 | $timeformat = '%d/%b'; |
||
| 124 | $minticksize = array( 1, 'hour' ); |
||
| 125 | $min = strtotime( date( 'Y-m-d 00:00:00' ) ); |
||
| 126 | $max = strtotime( date( 'Y-m-d 23:59:59' ) ); |
||
| 127 | break; |
||
| 128 | |||
| 129 | View Code Duplication | case 'this_week': |
|
| 130 | $scale = 'Y-m-d 00:00:00'; |
||
| 131 | $timeformat = '%a'; |
||
| 132 | $minticksize = array( 1, 'day' ); |
||
| 133 | $min = strtotime( 'last monday' ); |
||
| 134 | $max = strtotime( 'next sunday' ); |
||
| 135 | break; |
||
| 136 | |||
| 137 | View Code Duplication | case 'last_week': |
|
| 138 | $scale = 'Y-m-d 00:00:00'; |
||
| 139 | $timeformat = '%a'; |
||
| 140 | $minticksize = array( 1, 'day' ); |
||
| 141 | $min = strtotime( 'last monday -7 days' ); |
||
| 142 | $max = strtotime( 'next sunday -7 days' ); |
||
| 143 | break; |
||
| 144 | |||
| 145 | View Code Duplication | case 'this_month': |
|
| 146 | $scale = 'Y-m-d 00:00:00'; |
||
| 147 | $timeformat = '%a'; |
||
| 148 | $minticksize = array( 1, 'day' ); |
||
| 149 | $min = strtotime( 'first day of this month' ); |
||
| 150 | $max = strtotime( 'last day of this month' ); |
||
| 151 | break; |
||
| 152 | |||
| 153 | View Code Duplication | case 'last_month': |
|
| 154 | $scale = 'Y-m-d 00:00:00'; |
||
| 155 | $timeformat = '%a'; |
||
| 156 | $minticksize = array( 1, 'day' ); |
||
| 157 | $min = strtotime( 'first day of last month' ); |
||
| 158 | $max = strtotime( 'last day of last month' ); |
||
| 159 | break; |
||
| 160 | |||
| 161 | case 'this_quarter': |
||
| 162 | |||
| 163 | $scale = 'Y-m-d 00:00:00'; |
||
| 164 | $timeformat = '%b'; |
||
| 165 | $minticksize = array( 1, 'month' ); |
||
| 166 | $quarters = array( 1, 4, 7, 10 ); |
||
| 167 | $month = intval( date( 'm' ) ); |
||
| 168 | |||
| 169 | View Code Duplication | if ( in_array( $month, $quarters ) ) { |
|
| 170 | $current = date( 'Y-m-d', time() ); |
||
| 171 | } else { |
||
| 172 | |||
| 173 | /* Get first month of this quarter */ |
||
| 174 | while ( ! in_array( $month, $quarters ) ) { |
||
| 175 | $month = $month - 1; |
||
| 176 | } |
||
| 177 | |||
| 178 | $current = date( 'Y' ) . '-' . $month . '-' . '01'; |
||
| 179 | |||
| 180 | } |
||
| 181 | |||
| 182 | $current = strtotime( $current ); |
||
| 183 | $min = strtotime( 'first day of this month', $current ); |
||
| 184 | $max = strtotime( 'last day of this month', strtotime( '+2 months', $current ) ); |
||
| 185 | |||
| 186 | break; |
||
| 187 | |||
| 188 | case 'last_quarter': |
||
| 189 | |||
| 190 | $scale = 'Y-m-d 00:00:00'; |
||
| 191 | $timeformat = '%b'; |
||
| 192 | $minticksize = array( 1, 'month' ); |
||
| 193 | $quarters = array( 1, 4, 7, 10 ); |
||
| 194 | $month = intval( date( 'm' ) ) - 3; |
||
| 195 | $rewind = false; |
||
| 196 | |||
| 197 | View Code Duplication | if ( in_array( $month, $quarters ) ) { |
|
| 198 | $current = date( 'Y-m-d', time() ); |
||
| 199 | } else { |
||
| 200 | |||
| 201 | /* Get first month of this quarter */ |
||
| 202 | while ( ! in_array( $month, $quarters ) ) { |
||
| 203 | |||
| 204 | $month = $month - 1; |
||
| 205 | |||
| 206 | /* Rewind to last year after we passed January */ |
||
| 207 | if ( 0 === $month ) { |
||
| 208 | $month = 12; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | $current = date( 'Y' ) . '-' . $month . '-' . '01'; |
||
| 213 | |||
| 214 | } |
||
| 215 | |||
| 216 | /* Set the theorical current date */ |
||
| 217 | $current = false === $rewind ? strtotime( $current ) : strtotime( '-1 year', $current ); |
||
| 218 | $min = strtotime( 'first day of this month', $current ); |
||
| 219 | $max = strtotime( 'last day of this month', strtotime( '+2 months', $current ) ); |
||
| 220 | |||
| 221 | break; |
||
| 222 | |||
| 223 | View Code Duplication | case 'this_year': |
|
| 224 | $scale = 'Y-m-d 00:00:00'; |
||
| 225 | $timeformat = '%b'; |
||
| 226 | $minticksize = array( 1, 'month' ); |
||
| 227 | $min = strtotime( 'first day of January', time() ); |
||
| 228 | $max = strtotime( 'last day of December', time() ); |
||
| 229 | break; |
||
| 230 | |||
| 231 | View Code Duplication | case 'last_year': |
|
| 232 | $scale = 'Y-m-d 00:00:00'; |
||
| 233 | $timeformat = '%b'; |
||
| 234 | $minticksize = array( 1, 'month' ); |
||
| 235 | $min = strtotime( 'first day of January last year', time() ); |
||
| 236 | $max = strtotime( 'last day of December last year', time() ); |
||
| 237 | break; |
||
| 238 | |||
| 239 | endswitch; |
||
| 240 | |||
| 241 | /* Propare global array */ |
||
| 242 | $datas = array( |
||
| 243 | 'impressionsData' => array( |
||
| 244 | 'label' => __( 'Impressions', 'betteroptin' ), |
||
| 245 | 'id' => 'impressions', |
||
| 246 | 'data' => array() |
||
| 247 | ), |
||
| 248 | 'conversionsData' => array( |
||
| 249 | 'label' => __( 'Conversions', 'betteroptin' ), |
||
| 250 | 'id' => 'conversions', |
||
| 251 | 'data' => array() |
||
| 252 | ), |
||
| 253 | 'scale' => array( |
||
| 254 | 'minTickSize' => $minticksize, |
||
| 255 | 'timeformat' => $timeformat |
||
| 256 | ), |
||
| 257 | 'min' => $min * 1000, |
||
| 258 | 'max' => $max * 1000 |
||
| 259 | ); |
||
| 260 | |||
| 261 | /* Get the count on the scaled timestamp */ |
||
| 262 | $imp_array = wpbo_array_merge_combine( $impressions, $scale ); |
||
| 263 | $con_array = wpbo_array_merge_combine( $conversions, $scale ); |
||
| 264 | |||
| 265 | // Get a complete data array for the given timeframe (meaning there is a value for evey single time point) |
||
| 266 | $increment = "$minticksize[0] $minticksize[1]"; // Increment formatted to be used in strtotime() |
||
| 267 | $imp_array = wpbo_fill_hits_period( $imp_array, $min, $max, $increment, $scale ); |
||
| 268 | $con_array = wpbo_fill_hits_period( $con_array, $min, $max, $increment, $scale ); |
||
| 269 | |||
| 270 | /* Add the hits to datas array */ |
||
| 271 | $datas['impressionsData']['data'] = $imp_array; |
||
| 272 | $datas['conversionsData']['data'] = $con_array; |
||
| 273 | |||
| 274 | /* Return results to script */ |
||
| 275 | echo json_encode( $datas ); |
||
| 276 | die(); |
||
| 277 | |||
| 278 | } |
||
| 279 | |||
| 405 | } |
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.