Complex classes like AdminPageFramework_Factory_Router 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 AdminPageFramework_Factory_Router, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | abstract class AdminPageFramework_Factory_Router { |
||
1 ignored issue
–
show
|
|||
24 | |||
25 | /** |
||
26 | * Stores the property object. |
||
27 | * |
||
28 | * @since 2.0.0 |
||
29 | * @access public The AdminPageFramework_Page_MetaBox class accesses it. |
||
30 | */ |
||
31 | public $oProp; |
||
32 | |||
33 | /** |
||
34 | * The object that provides the debug methods. |
||
35 | * |
||
36 | * @internal |
||
37 | * @access public |
||
38 | * @since 2.0.0 |
||
39 | * @since 3.1.0 Changed the scope to public from protected. |
||
40 | */ |
||
41 | public $oDebug; |
||
42 | /** |
||
43 | * Provides the utility methods. |
||
44 | * |
||
45 | * @internal |
||
46 | * @since 2.0.0 |
||
47 | * @since 3.1.0 Changed the scope to public from protected. |
||
48 | */ |
||
49 | public $oUtil; |
||
50 | /** |
||
51 | * Provides the methods for text messages of the framework. |
||
52 | * |
||
53 | * @since 2.0.0 |
||
54 | * @since 3.1.0 Changed the scope to public from protected. |
||
55 | * @access public |
||
56 | * @internal |
||
57 | */ |
||
58 | public $oMsg; |
||
59 | |||
60 | /** |
||
61 | * The form object that provides methods to handle form sections and fields. |
||
62 | * @internal |
||
63 | * @since 3.0.0 |
||
64 | * @since 3.5.2 Changed the scope to public from protected as the widget class needs to initialize this object. |
||
65 | */ |
||
66 | public $oForm; |
||
67 | |||
68 | /** |
||
69 | * Inserts page load information into the footer area of the page. |
||
70 | * |
||
71 | */ |
||
72 | protected $oPageLoadInfo; |
||
73 | |||
74 | /** |
||
75 | * Provides the methods to insert head tag elements. |
||
76 | * |
||
77 | * @since 3.3.0 Changed the name from $oHeadTag as it has become to deal with footer elements. |
||
78 | */ |
||
79 | protected $oResource; |
||
80 | |||
81 | /** |
||
82 | * Provides the methods to insert head tag elements. |
||
83 | * @deprecated |
||
84 | */ |
||
85 | protected $oHeadTag; |
||
86 | |||
87 | /** |
||
88 | * Provides methods to manipulate contextual help pane. |
||
89 | */ |
||
90 | protected $oHelpPane; |
||
91 | |||
92 | /** |
||
93 | * Provides the methods for creating HTML link elements. |
||
94 | * |
||
95 | */ |
||
96 | protected $oLink; |
||
97 | |||
98 | /** |
||
99 | * Stores sub-class names. |
||
100 | * |
||
101 | * Used in the __get() method to check whether a method with the name of the property should be called or not. |
||
102 | * |
||
103 | * @since 3.5.3 |
||
104 | */ |
||
105 | protected $_aSubClassNames = array( |
||
106 | 'oDebug', |
||
107 | 'oUtil', |
||
108 | 'oMsg', |
||
109 | 'oForm', |
||
110 | 'oPageLoadInfo', |
||
111 | 'oResource', |
||
112 | 'oHelpPane', |
||
113 | 'oLink', |
||
114 | ); |
||
115 | |||
116 | /** |
||
117 | * Sets up built-in objects. |
||
118 | */ |
||
119 | public function __construct( $oProp ) { |
||
148 | |||
149 | /** |
||
150 | * Determines whether the class component classes should be instantiated or not. |
||
151 | * |
||
152 | * @internal |
||
153 | * @callback action current_screen |
||
154 | * @return void |
||
155 | */ |
||
156 | public function _replyToLoadComponents( /* $oScreen */ ) { |
||
175 | /** |
||
176 | * Sets sub-class objects. |
||
177 | * |
||
178 | * This method forces the overload method __get() to be triggered if those sub-class objects |
||
179 | * are not set. |
||
180 | * |
||
181 | * @since 3.5.3 |
||
182 | * @internal |
||
183 | * @return void |
||
184 | */ |
||
185 | private function _setSubClasses() { |
||
191 | |||
192 | /** |
||
193 | * Determines whether the class object is instantiatable in the current page. |
||
194 | * |
||
195 | * This method should be redefined in the extended class. |
||
196 | * |
||
197 | * @since 3.1.0 |
||
198 | * @internal |
||
199 | */ |
||
200 | protected function _isInstantiatable() { |
||
203 | |||
204 | /** |
||
205 | * Determines whether the instantiated object and its producing elements belong to the loading page. |
||
206 | * |
||
207 | * This method should be redefined in the extended class. |
||
208 | * |
||
209 | * @since 3.0.3 |
||
210 | * @since 3.2.0 Changed the scope to public from protected as the head tag object will access it. |
||
211 | * @internal |
||
212 | */ |
||
213 | public function _isInThePage() { |
||
216 | |||
217 | /** |
||
218 | * Instantiate a form object based on the type. |
||
219 | * |
||
220 | * @since 3.1.0 |
||
221 | * @internal |
||
222 | * @return object|null |
||
223 | * @deprecated DEVVER |
||
224 | */ |
||
225 | protected function _getFormInstance( $oProp ) { |
||
235 | |||
236 | /** |
||
237 | * Stores class names by fields type for help pane objects. |
||
238 | * @since 3.5.3 |
||
239 | */ |
||
240 | protected $_aResourceClassNameMap = array( |
||
241 | 'admin_page' => 'AdminPageFramework_Resource_Page', |
||
242 | 'network_admin_page' => 'AdminPageFramework_Resource_Page', |
||
243 | 'post_meta_box' => 'AdminPageFramework_Resource_MetaBox', |
||
244 | 'page_meta_box' => 'AdminPageFramework_Resource_MetaBox_Page', |
||
245 | 'post_type' => 'AdminPageFramework_Resource_PostType', |
||
246 | 'taxonomy_field' => 'AdminPageFramework_Resource_TaxonomyField', |
||
247 | 'widget' => 'AdminPageFramework_Resource_Widget', |
||
248 | 'user_meta' => 'AdminPageFramework_Resource_UserMeta', |
||
249 | ); |
||
250 | /** |
||
251 | * Instantiate a resource handler object based on the type. |
||
252 | * |
||
253 | * @since 3.0.4 |
||
254 | * @internal |
||
255 | */ |
||
256 | protected function _getResourceInstance( $oProp ) { |
||
259 | |||
260 | /** |
||
261 | * Stores class names by fields type for help pane objects. |
||
262 | * @since 3.5.3 |
||
263 | */ |
||
264 | protected $_aHelpPaneClassNameMap = array( |
||
265 | 'admin_page' => 'AdminPageFramework_HelpPane_Page', |
||
266 | 'network_admin_page' => 'AdminPageFramework_HelpPane_Page', |
||
267 | 'post_meta_box' => 'AdminPageFramework_HelpPane_MetaBox', |
||
268 | 'page_meta_box' => 'AdminPageFramework_HelpPane_MetaBox_Page', |
||
269 | 'post_type' => null, // no help pane class for the post type factory class. |
||
270 | 'taxonomy_field' => 'AdminPageFramework_HelpPane_TaxonomyField', |
||
271 | 'widget' => 'AdminPageFramework_HelpPane_Widget', |
||
272 | 'user_meta' => 'AdminPageFramework_HelpPane_UserMeta', |
||
273 | ); |
||
274 | /** |
||
275 | * Instantiates a help pane object based on the type. |
||
276 | * |
||
277 | * @since 3.0.4 |
||
278 | * @internal |
||
279 | */ |
||
280 | protected function _getHelpPaneInstance( $oProp ) { |
||
283 | |||
284 | /** |
||
285 | * Stores class names by fields type for link objects. |
||
286 | * @since 3.5.3 |
||
287 | */ |
||
288 | protected $_aLinkClassNameMap = array( |
||
289 | 'admin_page' => 'AdminPageFramework_Link_Page', |
||
290 | 'network_admin_page' => 'AdminPageFramework_Link_NetworkAdmin', |
||
291 | 'post_meta_box' => null, |
||
292 | 'page_meta_box' => null, |
||
293 | 'post_type' => 'AdminPageFramework_Link_PostType', |
||
294 | 'taxonomy_field' => null, |
||
295 | 'widget' => null, |
||
296 | 'user_meta' => null, |
||
297 | ); |
||
298 | /** |
||
299 | * Instantiates a link object based on the type. |
||
300 | * |
||
301 | * @since 3.0.4 |
||
302 | * @internal |
||
303 | */ |
||
304 | protected function _getLinkInstancce( $oProp, $oMsg ) { |
||
307 | |||
308 | /** |
||
309 | * Stores class names by fields type for page load objects. |
||
310 | * @since 3.5.3 |
||
311 | */ |
||
312 | protected $_aPageLoadClassNameMap = array( |
||
313 | 'admin_page' => 'AdminPageFramework_PageLoadInfo_Page', |
||
314 | 'network_admin_page' => 'AdminPageFramework_PageLoadInfo_NetworkAdminPage', |
||
315 | 'post_meta_box' => null, |
||
316 | 'page_meta_box' => null, |
||
317 | 'post_type' => 'AdminPageFramework_PageLoadInfo_PostType', |
||
318 | 'taxonomy_field' => null, |
||
319 | 'widget' => null, |
||
320 | 'user_meta' => null, |
||
321 | ); |
||
322 | /** |
||
323 | * Instantiates a page load object based on the type. |
||
324 | * |
||
325 | * @since 3.0.4 |
||
326 | * @internal |
||
327 | */ |
||
328 | protected function _getPageLoadInfoInstance( $oProp, $oMsg ) { |
||
337 | |||
338 | /** |
||
339 | * Returns a class object instance by the given map array and the key, plus one or two arguments. |
||
340 | * |
||
341 | * @remark There is a limitation that only can accept up to 3 parameters at the moment. |
||
342 | * @internal |
||
343 | * @since 3.5.3 |
||
344 | * @return null|object |
||
345 | */ |
||
346 | private function _getInstanceByMap( /* array $aClassNameMap, $sKey, $mParam1, $mParam2, $mParam3 */ ) { |
||
373 | /**#@+ |
||
374 | * @internal |
||
375 | * @return object |
||
376 | */ |
||
377 | /** |
||
378 | * Instantiate a class with zero parameter. |
||
379 | * @since 3.5.3 |
||
380 | */ |
||
381 | private function _replyToGetClassInstanceByArgumentOf0( $sClassName ) { |
||
384 | /** |
||
385 | * Instantiate a class with one parameter. |
||
386 | * @since 3.5.3 |
||
387 | */ |
||
388 | private function _replyToGetClassInstanceByArgumentOf1( $sClassName, $mArg ) { |
||
391 | /** |
||
392 | * Instantiate a class with two parameters. |
||
393 | * @since 3.5.3 |
||
394 | */ |
||
395 | private function _replyToGetClassInstanceByArgumentOf2( $sClassName, $mArg1, $mArg2 ) { |
||
398 | /** |
||
399 | * Instantiate a class with two parameters. |
||
400 | * @since 3.5.3 |
||
401 | */ |
||
402 | private function _replyToGetClassInstanceByArgumentOf3( $sClassName, $mArg1, $mArg2, $mArg3 ) { |
||
405 | /**#@-*/ |
||
406 | |||
407 | /** |
||
408 | * Responds to a request of an undefined property. |
||
409 | * |
||
410 | * This is used to instantiate classes only when necessary, rather than instantiating them all at once. |
||
411 | * |
||
412 | * @internal |
||
413 | */ |
||
414 | public function __get( $sPropertyName ) { |
||
430 | /**#@+ |
||
431 | * @internal |
||
432 | * @return object |
||
433 | * @callback function call_user_func |
||
434 | */ |
||
435 | /** |
||
436 | * Sets and returns the `oUtil` property. |
||
437 | * @since 3.5.3 |
||
438 | */ |
||
439 | public function _replyTpSetAndGetInstance_oUtil() { |
||
443 | /** |
||
444 | * Sets and returns the `oDebug` property. |
||
445 | * @since 3.5.3 |
||
446 | */ |
||
447 | public function _replyTpSetAndGetInstance_oDebug() { |
||
451 | /** |
||
452 | * Sets and returns the `oMsg` property. |
||
453 | * @since 3.5.3 |
||
454 | */ |
||
455 | public function _replyTpSetAndGetInstance_oMsg() { |
||
459 | /** |
||
460 | * Sets and returns the `oForm` property. |
||
461 | * @since 3.5.3 |
||
462 | */ |
||
463 | public function _replyTpSetAndGetInstance_oForm() { |
||
467 | /** |
||
468 | * Sets and returns the `oResouce` property. |
||
469 | * @since 3.5.3 |
||
470 | */ |
||
471 | public function _replyTpSetAndGetInstance_oResource() { |
||
475 | /** |
||
476 | * Sets and returns the `oHelpPane` property. |
||
477 | * @since 3.5.3 |
||
478 | */ |
||
479 | public function _replyTpSetAndGetInstance_oHelpPane() { |
||
483 | /** |
||
484 | * Sets and returns the `oLink` property. |
||
485 | * @since 3.5.3 |
||
486 | */ |
||
487 | public function _replyTpSetAndGetInstance_oLink() { |
||
491 | /** |
||
492 | * Sets and returns the `oPageLoadInfo` property. |
||
493 | * @since 3.5.3 |
||
494 | */ |
||
495 | public function _replyTpSetAndGetInstance_oPageLoadInfo() { |
||
499 | /**#@-*/ |
||
500 | |||
501 | /** |
||
502 | * Redirects dynamic function calls to the pre-defined internal method. |
||
503 | * |
||
504 | * @internal |
||
505 | */ |
||
506 | public function __call( $sMethodName, $aArguments=null ) { |
||
534 | /** |
||
535 | * Returns the first parameter value if the method name does not contain a backslash. |
||
536 | * If it contains a backslash, the user uses a name-spaced class name. In that case, |
||
537 | * the backslashes need to be converted to underscores to support valid PHP method names. |
||
538 | * |
||
539 | * @since DEVVER |
||
540 | */ |
||
541 | private function _getAutoCallback( $sMethodName, $aArguments ) { |
||
559 | |||
560 | /** |
||
561 | * @since DEVVER |
||
562 | * @return void |
||
563 | */ |
||
564 | private function _triggerUndefinedMethodWarning( $sMethodName ) { |
||
574 | |||
575 | |||
576 | |||
577 | /** |
||
578 | * Prevents the output from getting too long when the object is dumped. |
||
579 | * |
||
580 | * Field definition arrays contain the factory object reference and when the debug log method tries to dump it, the output gets too long. |
||
581 | * So shorten it here. |
||
582 | * |
||
583 | * @remark Called when the object is called as a string. |
||
584 | * @since 3.4.4 |
||
585 | */ |
||
586 | public function __toString() { |
||
589 | |||
590 | /** |
||
591 | * Deprecated methods. |
||
592 | */ |
||
593 | /** |
||
594 | * @remark This was not functional since 3.1.3 |
||
595 | * @deprecated 3.5.5 |
||
596 | */ |
||
597 | public function setFooterInfoRight() {} |
||
598 | /** |
||
599 | * @remark This was not functional since 3.1.3 |
||
600 | * @deprecated 3.5.5 |
||
601 | */ |
||
602 | public function setFooterInfoLeft() {} |
||
603 | |||
604 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.