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 ) { |
||
144 | |||
145 | /** |
||
146 | * Determines whether the class component classes should be instantiated or not. |
||
147 | * |
||
148 | * @internal |
||
149 | * @callback action current_screen |
||
150 | * @return void |
||
151 | */ |
||
152 | public function _replyToLoadComponents( /* $oScreen */ ) { |
||
171 | /** |
||
172 | * Sets sub-classes. |
||
173 | * |
||
174 | * This method forces the overload method __get() to be triggered if those sub-class objects |
||
175 | * are not set. |
||
176 | * |
||
177 | * @since 3.5.3 |
||
178 | * @internal |
||
179 | * @return void |
||
180 | */ |
||
181 | private function _setSubClasses() { |
||
187 | |||
188 | /** |
||
189 | * Determines whether the class object is instantiatable in the current page. |
||
190 | * |
||
191 | * This method should be redefined in the extended class. |
||
192 | * |
||
193 | * @since 3.1.0 |
||
194 | * @internal |
||
195 | */ |
||
196 | protected function _isInstantiatable() { return true; } |
||
197 | |||
198 | /** |
||
199 | * Determines whether the instantiated object and its producing elements belong to the loading page. |
||
200 | * |
||
201 | * This method should be redefined in the extended class. |
||
202 | * |
||
203 | * @since 3.0.3 |
||
204 | * @since 3.2.0 Changed the scope to public from protected as the head tag object will access it. |
||
205 | * @internal |
||
206 | */ |
||
207 | public function _isInThePage() { return true; } |
||
208 | |||
209 | /** |
||
210 | * Stores class names by fields type for form element objects. |
||
211 | * @since 3.5.3 |
||
212 | */ |
||
213 | protected $_aFormElementClassNameMap = array( |
||
214 | 'page' => 'AdminPageFramework_FormDefinition_Page', |
||
215 | 'network_admin_page' => 'AdminPageFramework_FormDefinition_Page', |
||
216 | 'post_meta_box' => 'AdminPageFramework_FormDefinition_Meta', |
||
217 | 'page_meta_box' => 'AdminPageFramework_FormDefinition', |
||
218 | 'post_type' => 'AdminPageFramework_FormDefinition', |
||
219 | 'taxonomy' => 'AdminPageFramework_FormDefinition', |
||
220 | 'widget' => 'AdminPageFramework_FormDefinition', |
||
221 | 'user_meta' => 'AdminPageFramework_FormDefinition_Meta', |
||
222 | ); |
||
223 | /** |
||
224 | * Instantiate a form object based on the type. |
||
225 | * |
||
226 | * @since 3.1.0 |
||
227 | * @internal |
||
228 | * @return object|null |
||
229 | */ |
||
230 | protected function _getFormInstance( $oProp ) { |
||
250 | |||
251 | /** |
||
252 | * Stores class names by fields type for help pane objects. |
||
253 | * @since 3.5.3 |
||
254 | */ |
||
255 | protected $_aResourceClassNameMap = array( |
||
256 | 'page' => 'AdminPageFramework_Resource_Page', |
||
257 | 'network_admin_page' => 'AdminPageFramework_Resource_Page', |
||
258 | 'post_meta_box' => 'AdminPageFramework_Resource_MetaBox', |
||
259 | 'page_meta_box' => 'AdminPageFramework_Resource_MetaBox_Page', |
||
260 | 'post_type' => 'AdminPageFramework_Resource_PostType', |
||
261 | 'taxonomy' => 'AdminPageFramework_Resource_TaxonomyField', |
||
262 | 'widget' => 'AdminPageFramework_Resource_Widget', |
||
263 | 'user_meta' => 'AdminPageFramework_Resource_UserMeta', |
||
264 | ); |
||
265 | /** |
||
266 | * Instantiate a resource handler object based on the type. |
||
267 | * |
||
268 | * @since 3.0.4 |
||
269 | * @internal |
||
270 | */ |
||
271 | protected function _getResourceInstance( $oProp ) { |
||
274 | |||
275 | /** |
||
276 | * Stores class names by fields type for help pane objects. |
||
277 | * @since 3.5.3 |
||
278 | */ |
||
279 | protected $_aHelpPaneClassNameMap = array( |
||
280 | 'page' => 'AdminPageFramework_HelpPane_Page', |
||
281 | 'network_admin_page' => 'AdminPageFramework_HelpPane_Page', |
||
282 | 'post_meta_box' => 'AdminPageFramework_HelpPane_MetaBox', |
||
283 | 'page_meta_box' => 'AdminPageFramework_HelpPane_MetaBox_Page', |
||
284 | 'post_type' => null, // no help pane class for the post type factory class. |
||
285 | 'taxonomy' => 'AdminPageFramework_HelpPane_TaxonomyField', |
||
286 | 'widget' => 'AdminPageFramework_HelpPane_Widget', |
||
287 | 'user_meta' => 'AdminPageFramework_HelpPane_UserMeta', |
||
288 | ); |
||
289 | /** |
||
290 | * Instantiates a help pane object based on the type. |
||
291 | * |
||
292 | * @since 3.0.4 |
||
293 | * @internal |
||
294 | */ |
||
295 | protected function _getHelpPaneInstance( $oProp ) { |
||
298 | |||
299 | /** |
||
300 | * Stores class names by fields type for link objects. |
||
301 | * @since 3.5.3 |
||
302 | */ |
||
303 | protected $_aLinkClassNameMap = array( |
||
304 | 'page' => 'AdminPageFramework_Link_Page', |
||
305 | 'network_admin_page' => 'AdminPageFramework_Link_NetworkAdmin', |
||
306 | 'post_meta_box' => null, |
||
307 | 'page_meta_box' => null, |
||
308 | 'post_type' => 'AdminPageFramework_Link_PostType', |
||
309 | 'taxonomy' => null, |
||
310 | 'widget' => null, |
||
311 | 'user_meta' => null, |
||
312 | ); |
||
313 | /** |
||
314 | * Instantiates a link object based on the type. |
||
315 | * |
||
316 | * @since 3.0.4 |
||
317 | * @internal |
||
318 | */ |
||
319 | protected function _getLinkInstancce( $oProp, $oMsg ) { |
||
322 | |||
323 | /** |
||
324 | * Stores class names by fields type for page load objects. |
||
325 | * @since 3.5.3 |
||
326 | */ |
||
327 | protected $_aPageLoadClassNameMap = array( |
||
328 | 'page' => 'AdminPageFramework_PageLoadInfo_Page', |
||
329 | 'network_admin_page' => 'AdminPageFramework_PageLoadInfo_NetworkAdminPage', |
||
330 | 'post_meta_box' => null, |
||
331 | 'page_meta_box' => null, |
||
332 | 'post_type' => 'AdminPageFramework_PageLoadInfo_PostType', |
||
333 | 'taxonomy' => null, |
||
334 | 'widget' => null, |
||
335 | 'user_meta' => null, |
||
336 | ); |
||
337 | /** |
||
338 | * Instantiates a page load object based on the type. |
||
339 | * |
||
340 | * @since 3.0.4 |
||
341 | * @internal |
||
342 | */ |
||
343 | protected function _getPageLoadInfoInstance( $oProp, $oMsg ) { |
||
352 | |||
353 | /** |
||
354 | * Returns a class object instance by the given map array and the key, plus one or two arguments. |
||
355 | * |
||
356 | * @remark There is a limitation that only can accept up to 3 parameters at the moment. |
||
357 | * @internal |
||
358 | * @since 3.5.3 |
||
359 | * @return null|object |
||
360 | */ |
||
361 | private function _getInstanceByMap( /* array $aClassNameMap, $sKey, $mParam1, $mParam2, $mParam3 */ ) { |
||
388 | /**#@+ |
||
389 | * @internal |
||
390 | * @return object |
||
391 | */ |
||
392 | /** |
||
393 | * Instantiate a class with zero parameter. |
||
394 | * @since 3.5.3 |
||
395 | */ |
||
396 | private function _replyToGetClassInstanceByArgumentOf0( $sClassName ) { |
||
399 | /** |
||
400 | * Instantiate a class with one parameter. |
||
401 | * @since 3.5.3 |
||
402 | */ |
||
403 | private function _replyToGetClassInstanceByArgumentOf1( $sClassName, $mArg ) { |
||
406 | /** |
||
407 | * Instantiate a class with two parameters. |
||
408 | * @since 3.5.3 |
||
409 | */ |
||
410 | private function _replyToGetClassInstanceByArgumentOf2( $sClassName, $mArg1, $mArg2 ) { |
||
413 | /** |
||
414 | * Instantiate a class with two parameters. |
||
415 | * @since 3.5.3 |
||
416 | */ |
||
417 | private function _replyToGetClassInstanceByArgumentOf3( $sClassName, $mArg1, $mArg2, $mArg3 ) { |
||
420 | /**#@-*/ |
||
421 | |||
422 | /** |
||
423 | * Responds to a request of an undefined property. |
||
424 | * |
||
425 | * This is used to instantiate classes only when necessary, rather than instantiating them all at once. |
||
426 | * |
||
427 | * @internal |
||
428 | */ |
||
429 | public function __get( $sPropertyName ) { |
||
445 | /**#@+ |
||
446 | * @internal |
||
447 | * @return object |
||
448 | * @callback function call_user_func |
||
449 | */ |
||
450 | /** |
||
451 | * Sets and returns the `oUtil` property. |
||
452 | * @since 3.5.3 |
||
453 | */ |
||
454 | public function _replyTpSetAndGetInstance_oUtil() { |
||
458 | /** |
||
459 | * Sets and returns the `oDebug` property. |
||
460 | * @since 3.5.3 |
||
461 | */ |
||
462 | public function _replyTpSetAndGetInstance_oDebug() { |
||
466 | /** |
||
467 | * Sets and returns the `oMsg` property. |
||
468 | * @since 3.5.3 |
||
469 | */ |
||
470 | public function _replyTpSetAndGetInstance_oMsg() { |
||
474 | /** |
||
475 | * Sets and returns the `oForm` property. |
||
476 | * @since 3.5.3 |
||
477 | */ |
||
478 | public function _replyTpSetAndGetInstance_oForm() { |
||
482 | /** |
||
483 | * Sets and returns the `oResouce` property. |
||
484 | * @since 3.5.3 |
||
485 | */ |
||
486 | public function _replyTpSetAndGetInstance_oResource() { |
||
490 | /** |
||
491 | * Sets and returns the `oHelpPane` property. |
||
492 | * @since 3.5.3 |
||
493 | */ |
||
494 | public function _replyTpSetAndGetInstance_oHelpPane() { |
||
498 | /** |
||
499 | * Sets and returns the `oLink` property. |
||
500 | * @since 3.5.3 |
||
501 | */ |
||
502 | public function _replyTpSetAndGetInstance_oLink() { |
||
506 | /** |
||
507 | * Sets and returns the `oPageLoadInfo` property. |
||
508 | * @since 3.5.3 |
||
509 | */ |
||
510 | public function _replyTpSetAndGetInstance_oPageLoadInfo() { |
||
514 | /**#@-*/ |
||
515 | |||
516 | /** |
||
517 | * Redirects dynamic function calls to the pre-defined internal method. |
||
518 | * |
||
519 | * @internal |
||
520 | */ |
||
521 | public function __call( $sMethodName, $aArguments=null ) { |
||
550 | /** |
||
551 | * Returns the first parameter value if the method name does not contain a backslash. |
||
552 | * If it contains a backslash, the user uses a name-spaced class name. In that case, |
||
553 | * the backslashes need to be converted to underscores to support valid PHP method names. |
||
554 | * |
||
555 | * @since 3.6.6 |
||
556 | */ |
||
557 | private function _getAutoCallback( $sMethodName, $aArguments ) { |
||
575 | |||
576 | /** |
||
577 | * @since 3.6.6 |
||
578 | * @return void |
||
579 | */ |
||
580 | private function _triggerUndefinedMethodWarning( $sMethodName ) { |
||
590 | |||
591 | |||
592 | /** |
||
593 | * Prevents the output from getting too long when the object is dumped. |
||
594 | * |
||
595 | * Field definition arrays contain the factory object reference and when the debug log method tries to dump it, the output gets too long. |
||
596 | * So shorten it here. |
||
597 | * |
||
598 | * @remark Called when the object is called as a string. |
||
599 | * @since 3.4.4 |
||
600 | */ |
||
601 | public function __toString() { |
||
604 | |||
605 | /** |
||
606 | * Deprecated methods. |
||
607 | */ |
||
608 | /** |
||
609 | * @remark This was not functional since 3.1.3 |
||
610 | * @deprecated 3.5.5 |
||
611 | */ |
||
612 | public function setFooterInfoRight() {} |
||
613 | /** |
||
614 | * @remark This was not functional since 3.1.3 |
||
615 | * @deprecated 3.5.5 |
||
616 | */ |
||
617 | public function setFooterInfoLeft() {} |
||
618 | |||
619 | } |
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.