Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EEH_Event_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 EEH_Event_Query, and based on these observations, apply Extract Interface, too.
1 | <?php if ( ! defined( 'EVENT_ESPRESSO_VERSION' ) ) { |
||
35 | class EEH_Event_Query { |
||
36 | |||
37 | /** |
||
38 | * Start Date |
||
39 | * @var $_event_query_month |
||
40 | */ |
||
41 | protected static $_event_query_month; |
||
42 | |||
43 | /** |
||
44 | * Category |
||
45 | * @var $_event_query_category |
||
46 | */ |
||
47 | protected static $_event_query_category; |
||
48 | |||
49 | /** |
||
50 | * whether to display expired events in the event list |
||
51 | * @var bool $_show_expired |
||
52 | */ |
||
53 | protected static $_event_query_show_expired = false; |
||
54 | |||
55 | /** |
||
56 | * list of params for controlling how the query results are ordered |
||
57 | * @var array $_event_query_orderby |
||
58 | */ |
||
59 | protected static $_event_query_orderby = array(); |
||
60 | |||
61 | /** |
||
62 | * direction list is sorted |
||
63 | * @var string $_event_query_sort |
||
64 | */ |
||
65 | protected static $_event_query_sort; |
||
66 | |||
67 | /** |
||
68 | * list of params used to build the query's various clauses |
||
69 | * @var $_query_params |
||
70 | */ |
||
71 | protected static $_query_params = array(); |
||
72 | |||
73 | |||
74 | |||
75 | /** |
||
76 | * add_query_filters |
||
77 | * |
||
78 | */ |
||
79 | public static function add_query_filters() { |
||
83 | |||
84 | |||
85 | |||
86 | /** |
||
87 | * apply_query_filters |
||
88 | * |
||
89 | * @param \WP_Query $WP_Query |
||
90 | * @return bool |
||
91 | */ |
||
92 | public static function apply_query_filters( WP_Query $WP_Query ) { |
||
98 | |||
99 | |||
100 | /** |
||
101 | * filter_query_parts |
||
102 | * |
||
103 | * @param \WP_Query $WP_Query |
||
104 | */ |
||
105 | public static function filter_query_parts( WP_Query $WP_Query ) { |
||
118 | |||
119 | |||
120 | |||
121 | /** |
||
122 | * set_query_params |
||
123 | * |
||
124 | * @param string $month |
||
125 | * @param string $category |
||
126 | * @param bool $show_expired |
||
127 | * @param string $orderby |
||
128 | * @param string $sort |
||
129 | */ |
||
130 | public static function set_query_params( |
||
144 | |||
145 | |||
146 | |||
147 | /** |
||
148 | * _display_month - what month should the event list display events for? |
||
149 | * |
||
150 | * @param string $month |
||
151 | * @return string |
||
152 | */ |
||
153 | private static function _display_month( $month = '' ) { |
||
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * _event_category_slug |
||
161 | * |
||
162 | * @param string $category |
||
163 | * @return string |
||
164 | */ |
||
165 | private static function _event_category_slug( $category = '' ) { |
||
168 | |||
169 | |||
170 | |||
171 | /** |
||
172 | * _show_expired |
||
173 | * |
||
174 | * @param bool $show_expired |
||
175 | * @return boolean |
||
176 | */ |
||
177 | private static function _show_expired( $show_expired = false ) { |
||
184 | |||
185 | |||
186 | |||
187 | /** |
||
188 | * _orderby |
||
189 | * |
||
190 | * @param string $orderby |
||
191 | * @return array |
||
192 | */ |
||
193 | private static function _orderby( $orderby = 'start_date' ) { |
||
202 | |||
203 | |||
204 | |||
205 | /** |
||
206 | * _sort |
||
207 | * |
||
208 | * @param string $sort |
||
209 | * @return string |
||
210 | */ |
||
211 | private static function _sort( $sort = 'ASC' ) { |
||
217 | |||
218 | |||
219 | |||
220 | /** |
||
221 | * Filters the clauses for the WP_Query object |
||
222 | * |
||
223 | * @param array $clauses array of clauses |
||
224 | * @param WP_Query $wp_query |
||
225 | * |
||
226 | * @return array array of clauses |
||
227 | */ |
||
228 | public static function posts_clauses( $clauses, WP_Query $wp_query ) { |
||
235 | |||
236 | |||
237 | |||
238 | /** |
||
239 | * posts_fields |
||
240 | * |
||
241 | * @param $SQL |
||
242 | * @param WP_Query $wp_query |
||
243 | * @return string |
||
244 | * @throws \EE_Error |
||
245 | */ |
||
246 | public static function posts_fields( $SQL, WP_Query $wp_query ) { |
||
253 | |||
254 | |||
255 | |||
256 | /** |
||
257 | * posts_join_sql_for_terms |
||
258 | * |
||
259 | * @param array $orderby_params |
||
260 | * @return string |
||
261 | * @throws \EE_Error |
||
262 | */ |
||
263 | public static function posts_fields_sql_for_orderby( $orderby_params = array() ) { |
||
292 | |||
293 | |||
294 | |||
295 | /** |
||
296 | * posts_join |
||
297 | * |
||
298 | * @param string $SQL |
||
299 | * @param WP_Query $wp_query |
||
300 | * @return string |
||
301 | * @throws \EE_Error |
||
302 | */ |
||
303 | public static function posts_join( $SQL = '', WP_Query $wp_query ) { |
||
312 | |||
313 | |||
314 | |||
315 | /** |
||
316 | * posts_join_sql_for_terms |
||
317 | * |
||
318 | * @param string $SQL |
||
319 | * @param boolean $show_expired if TRUE, then displayed past events |
||
320 | * @return string |
||
321 | * @throws \EE_Error |
||
322 | */ |
||
323 | public static function posts_join_sql_for_show_expired( $SQL = '', $show_expired = FALSE ) { |
||
334 | |||
335 | |||
336 | |||
337 | /** |
||
338 | * posts_join_sql_for_terms |
||
339 | * |
||
340 | * @param string $SQL |
||
341 | * @param string $join_terms pass TRUE or term string, doesn't really matter since this value doesn't really get used for anything yet |
||
342 | * @return string |
||
343 | */ |
||
344 | View Code Duplication | public static function posts_join_sql_for_terms( $SQL = '', $join_terms = '' ) { |
|
353 | |||
354 | |||
355 | |||
356 | /** |
||
357 | * posts_join_for_orderby |
||
358 | * usage: $SQL .= EEH_Event_Query::posts_join_for_orderby( $orderby_params ); |
||
359 | * |
||
360 | * @param string $SQL |
||
361 | * @param array $orderby_params |
||
362 | * @return string |
||
363 | * @throws \EE_Error |
||
364 | */ |
||
365 | public static function posts_join_for_orderby( $SQL = '', $orderby_params = array() ) { |
||
398 | |||
399 | |||
400 | |||
401 | /** |
||
402 | * _posts_join_for_datetime |
||
403 | * |
||
404 | * @param string $SQL |
||
405 | * @param string $join |
||
406 | * @return string |
||
407 | * @throws \EE_Error |
||
408 | */ |
||
409 | protected static function _posts_join_for_datetime( $SQL = '', $join = '' ) { |
||
418 | |||
419 | |||
420 | |||
421 | /** |
||
422 | * _posts_join_for_event_venue |
||
423 | * |
||
424 | * @param string $SQL |
||
425 | * @return string |
||
426 | * @throws \EE_Error |
||
427 | */ |
||
428 | protected static function _posts_join_for_event_venue( $SQL = '' ) { |
||
454 | |||
455 | |||
456 | |||
457 | /** |
||
458 | * _posts_join_for_venue_state |
||
459 | * |
||
460 | * @param string $SQL |
||
461 | * @return string |
||
462 | * @throws \EE_Error |
||
463 | */ |
||
464 | protected static function _posts_join_for_venue_state( $SQL = '' ) { |
||
484 | |||
485 | |||
486 | |||
487 | /** |
||
488 | * posts_where |
||
489 | * |
||
490 | * @param string $SQL |
||
491 | * @param WP_Query $wp_query |
||
492 | * @return string |
||
493 | * @throws \EE_Error |
||
494 | */ |
||
495 | public static function posts_where( $SQL = '', WP_Query $wp_query ) { |
||
506 | |||
507 | |||
508 | |||
509 | /** |
||
510 | * posts_where_sql_for_show_expired |
||
511 | * |
||
512 | * @param boolean $show_expired if TRUE, then displayed past events |
||
513 | * @return string |
||
514 | * @throws \EE_Error |
||
515 | */ |
||
516 | public static function posts_where_sql_for_show_expired( $show_expired = FALSE ) { |
||
521 | |||
522 | |||
523 | |||
524 | /** |
||
525 | * posts_where_sql_for_event_category_slug |
||
526 | * |
||
527 | * @param boolean $event_category_slug |
||
528 | * @return string |
||
529 | */ |
||
530 | public static function posts_where_sql_for_event_category_slug( $event_category_slug = NULL ) { |
||
537 | |||
538 | |||
539 | |||
540 | /** |
||
541 | * posts_where_sql_for_event_list_month |
||
542 | * |
||
543 | * @param boolean $month |
||
544 | * @return string |
||
545 | * @throws \EE_Error |
||
546 | */ |
||
547 | public static function posts_where_sql_for_event_list_month( $month = NULL ) { |
||
558 | |||
559 | |||
560 | |||
561 | /** |
||
562 | * posts_orderby |
||
563 | * |
||
564 | * @param string $SQL |
||
565 | * @param WP_Query $wp_query |
||
566 | * @return string |
||
567 | * @throws \EE_Error |
||
568 | */ |
||
569 | public static function posts_orderby( $SQL = '', WP_Query $wp_query ) { |
||
578 | |||
579 | |||
580 | |||
581 | /** |
||
582 | * posts_orderby_sql |
||
583 | * possible parameters: |
||
584 | * ID |
||
585 | * start_date |
||
586 | * end_date |
||
587 | * event_name |
||
588 | * category_slug |
||
589 | * ticket_start |
||
590 | * ticket_end |
||
591 | * venue_title |
||
592 | * city |
||
593 | * state |
||
594 | * **IMPORTANT** |
||
595 | * make sure to also send the $orderby_params array to the posts_join_for_orderby() method |
||
596 | * or else some of the table references below will result in MySQL errors |
||
597 | * |
||
598 | * @param array|bool $orderby_params |
||
599 | * @param string $sort |
||
600 | * @return string |
||
601 | * @throws \EE_Error |
||
602 | */ |
||
603 | public static function posts_orderby_sql( $orderby_params = array(), $sort = 'ASC' ) { |
||
666 | |||
667 | |||
668 | } |
||
669 | |||
674 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.