Complex classes like Give_Payments_Query often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Give_Payments_Query, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 26 | class Give_Payments_Query extends Give_Stats { |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The args to pass to the give_get_payments() query |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | * @access public |
||
| 33 | * @since 1.0 |
||
| 34 | */ |
||
| 35 | public $args = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The payments found based on the criteria set |
||
| 39 | * |
||
| 40 | * @var array |
||
| 41 | * @access public |
||
| 42 | * @since 1.0 |
||
| 43 | */ |
||
| 44 | public $payments = array(); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Default query arguments. |
||
| 48 | * |
||
| 49 | * Not all of these are valid arguments that can be passed to WP_Query. The ones that are not, are modified before |
||
| 50 | * the query is run to convert them to the proper syntax. |
||
| 51 | * |
||
| 52 | * @access public |
||
| 53 | * @since 1.0 |
||
| 54 | * |
||
| 55 | * @param $args array The array of arguments that can be passed in and used for setting up this payment query. |
||
| 56 | */ |
||
| 57 | 52 | public function __construct( $args = array() ) { |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Set a query variable. |
||
| 87 | * |
||
| 88 | * @access public |
||
| 89 | * @since 1.0 |
||
| 90 | */ |
||
| 91 | 52 | public function __set( $query_var, $value ) { |
|
| 98 | |||
| 99 | /** |
||
| 100 | * Unset a query variable. |
||
| 101 | * |
||
| 102 | * @access public |
||
| 103 | * @since 1.0 |
||
| 104 | */ |
||
| 105 | 52 | public function __unset( $query_var ) { |
|
| 108 | |||
| 109 | /** |
||
| 110 | * Modify the query/query arguments before we retrieve payments. |
||
| 111 | * |
||
| 112 | * @access public |
||
| 113 | * @since 1.0 |
||
| 114 | * @return void |
||
| 115 | */ |
||
| 116 | 52 | public function init() { |
|
| 132 | |||
| 133 | /** |
||
| 134 | * Retrieve payments. |
||
| 135 | * |
||
| 136 | * The query can be modified in two ways; either the action before the |
||
| 137 | * query is run, or the filter on the arguments (existing mainly for backwards |
||
| 138 | * compatibility). |
||
| 139 | * |
||
| 140 | * @access public |
||
| 141 | * @since 1.0 |
||
| 142 | * @return object |
||
| 143 | */ |
||
| 144 | 52 | public function get_payments() { |
|
| 145 | |||
| 146 | 52 | do_action( 'give_pre_get_payments', $this ); |
|
| 147 | |||
| 148 | 52 | $query = new WP_Query( $this->args ); |
|
| 149 | |||
| 150 | $custom_output = array( |
||
| 151 | 52 | 'payments', |
|
| 152 | 52 | 'give_payments', |
|
| 153 | 52 | ); |
|
| 154 | |||
| 155 | 52 | if ( ! in_array( $this->args['output'], $custom_output ) ) { |
|
| 156 | 52 | return $query->posts; |
|
| 157 | } |
||
| 158 | |||
| 159 | if ( $query->have_posts() ) { |
||
| 160 | while ( $query->have_posts() ) { |
||
| 161 | $query->the_post(); |
||
| 162 | |||
| 163 | $payment_id = get_post()->ID; |
||
| 164 | $payment = new Give_Payment( $payment_id ); |
||
| 165 | |||
| 166 | $this->payments[] = apply_filters( 'give_payment', $payment, $payment_id, $this ); |
||
| 167 | } |
||
| 168 | |||
| 169 | wp_reset_postdata(); |
||
| 170 | } |
||
| 171 | |||
| 172 | do_action( 'give_post_get_payments', $this ); |
||
| 173 | |||
| 174 | return $this->payments; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * If querying a specific date, add the proper filters. |
||
| 179 | * |
||
| 180 | * @access public |
||
| 181 | * @since 1.0 |
||
| 182 | * @return void |
||
| 183 | */ |
||
| 184 | 52 | public function date_filter_pre() { |
|
| 193 | |||
| 194 | /** |
||
| 195 | * If querying a specific date, remove filters after the query has been run |
||
| 196 | * to avoid affecting future queries. |
||
| 197 | * |
||
| 198 | * @access public |
||
| 199 | * @since 1.0 |
||
| 200 | * @return void |
||
| 201 | */ |
||
| 202 | public function date_filter_post() { |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Post Status |
||
| 212 | * |
||
| 213 | * @access public |
||
| 214 | * @since 1.0 |
||
| 215 | * @return void |
||
| 216 | */ |
||
| 217 | 52 | public function status() { |
|
| 225 | |||
| 226 | /** |
||
| 227 | * Current Page |
||
| 228 | * |
||
| 229 | * @access public |
||
| 230 | * @since 1.0 |
||
| 231 | * @return void |
||
| 232 | */ |
||
| 233 | 52 | public function page() { |
|
| 241 | |||
| 242 | /** |
||
| 243 | * Posts Per Page |
||
| 244 | * |
||
| 245 | * @access public |
||
| 246 | * @since 1.0 |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | 52 | public function per_page() { |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Current Month |
||
| 266 | * |
||
| 267 | * @access public |
||
| 268 | * @since 1.0 |
||
| 269 | * @return void |
||
| 270 | */ |
||
| 271 | 52 | public function month() { |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Order by |
||
| 282 | * |
||
| 283 | * @access public |
||
| 284 | * @since 1.0 |
||
| 285 | * @return void |
||
| 286 | */ |
||
| 287 | 52 | public function orderby() { |
|
| 298 | |||
| 299 | /** |
||
| 300 | * Specific User |
||
| 301 | * |
||
| 302 | * @access public |
||
| 303 | * @since 1.0 |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | 52 | public function user() { |
|
| 322 | |||
| 323 | /** |
||
| 324 | * Search |
||
| 325 | * |
||
| 326 | * @access public |
||
| 327 | * @since 1.0 |
||
| 328 | * @return void |
||
| 329 | */ |
||
| 330 | 52 | public function search() { |
|
| 331 | |||
| 332 | 52 | if ( ! isset( $this->args['s'] ) ) { |
|
| 333 | 52 | return; |
|
| 334 | } |
||
| 335 | |||
| 336 | 32 | $search = trim( $this->args['s'] ); |
|
| 337 | |||
| 338 | 32 | if ( empty( $search ) ) { |
|
| 339 | return; |
||
| 340 | } |
||
| 341 | |||
| 342 | 32 | $is_email = is_email( $search ) || strpos( $search, '@' ) !== false; |
|
| 343 | 32 | $is_user = strpos( $search, strtolower( 'user:' ) ) !== false; |
|
| 344 | |||
| 345 | 32 | if ( ! empty( $this->args['search_in_notes'] ) ) { |
|
| 346 | |||
| 347 | $notes = give_get_payment_notes( 0, $search ); |
||
| 348 | |||
| 349 | if ( ! empty( $notes ) ) { |
||
| 350 | |||
| 351 | $payment_ids = wp_list_pluck( (array) $notes, 'comment_post_ID' ); |
||
| 352 | |||
| 353 | $this->__set( 'post__in', $payment_ids ); |
||
| 354 | } |
||
| 355 | |||
| 356 | $this->__unset( 's' ); |
||
| 357 | |||
| 358 | 32 | } elseif ( $is_email || strlen( $search ) == 32 ) { |
|
| 359 | |||
| 360 | 32 | $key = $is_email ? '_give_payment_user_email' : '_give_payment_purchase_key'; |
|
| 361 | $search_meta = array( |
||
| 362 | 32 | 'key' => $key, |
|
| 363 | 32 | 'value' => $search, |
|
| 364 | 'compare' => 'LIKE' |
||
| 365 | 32 | ); |
|
| 366 | |||
| 367 | 32 | $this->__set( 'meta_query', $search_meta ); |
|
| 368 | 32 | $this->__unset( 's' ); |
|
| 369 | |||
| 370 | 32 | } elseif ( $is_user ) { |
|
| 371 | |||
| 372 | $search_meta = array( |
||
| 373 | 'key' => '_give_payment_user_id', |
||
| 374 | 'value' => trim( str_replace( 'user:', '', strtolower( $search ) ) ) |
||
| 375 | ); |
||
| 376 | |||
| 377 | $this->__set( 'meta_query', $search_meta ); |
||
| 378 | |||
| 379 | if ( give_get_option( 'enable_sequential' ) ) { |
||
| 380 | |||
| 381 | $search_meta = array( |
||
| 382 | 'key' => '_give_payment_number', |
||
| 383 | 'value' => $search, |
||
| 384 | 'compare' => 'LIKE' |
||
| 385 | ); |
||
| 386 | |||
| 387 | $this->__set( 'meta_query', $search_meta ); |
||
| 388 | |||
| 389 | $this->args['meta_query']['relation'] = 'OR'; |
||
| 390 | |||
| 391 | } |
||
| 392 | |||
| 393 | $this->__unset( 's' ); |
||
| 394 | |||
| 395 | } elseif ( |
||
| 396 | give_get_option( 'enable_sequential' ) && |
||
| 397 | ( |
||
| 398 | false !== strpos( $search, give_get_option( 'sequential_prefix' ) ) || |
||
| 399 | false !== strpos( $search, give_get_option( 'sequential_postfix' ) ) |
||
| 400 | ) |
||
| 401 | ) { |
||
| 402 | |||
| 403 | $search_meta = array( |
||
| 404 | 'key' => '_give_payment_number', |
||
| 405 | 'value' => $search, |
||
| 406 | 'compare' => 'LIKE' |
||
| 407 | ); |
||
| 408 | |||
| 409 | $this->__set( 'meta_query', $search_meta ); |
||
| 410 | $this->__unset( 's' ); |
||
| 411 | |||
| 412 | } elseif ( is_numeric( $search ) ) { |
||
| 413 | |||
| 414 | $post = get_post( $search ); |
||
| 415 | |||
| 416 | if ( is_object( $post ) && $post->post_type == 'give_payment' ) { |
||
| 417 | |||
| 418 | $arr = array(); |
||
| 419 | $arr[] = $search; |
||
| 420 | $this->__set( 'post__in', $arr ); |
||
| 421 | $this->__unset( 's' ); |
||
| 422 | } |
||
| 423 | |||
| 424 | } elseif ( '#' == substr( $search, 0, 1 ) ) { |
||
| 425 | |||
| 426 | $search = str_replace( '#:', '', $search ); |
||
| 427 | $search = str_replace( '#', '', $search ); |
||
| 428 | $this->__set( 'give_forms', $search ); |
||
| 429 | $this->__unset( 's' ); |
||
| 430 | |||
| 431 | } else { |
||
| 432 | $this->__set( 's', $search ); |
||
| 433 | |||
| 434 | } |
||
| 435 | |||
| 436 | 32 | } |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Payment Mode |
||
| 440 | * |
||
| 441 | * @access public |
||
| 442 | * @since 1.0 |
||
| 443 | * @return void |
||
| 444 | */ |
||
| 445 | 52 | public function mode() { |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Children |
||
| 460 | * |
||
| 461 | * @access public |
||
| 462 | * @since 1.0 |
||
| 463 | * @return void |
||
| 464 | */ |
||
| 465 | 52 | public function children() { |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Specific Give Form |
||
| 474 | * |
||
| 475 | * @access public |
||
| 476 | * @since 1.0 |
||
| 477 | * @return void |
||
| 478 | */ |
||
| 479 | 52 | public function give_forms() { |
|
| 526 | } |
||
| 527 |
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.