Complex classes like WordPoints_DB_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 WordPoints_DB_Query, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class WordPoints_DB_Query { |
||
20 | |||
21 | /** |
||
22 | * The name of the table this query class is for. |
||
23 | * |
||
24 | * This should be the full name of the table, including the prefix. You will |
||
25 | * therefore likely need to define it from inside your constructor. |
||
26 | * |
||
27 | * @since 1.0.0 |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $table_name; |
||
32 | |||
33 | /** |
||
34 | * The columns in the table being queried. |
||
35 | * |
||
36 | * The keys are the names of the columns. The values are arrays that support the |
||
37 | * following keys: |
||
38 | * |
||
39 | * - format (required) The format (%s, %d, or %f) to use when passing the values |
||
40 | * for this format to $wpdb->prepare(). |
||
41 | * - values (optional) An array of values that this column can have. Any values |
||
42 | * that aren't in this list will be discarded from a query. |
||
43 | * - unsigned (optional) Whether the value is unsigned. If this is true, values |
||
44 | * for this column will be rejected if they are not positive. |
||
45 | * - is_date (optional) Whether this is a DATETIME field. If so date queries will |
||
46 | * be supported. |
||
47 | * |
||
48 | * For each column in this array, the following query args are supported: |
||
49 | * |
||
50 | * - "{$column}" A single value that this column should have. |
||
51 | * - "{$column}__compare" How to compare the above value to the value in the DB. |
||
52 | * - "{$column}__in" An array of values that this column may have. |
||
53 | * - "{$column}__not_in" An array of values that this column may not have. |
||
54 | * |
||
55 | * Where {$column} is the name of the column. |
||
56 | * |
||
57 | * The "{$column}" query arg takes precedence over the "{$column}__in" and |
||
58 | * "{$column}__not_in" query args. |
||
59 | * |
||
60 | * However, if the column specifies that is_date is true, then the above are not |
||
61 | * supported, and the following are offered instead: |
||
62 | * |
||
63 | * - "{$column}_query" Arguments to pass to a WP_Date_Query. |
||
64 | * |
||
65 | * @since 1.0.0 |
||
66 | * |
||
67 | * @var array[] |
||
68 | */ |
||
69 | protected $columns = array(); |
||
70 | |||
71 | /** |
||
72 | * The slug of the meta type. |
||
73 | * |
||
74 | * If this is defined, the 'meta_query', 'meta_key', 'meta_value', |
||
75 | * 'meta_compare', and 'meta_type' args are supported, and will be passed to |
||
76 | * WP_Meta_Query. |
||
77 | * |
||
78 | * @since 1.0.0 |
||
79 | * |
||
80 | * @var string |
||
81 | */ |
||
82 | protected $meta_type; |
||
83 | |||
84 | /** |
||
85 | * The default values for the query args. |
||
86 | * |
||
87 | * You can override this entirely if needed, or just modify it in your |
||
88 | * constructor before calling parent::__construct(). |
||
89 | * |
||
90 | * @since 1.0.0 |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $defaults = array( |
||
95 | 'start' => 0, |
||
96 | 'order' => 'DESC', |
||
97 | ); |
||
98 | |||
99 | /** |
||
100 | * The query arguments. |
||
101 | * |
||
102 | * @since 1.0.0 |
||
103 | * |
||
104 | * @type array $args |
||
105 | */ |
||
106 | protected $args = array(); |
||
107 | |||
108 | /** |
||
109 | * Whether the query is ready for execution, or still needs to be prepared. |
||
110 | * |
||
111 | * @since 1.0.0 |
||
112 | * |
||
113 | * @type bool $is_query_ready |
||
114 | */ |
||
115 | protected $is_query_ready = false; |
||
116 | |||
117 | /** |
||
118 | * The SELECT statement for the query. |
||
119 | * |
||
120 | * @since 1.0.0 |
||
121 | * |
||
122 | * @type string $select |
||
123 | */ |
||
124 | protected $select; |
||
125 | |||
126 | /** |
||
127 | * The SELECT COUNT statement for a count query. |
||
128 | * |
||
129 | * @since 1.0.0 |
||
130 | * |
||
131 | * @type string $select_count |
||
132 | */ |
||
133 | protected $select_count = 'SELECT COUNT(*)'; |
||
134 | |||
135 | /** |
||
136 | * The JOIN query with the meta table. |
||
137 | * |
||
138 | * @since 1.0.0 |
||
139 | * |
||
140 | * @type string $meta_join |
||
141 | */ |
||
142 | protected $meta_join; |
||
143 | |||
144 | /** |
||
145 | * The WHERE clause for the query. |
||
146 | * |
||
147 | * @since 1.0.0 |
||
148 | * |
||
149 | * @type string $where |
||
150 | */ |
||
151 | protected $where; |
||
152 | |||
153 | /** |
||
154 | * The array of conditions for the WHERE clause. |
||
155 | * |
||
156 | * @since 1.0.0 |
||
157 | * |
||
158 | * @type array $wheres |
||
159 | */ |
||
160 | protected $wheres = array(); |
||
161 | |||
162 | /** |
||
163 | * The LIMIT clause for the query. |
||
164 | * |
||
165 | * @since 1.0.0 |
||
166 | * |
||
167 | * @type string $limit |
||
168 | */ |
||
169 | protected $limit; |
||
170 | |||
171 | /** |
||
172 | * The ORDER clause for the query. |
||
173 | * |
||
174 | * @since 1.0.0 |
||
175 | * |
||
176 | * @type string $order |
||
177 | */ |
||
178 | protected $order; |
||
179 | |||
180 | /** |
||
181 | * Holds the meta query object when a meta query is being performed. |
||
182 | * |
||
183 | * @since 1.0.0 |
||
184 | * |
||
185 | * @type WP_Meta_Query $meta_query |
||
186 | */ |
||
187 | protected $meta_query; |
||
188 | |||
189 | // |
||
190 | // Public Methods. |
||
191 | // |
||
192 | |||
193 | /** |
||
194 | * Construct the class. |
||
195 | * |
||
196 | * All of the arguments are expected *not* to be SQL escaped. |
||
197 | * |
||
198 | * @since 1.0.0 |
||
199 | * |
||
200 | * @see WP_Meta_Query for the proper arguments for 'meta_query', 'meta_key', 'meta_value', 'meta_compare', and 'meta_type'. |
||
201 | * |
||
202 | * @param array $args { |
||
203 | * The arguments for the query. |
||
204 | * |
||
205 | * @type string|array $fields Fields to include in the results. |
||
206 | * @type int $limit The maximum number of results to return. Default is null (no limit). |
||
207 | * @type int $start The start for the LIMIT clause. Default: 0. |
||
208 | * @type string $order_by The field to use to order the results. Default: 'date'. |
||
209 | * @type string $order The order for the query: ASC or DESC (default). |
||
210 | * @type string $meta_key See WP_Meta_Query. |
||
211 | * @type mixed $meta_value See WP_Meta_Query. |
||
212 | * @type string $meta_compare See WP_Meta_Query. |
||
213 | * @type string $meta_type See WP_Meta_Query. |
||
214 | * @type array $meta_query See WP_Meta_Query. |
||
215 | * } |
||
216 | */ |
||
217 | public function __construct( $args = array() ) { |
||
220 | |||
221 | /** |
||
222 | * Get a query arg. |
||
223 | * |
||
224 | * @since 1.0.0 |
||
225 | * |
||
226 | * @param string $arg The query arg whose value to retrieve. |
||
227 | * |
||
228 | * @return mixed|null The query arg's value, or null if it isn't set. |
||
229 | */ |
||
230 | public function get_arg( $arg ) { |
||
238 | |||
239 | /** |
||
240 | * Set arguments for the query. |
||
241 | * |
||
242 | * All of the arguments supported by the constructor may be passed in here, and |
||
243 | * will be merged into the array of existing args. |
||
244 | * |
||
245 | * @since 1.0.0 |
||
246 | * |
||
247 | * @param array $args A list of arguments to set and their values. |
||
248 | */ |
||
249 | public function set_args( array $args ) { |
||
255 | |||
256 | /** |
||
257 | * Count the number of results. |
||
258 | * |
||
259 | * When used with a query that contains a LIMIT clause, this method currently |
||
260 | * returns the count of the query ignoring the LIMIT, as would be the case with |
||
261 | * any similar query. However, this behaviour is not hardened and should not be |
||
262 | * relied upon. Make inquiry before assuming the constancy of this behaviour. |
||
263 | * |
||
264 | * @since 1.0.0 |
||
265 | * |
||
266 | * @return int The number of results. |
||
267 | */ |
||
268 | public function count() { |
||
276 | |||
277 | /** |
||
278 | * Get the results for the query. |
||
279 | * |
||
280 | * @since 1.0.0 |
||
281 | * |
||
282 | * @param string $method The method to use. Options are 'results', 'row', 'col', |
||
283 | * and 'var'. |
||
284 | * |
||
285 | * @return mixed The results of the query, or false on failure. |
||
286 | */ |
||
287 | public function get( $method = 'results' ) { |
||
304 | |||
305 | /** |
||
306 | * Get the SQL for the query. |
||
307 | * |
||
308 | * This function can return the SQL for a SELECT or SELECT COUNT query. To |
||
309 | * specify which one to return, set the $select_type parameter. Defaults to |
||
310 | * SELECT. |
||
311 | * |
||
312 | * This function is public for debugging purposes. |
||
313 | * |
||
314 | * @since 1.0.0 |
||
315 | * |
||
316 | * @param string $select_type The type of query, SELECT, or SELECT COUNT. |
||
317 | * |
||
318 | * @return string The SQL for the query. |
||
319 | */ |
||
320 | public function get_sql( $select_type = 'SELECT' ) { |
||
335 | |||
336 | // |
||
337 | // Filter Methods. |
||
338 | // |
||
339 | |||
340 | /** |
||
341 | * Filter date query valid columns for WP_Date_Query. |
||
342 | * |
||
343 | * @since 1.0.0 |
||
344 | * |
||
345 | * @WordPress\filter date_query_valid_columns Added and subsequently removed by |
||
346 | * self::prepare_date_where(). |
||
347 | * |
||
348 | * @param string[] $valid_columns The names of the valid columns for date queries. |
||
349 | * |
||
350 | * @return string[] The valid columns. |
||
351 | */ |
||
352 | public function date_query_valid_columns_filter( $valid_columns ) { |
||
363 | |||
364 | // |
||
365 | // Protected Methods. |
||
366 | // |
||
367 | |||
368 | /** |
||
369 | * Prepare the query. |
||
370 | * |
||
371 | * @since 1.0.0 |
||
372 | */ |
||
373 | protected function prepare_query() { |
||
385 | |||
386 | /** |
||
387 | * Prepare the select statement. |
||
388 | * |
||
389 | * @since 1.0.0 |
||
390 | */ |
||
391 | protected function prepare_select() { |
||
416 | |||
417 | /** |
||
418 | * Validates a value against an array of sanitizing functions. |
||
419 | * |
||
420 | * @since 1.0.0 |
||
421 | * |
||
422 | * @param mixed $value The value to validate. |
||
423 | * @param callable[] $validators The validators to validate it against. |
||
424 | * |
||
425 | * @return mixed The validated value, or false if invalid. |
||
426 | */ |
||
427 | protected function validate_value( $value, $validators ) { |
||
440 | |||
441 | /** |
||
442 | * Validates an array of values against an array of sanitizing functions. |
||
443 | * |
||
444 | * @since 1.0.0 |
||
445 | * |
||
446 | * @param array $values The values to validate. |
||
447 | * @param callable[] $validators The validators to validate each value against. |
||
448 | * |
||
449 | * @return array The validated values, with any invalid ones removed. |
||
450 | */ |
||
451 | protected function validate_values( $values, $validators ) { |
||
464 | |||
465 | /** |
||
466 | * Validate an unsigned column. |
||
467 | * |
||
468 | * The value must be positive, zero-inclusive. We can't just use |
||
469 | * wordpoints_posint() because it is zero exclusive. |
||
470 | * |
||
471 | * @since 1.0.0 |
||
472 | * |
||
473 | * @param mixed $value The value to validate. |
||
474 | * |
||
475 | * @return int|false The validated value or false. |
||
476 | */ |
||
477 | protected function validate_unsigned_column( $value ) { |
||
485 | |||
486 | /** |
||
487 | * Get an array of validating/sanitizing functions for the values of a column. |
||
488 | * |
||
489 | * @since 1.0.0 |
||
490 | * |
||
491 | * @param array $data The data for the column. |
||
492 | * |
||
493 | * @return callable[] The validation functions. |
||
494 | */ |
||
495 | protected function get_validators_for_column( $data ) { |
||
510 | |||
511 | /** |
||
512 | * Prepare the conditions for the WHERE clause for a column. |
||
513 | * |
||
514 | * @since 1.0.0 |
||
515 | * |
||
516 | * @param string $column The column name. |
||
517 | * @param array $data The column data. |
||
518 | */ |
||
519 | protected function prepare_column_where( $column, $data ) { |
||
530 | |||
531 | /** |
||
532 | * Prepare a single-value condition for the WHERE clause for a column. |
||
533 | * |
||
534 | * @since 1.0.0 |
||
535 | * |
||
536 | * @param string $column The name of the column |
||
537 | * @param array $data The column data. |
||
538 | */ |
||
539 | protected function prepare_column( $column, $data ) { |
||
568 | |||
569 | /** |
||
570 | * Get the comparator for a column. |
||
571 | * |
||
572 | * @since 1.0.0 |
||
573 | * |
||
574 | * @param string $column The column name. |
||
575 | * @param array $data The column data. |
||
576 | * |
||
577 | * @return string The comparator for the column. |
||
578 | */ |
||
579 | protected function get_comparator_for_column( $column, $data ) { |
||
600 | |||
601 | /** |
||
602 | * Prepare the IN or NOT IN conditions for a column. |
||
603 | * |
||
604 | * @since 1.0.0 |
||
605 | * |
||
606 | * @param string $column The name of the column. |
||
607 | * @param array $data The column data. |
||
608 | * @param string $type The type of IN clause, IN or NOT IN. |
||
609 | */ |
||
610 | protected function prepare_column__in( $column, $data, $type = 'IN' ) { |
||
643 | |||
644 | /** |
||
645 | * Prepare the WHERE clause for the query. |
||
646 | * |
||
647 | * @since 1.0.0 |
||
648 | */ |
||
649 | protected function prepare_where() { |
||
668 | |||
669 | /** |
||
670 | * Prepare the LIMIT clause for the query. |
||
671 | * |
||
672 | * @since 1.0.0 |
||
673 | */ |
||
674 | protected function prepare_limit() { |
||
707 | |||
708 | /** |
||
709 | * Prepare the ORDER BY clause for the query. |
||
710 | * |
||
711 | * @since 1.0.0 |
||
712 | */ |
||
713 | protected function prepare_order_by() { |
||
758 | |||
759 | /** |
||
760 | * Prepare the date query for a column. |
||
761 | * |
||
762 | * @since 1.0.0 |
||
763 | * |
||
764 | * @param string $column The name of the column. |
||
765 | */ |
||
766 | protected function prepare_date_where( $column ) { |
||
786 | |||
787 | /** |
||
788 | * Prepare the meta query. |
||
789 | * |
||
790 | * @since 1.0.0 |
||
791 | */ |
||
792 | protected function prepare_meta_where() { |
||
829 | } |
||
830 | |||
832 |