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 |
||
| 28 | class CptQueryModifier |
||
| 29 | { |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string $post_type |
||
| 33 | */ |
||
| 34 | protected $post_type = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * CPT details from CustomPostTypeDefinitions for specific post type |
||
| 38 | * |
||
| 39 | * @var array $cpt_details |
||
| 40 | */ |
||
| 41 | protected $cpt_details = array(); |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var EE_Table_Base[] $model_tables |
||
| 45 | */ |
||
| 46 | protected $model_tables = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var array $taxonomies |
||
| 50 | */ |
||
| 51 | protected $taxonomies = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * meta table for the related CPT |
||
| 55 | * |
||
| 56 | * @var EE_Secondary_Table $meta_table |
||
| 57 | */ |
||
| 58 | protected $meta_table; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * EEM_CPT_Base model for the related CPT |
||
| 62 | * |
||
| 63 | * @var EEM_CPT_Base $model |
||
| 64 | */ |
||
| 65 | protected $model; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var EE_Request_Handler $request_handler |
||
| 69 | */ |
||
| 70 | protected $request_handler; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var WP_Query $wp_query |
||
| 74 | */ |
||
| 75 | protected $wp_query; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var LoaderInterface $loader |
||
| 79 | */ |
||
| 80 | protected $loader; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var RequestInterface $request |
||
| 84 | */ |
||
| 85 | protected $request; |
||
| 86 | |||
| 87 | |||
| 88 | /** |
||
| 89 | * CptQueryModifier constructor |
||
| 90 | * |
||
| 91 | * @param string $post_type |
||
| 92 | * @param array $cpt_details |
||
| 93 | * @param WP_Query $WP_Query |
||
| 94 | * @param EE_Request_Handler $request_handler |
||
| 95 | * @param RequestInterface $request |
||
| 96 | * @param LoaderInterface $loader |
||
| 97 | * @throws EE_Error |
||
| 98 | */ |
||
| 99 | public function __construct( |
||
| 115 | |||
| 116 | |||
| 117 | /** |
||
| 118 | * @return string |
||
| 119 | */ |
||
| 120 | public function postType() |
||
| 124 | |||
| 125 | |||
| 126 | /** |
||
| 127 | * @param string $post_type |
||
| 128 | */ |
||
| 129 | protected function setPostType($post_type) |
||
| 133 | |||
| 134 | |||
| 135 | /** |
||
| 136 | * @return array |
||
| 137 | */ |
||
| 138 | public function cptDetails() |
||
| 142 | |||
| 143 | |||
| 144 | /** |
||
| 145 | * @param array $cpt_details |
||
| 146 | */ |
||
| 147 | protected function setCptDetails($cpt_details) |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * @return EE_Table_Base[] |
||
| 155 | */ |
||
| 156 | public function modelTables() |
||
| 160 | |||
| 161 | |||
| 162 | /** |
||
| 163 | * @param EE_Table_Base[] $model_tables |
||
| 164 | */ |
||
| 165 | protected function setModelTables($model_tables) |
||
| 169 | |||
| 170 | |||
| 171 | /** |
||
| 172 | * @return array |
||
| 173 | * @throws InvalidArgumentException |
||
| 174 | * @throws InvalidDataTypeException |
||
| 175 | * @throws InvalidInterfaceException |
||
| 176 | */ |
||
| 177 | public function taxonomies() |
||
| 184 | |||
| 185 | |||
| 186 | /** |
||
| 187 | * @param array $taxonomies |
||
| 188 | */ |
||
| 189 | protected function setTaxonomies(array $taxonomies) |
||
| 193 | |||
| 194 | |||
| 195 | /** |
||
| 196 | * @return EE_Secondary_Table |
||
| 197 | */ |
||
| 198 | public function metaTable() |
||
| 202 | |||
| 203 | |||
| 204 | /** |
||
| 205 | * @param EE_Secondary_Table $meta_table |
||
| 206 | */ |
||
| 207 | public function setMetaTable(EE_Secondary_Table $meta_table) |
||
| 211 | |||
| 212 | |||
| 213 | /** |
||
| 214 | * @return EEM_Base |
||
| 215 | */ |
||
| 216 | public function model() |
||
| 220 | |||
| 221 | |||
| 222 | /** |
||
| 223 | * @param EEM_Base $CPT_model |
||
| 224 | */ |
||
| 225 | protected function setModel(EEM_Base $CPT_model) |
||
| 229 | |||
| 230 | |||
| 231 | /** |
||
| 232 | * @deprecated $VID:$ |
||
| 233 | * @return EE_Request_Handler |
||
| 234 | */ |
||
| 235 | public function request() |
||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | // phpcs:disable PSR1.Methods.CamelCapsMethodName.NotCamelCaps |
||
| 243 | |||
| 244 | |||
| 245 | /** |
||
| 246 | * @return WP_Query |
||
| 247 | */ |
||
| 248 | public function WpQuery() |
||
| 252 | // phpcs:enable |
||
| 253 | |||
| 254 | |||
| 255 | /** |
||
| 256 | * @param WP_Query $wp_query |
||
| 257 | */ |
||
| 258 | public function setWpQuery(WP_Query $wp_query) |
||
| 262 | |||
| 263 | |||
| 264 | /** |
||
| 265 | * @return void |
||
| 266 | * @throws InvalidDataTypeException |
||
| 267 | * @throws InvalidInterfaceException |
||
| 268 | * @throws InvalidArgumentException |
||
| 269 | */ |
||
| 270 | protected function initializeTaxonomies() |
||
| 297 | |||
| 298 | |||
| 299 | /** |
||
| 300 | * @since $VID:$ |
||
| 301 | * @throws EE_Error |
||
| 302 | */ |
||
| 303 | protected function init() |
||
| 337 | |||
| 338 | |||
| 339 | /** |
||
| 340 | * sets some basic query vars that pertain to the CPT |
||
| 341 | * |
||
| 342 | * @access protected |
||
| 343 | * @return void |
||
| 344 | */ |
||
| 345 | protected function setAdditionalCptDetails() |
||
| 363 | |||
| 364 | |||
| 365 | /** |
||
| 366 | * Checks if we're on a EE-CPT archive-or-single page, and if we've never set the EE request var. |
||
| 367 | * If so, sets the 'ee' request variable |
||
| 368 | * so other parts of EE can know what CPT is getting queried. |
||
| 369 | * To Mike's knowledge, this must be called from during or after the pre_get_posts hook |
||
| 370 | * in order for is_archive() and is_single() methods to work properly. |
||
| 371 | * |
||
| 372 | * @return void |
||
| 373 | */ |
||
| 374 | public function setRequestVarsIfCpt() |
||
| 389 | |||
| 390 | |||
| 391 | /** |
||
| 392 | * setupModelsAndTables |
||
| 393 | * |
||
| 394 | * @access protected |
||
| 395 | * @param string $model_name |
||
| 396 | * @throws EE_Error |
||
| 397 | */ |
||
| 398 | protected function setupModelsAndTables($model_name) |
||
| 426 | |||
| 427 | |||
| 428 | /** |
||
| 429 | * cptStrategyClass |
||
| 430 | * |
||
| 431 | * @access protected |
||
| 432 | * @param string $model_name |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | protected function cptStrategyClass($model_name) |
||
| 452 | |||
| 453 | |||
| 454 | /** |
||
| 455 | * postsFields |
||
| 456 | * |
||
| 457 | * @access public |
||
| 458 | * @param $SQL |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | public function postsFields($SQL) |
||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * postsJoin |
||
| 475 | * |
||
| 476 | * @access public |
||
| 477 | * @param $SQL |
||
| 478 | * @return string |
||
| 479 | */ |
||
| 480 | public function postsJoin($SQL) |
||
| 499 | |||
| 500 | |||
| 501 | /** |
||
| 502 | * thePosts |
||
| 503 | * |
||
| 504 | * @access public |
||
| 505 | * @param WP_Post[] $posts |
||
| 506 | * @return WP_Post[] |
||
| 507 | */ |
||
| 508 | public function thePosts($posts) |
||
| 522 | |||
| 523 | |||
| 524 | /** |
||
| 525 | * @param $url |
||
| 526 | * @param $ID |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | public function getEditPostLink($url, $ID) |
||
| 554 | |||
| 555 | |||
| 556 | /** |
||
| 557 | * Execute any template filters. |
||
| 558 | * This method is only called if in main query. |
||
| 559 | * |
||
| 560 | * @return void |
||
| 561 | */ |
||
| 562 | public function addTemplateFilters() |
||
| 570 | |||
| 571 | |||
| 572 | /** |
||
| 573 | * Callback for single_template wp filter. |
||
| 574 | * This is used to load the set page_template for a single ee cpt if its set. If "default" then we load the normal |
||
| 575 | * hierarchy. |
||
| 576 | * |
||
| 577 | * @access public |
||
| 578 | * @param string $current_template Existing default template path derived for this page call. |
||
| 579 | * @return string the path to the full template file. |
||
| 580 | */ |
||
| 581 | public function singleCptTemplate($current_template) |
||
| 596 | } |
||
| 597 |
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.