Complex classes like CptQueryModifier 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 CptQueryModifier, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class CptQueryModifier { |
||
20 | |||
21 | /** |
||
22 | * @var string $post_type |
||
23 | */ |
||
24 | protected $post_type = ''; |
||
25 | |||
26 | /** |
||
27 | * CPT details from \EE_Register_CPTs::get_CPTs() for specific post type |
||
28 | * |
||
29 | * @var array $cpt_details |
||
30 | */ |
||
31 | protected $cpt_details = array(); |
||
32 | |||
33 | /** |
||
34 | * @var \EE_Table_Base[] $model_tables |
||
35 | */ |
||
36 | protected $model_tables = array(); |
||
37 | |||
38 | /** |
||
39 | * @var array $taxonomies |
||
40 | */ |
||
41 | protected $taxonomies = array(); |
||
42 | |||
43 | /** |
||
44 | * meta table for the related CPT |
||
45 | * |
||
46 | * @var \EE_Secondary_Table $meta_table |
||
47 | */ |
||
48 | protected $meta_table; |
||
49 | |||
50 | /** |
||
51 | * EEM_CPT_Base model for the related CPT |
||
52 | * |
||
53 | * @var \EEM_CPT_Base $model |
||
54 | */ |
||
55 | protected $model; |
||
56 | |||
57 | /** |
||
58 | * @var \EE_Request_Handler $request |
||
59 | */ |
||
60 | protected $request; |
||
61 | |||
62 | /** |
||
63 | * @var \WP_Query $wp_query |
||
64 | */ |
||
65 | protected $wp_query; |
||
66 | |||
67 | |||
68 | |||
69 | /** |
||
70 | * CptQueryModifier constructor |
||
71 | * |
||
72 | * @param string $post_type |
||
73 | * @param array $cpt_details |
||
74 | * @param \WP_Query $WP_Query |
||
75 | * @param \EE_Request_Handler $request |
||
76 | */ |
||
77 | public function __construct( |
||
89 | |||
90 | |||
91 | |||
92 | /** |
||
93 | * @return string |
||
94 | */ |
||
95 | public function postType() { |
||
98 | |||
99 | |||
100 | |||
101 | /** |
||
102 | * @param string $post_type |
||
103 | */ |
||
104 | protected function setPostType( $post_type ) { |
||
107 | |||
108 | |||
109 | |||
110 | /** |
||
111 | * @return array |
||
112 | */ |
||
113 | public function cptDetails() { |
||
116 | |||
117 | |||
118 | |||
119 | /** |
||
120 | * @param array $cpt_details |
||
121 | */ |
||
122 | protected function setCptDetails( $cpt_details ) { |
||
125 | |||
126 | |||
127 | |||
128 | /** |
||
129 | * @return \EE_Table_Base[] |
||
130 | */ |
||
131 | public function modelTables() { |
||
134 | |||
135 | |||
136 | |||
137 | /** |
||
138 | * @param \EE_Table_Base[] $model_tables |
||
139 | */ |
||
140 | protected function setModelTables( $model_tables ) { |
||
143 | |||
144 | |||
145 | |||
146 | /** |
||
147 | * @return array |
||
148 | */ |
||
149 | public function taxonomies() { |
||
155 | |||
156 | |||
157 | |||
158 | /** |
||
159 | * @param array $taxonomies |
||
160 | */ |
||
161 | protected function setTaxonomies( array $taxonomies ) { |
||
164 | |||
165 | |||
166 | |||
167 | /** |
||
168 | * @return \EE_Secondary_Table |
||
169 | */ |
||
170 | public function metaTable() { |
||
173 | |||
174 | |||
175 | |||
176 | /** |
||
177 | * @param \EE_Secondary_Table $meta_table |
||
178 | */ |
||
179 | public function setMetaTable( \EE_Secondary_Table $meta_table ) { |
||
182 | |||
183 | |||
184 | |||
185 | /** |
||
186 | * @return \EEM_Base |
||
187 | */ |
||
188 | public function model() { |
||
191 | |||
192 | |||
193 | |||
194 | /** |
||
195 | * @param \EEM_Base $CPT_model |
||
196 | */ |
||
197 | protected function setModel( \EEM_Base $CPT_model ) { |
||
200 | |||
201 | |||
202 | |||
203 | /** |
||
204 | * @return \EE_Request_Handler |
||
205 | */ |
||
206 | public function request() { |
||
209 | |||
210 | |||
211 | |||
212 | /** |
||
213 | * @param \EE_Request_Handler $request |
||
214 | */ |
||
215 | protected function setRequest( \EE_Request_Handler $request ) { |
||
218 | |||
219 | |||
220 | |||
221 | /** |
||
222 | * @return \WP_Query |
||
223 | */ |
||
224 | public function WpQuery() { |
||
227 | |||
228 | |||
229 | |||
230 | /** |
||
231 | * @param \WP_Query $wp_query |
||
232 | */ |
||
233 | public function setWpQuery( \WP_Query $wp_query ) { |
||
236 | |||
237 | |||
238 | |||
239 | /** |
||
240 | * initializeTaxonomies |
||
241 | * |
||
242 | * @access protected |
||
243 | * @return void |
||
244 | */ |
||
245 | protected function initializeTaxonomies() { |
||
266 | |||
267 | |||
268 | |||
269 | protected function init() { |
||
302 | |||
303 | |||
304 | |||
305 | /** |
||
306 | * sets some basic query vars that pertain to the CPT |
||
307 | * |
||
308 | * @access protected |
||
309 | * @return void |
||
310 | */ |
||
311 | protected function setAdditionalCptDetails() { |
||
329 | |||
330 | |||
331 | |||
332 | /** |
||
333 | * Checks if we're on a EE-CPT archive-or-single page, and if we've never set the EE request var. |
||
334 | * If so, sets the 'ee' request variable |
||
335 | * so other parts of EE can know what CPT is getting queried. |
||
336 | * To Mike's knowledge, this must be called from during or after the pre_get_posts hook |
||
337 | * in order for is_archive() and is_single() methods to work properly. |
||
338 | * |
||
339 | * @return void |
||
340 | */ |
||
341 | public function setRequestVarsIfCpt() { |
||
355 | |||
356 | |||
357 | |||
358 | /** |
||
359 | * setupModelsAndTables |
||
360 | * |
||
361 | * @access protected |
||
362 | * @param string $model_name |
||
363 | * @throws \EE_Error |
||
364 | */ |
||
365 | protected function setupModelsAndTables( $model_name ) { |
||
389 | |||
390 | |||
391 | |||
392 | /** |
||
393 | * cptStrategyClass |
||
394 | * |
||
395 | * @access protected |
||
396 | * @param string $model_name |
||
397 | * @return string |
||
398 | */ |
||
399 | protected function cptStrategyClass( $model_name ) { |
||
415 | |||
416 | |||
417 | |||
418 | /** |
||
419 | * postsFields |
||
420 | * |
||
421 | * @access public |
||
422 | * @param $SQL |
||
423 | * @return string |
||
424 | */ |
||
425 | public function postsFields( $SQL ) { |
||
434 | |||
435 | |||
436 | |||
437 | /** |
||
438 | * postsJoin |
||
439 | * |
||
440 | * @access public |
||
441 | * @param $SQL |
||
442 | * @return string |
||
443 | */ |
||
444 | public function postsJoin( $SQL ) { |
||
462 | |||
463 | |||
464 | |||
465 | /** |
||
466 | * thePosts |
||
467 | * |
||
468 | * @access public |
||
469 | * @param \WP_Post[] $posts |
||
470 | * @return \WP_Post[] |
||
471 | */ |
||
472 | public function thePosts( $posts ) { |
||
485 | |||
486 | |||
487 | |||
488 | /** |
||
489 | * @param $url |
||
490 | * @param $ID |
||
491 | * @return string |
||
492 | */ |
||
493 | public function getEditPostLink( $url, $ID ) { |
||
494 | // need to make sure we only edit links if our cpt |
||
495 | global $post; |
||
496 | //notice if the cpt is registered with `show_ee_ui` set to false, we take that to mean that the WordPress core ui |
||
497 | //for interacting with the CPT is desired and there is no EE UI for interacting with the CPT in the admin. |
||
498 | if ( |
||
499 | ! $post instanceof \WP_Post |
||
500 | || $post->post_type !== $this->post_type |
||
501 | || ( |
||
502 | isset( $this->cpt_details['args']['show_ee_ui'] ) |
||
503 | && ! $this->cpt_details['args']['show_ee_ui'] |
||
504 | ) |
||
505 | ) { |
||
506 | return $url; |
||
507 | } |
||
508 | //k made it here so all is good. |
||
509 | return wp_nonce_url( |
||
510 | add_query_arg( |
||
511 | array( 'page' => $this->post_type, 'post' => $ID, 'action' => 'edit' ), |
||
512 | admin_url( 'admin.php' ) |
||
513 | ), |
||
514 | 'edit', |
||
515 | 'edit_nonce' |
||
516 | ); |
||
517 | } |
||
518 | |||
519 | |||
520 | |||
521 | /** |
||
522 | * Execute any template filters. |
||
523 | * This method is only called if in main query. |
||
524 | * |
||
525 | * @return void |
||
526 | */ |
||
527 | public function addTemplateFilters() { |
||
534 | |||
535 | |||
536 | |||
537 | /** |
||
538 | * Callback for single_template wp filter. |
||
539 | * This is used to load the set page_template for a single ee cpt if its set. If "default" then we load the normal hierarchy. |
||
540 | * |
||
541 | * @access public |
||
542 | * @param string $current_template Existing default template path derived for this page call. |
||
543 | * @return string the path to the full template file. |
||
544 | */ |
||
545 | public function singleCptTemplate( $current_template ) { |
||
560 | |||
561 | |||
562 | } |
||
563 | // End of file CptQueryModifier.php |
||
564 | // Location: /CptQueryModifier.php |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.