Complex classes like AdminPageFramework_Resource_Base 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_Resource_Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 24 | abstract class AdminPageFramework_Resource_Base extends AdminPageFramework_WPUtility { |
||
|
2 ignored issues
–
show
|
|||
| 25 | |||
| 26 | /** |
||
| 27 | * Represents the structure of the array for enqueuing scripts and styles. |
||
| 28 | * |
||
| 29 | * @since 2.1.2 |
||
| 30 | * @since 2.1.5 Moved to the base class. |
||
| 31 | * @since 3.0.0 Moved from the property class. |
||
| 32 | * @since 3.3.0 Changed the name to `$_aStructure_EnqueuingResources` from `$_aStructure_EnqueuingScriptsAndStyles`. |
||
| 33 | * @internal |
||
| 34 | */ |
||
| 35 | protected static $_aStructure_EnqueuingResources = array( |
||
|
1 ignored issue
–
show
|
|||
| 36 | |||
| 37 | /* The system internal keys. */ |
||
| 38 | 'sSRC' => null, |
||
| 39 | 'aPostTypes' => array(), // for meta box class |
||
| 40 | 'sPageSlug' => null, |
||
| 41 | 'sTabSlug' => null, |
||
| 42 | 'sType' => null, // script or style |
||
| 43 | |||
| 44 | /* The below keys are for users. */ |
||
| 45 | 'handle_id' => null, |
||
| 46 | 'dependencies' => array(), |
||
| 47 | 'version' => false, // although the type should be string, the wp_enqueue_...() functions want false as the default value. |
||
| 48 | 'translation' => array(), // only for scripts |
||
| 49 | 'in_footer' => false, // only for scripts |
||
| 50 | 'media' => 'all', // only for styles |
||
| 51 | 'attributes' => array(), // 3.3.0+ - the attribute array |
||
| 52 | |||
| 53 | ); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Stores the class selector used for the class-specific style. |
||
| 57 | * |
||
| 58 | * @since 3.2.0 |
||
| 59 | * @remark This value should be overridden in an extended class. |
||
| 60 | * @internal |
||
| 61 | */ |
||
| 62 | protected $_sClassSelector_Style = 'admin-page-framework-style'; |
||
|
1 ignored issue
–
show
|
|||
| 63 | |||
| 64 | /** |
||
| 65 | * Stores the class selector used to the class-specific script. |
||
| 66 | * |
||
| 67 | * @since 3.2.0 |
||
| 68 | * @remark This value should be overridden in an extended class. |
||
| 69 | * @internal |
||
| 70 | */ |
||
| 71 | protected $_sClassSelector_Script = 'admin-page-framework-script'; |
||
|
1 ignored issue
–
show
|
|||
| 72 | |||
| 73 | /** |
||
| 74 | * Stores hand IDs by resource url to look up handle id and add custom arguments. |
||
| 75 | * @since 3.3.0 |
||
| 76 | * @internal |
||
| 77 | */ |
||
| 78 | protected $_aHandleIDs = array(); |
||
|
1 ignored issue
–
show
|
|||
| 79 | |||
| 80 | /** |
||
| 81 | * A property object. |
||
| 82 | * |
||
| 83 | * @remark Set in the constructor. |
||
| 84 | */ |
||
| 85 | public $oProp; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * A utility object. |
||
| 89 | * |
||
| 90 | * @remark Set in the constructor. |
||
| 91 | * @deprecated 3.6.3 |
||
| 92 | * @remark kept for backward compatibility. |
||
| 93 | */ |
||
| 94 | public $oUtil; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Sets up properties and hooks. |
||
| 98 | * @internal |
||
| 99 | */ |
||
| 100 | function __construct( $oProp ) { |
||
| 139 | |||
| 140 | /* |
||
| 141 | * Methods that should be overridden in extended classes. |
||
| 142 | * @internal |
||
| 143 | */ |
||
| 144 | |||
| 145 | public function _forceToEnqueueStyle( $sSRC, $aCustomArgs=array() ) {} |
||
| 147 | |||
| 148 | /** |
||
| 149 | * A helper function for the _replyToEnqueueScripts() and the `_replyToEnqueueStyle()` methods. |
||
| 150 | * |
||
| 151 | * @since 2.1.5 |
||
| 152 | * @since DEVVER Fixed a typo in the method name. |
||
| 153 | * @internal |
||
| 154 | * @remark The widget fields type does not have conditions unlike the meta-box type that requires to check currently loaded post type. |
||
| 155 | * @remark This method should be redefined in the extended class. |
||
| 156 | */ |
||
| 157 | protected function _enqueueSRCByCondition( $aEnqueueItem ) { |
||
| 160 | |||
| 161 | /* |
||
| 162 | * Shared methods |
||
| 163 | */ |
||
| 164 | /** |
||
| 165 | * Checks the src url of the enqueued script/style to determine whether or not to set up a attribute modification callback. |
||
| 166 | * |
||
| 167 | * If it is one of the framework added item, the method sets up a hook to modify the url to add custom attributes. |
||
| 168 | * |
||
| 169 | * @since 3.3.0 |
||
| 170 | * @internal |
||
| 171 | * @callback action script_loader_src |
||
| 172 | * @callback action style_loader_src |
||
| 173 | */ |
||
| 174 | public function _replyToSetupArgumentCallback( $sSRC, $sHandleID ) { |
||
| 184 | /** |
||
| 185 | * Modifies the attributes of the enqueued script tag. |
||
| 186 | * |
||
| 187 | * @since 3.3.0 |
||
| 188 | * @internal |
||
| 189 | */ |
||
| 190 | public function _replyToModifyEnqueuedAttrbutes( $sSanitizedURL, $sOriginalURL, $sContext ) { |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Flags whether the common styles are loaded or not. |
||
| 220 | * |
||
| 221 | * @since 3.2.0 |
||
| 222 | * @internal |
||
| 223 | */ |
||
| 224 | static private $_bCommonStyleLoaded = false; |
||
|
1 ignored issue
–
show
|
|||
| 225 | |||
| 226 | /** |
||
| 227 | * Prints the inline stylesheet of the meta-box common CSS rules with the style tag. |
||
| 228 | * |
||
| 229 | * @internal |
||
| 230 | * @since 3.0.0 |
||
| 231 | * @since 3.2.0 Moved to the base class from the meta box class. |
||
| 232 | * @remark The meta box class may be instantiated multiple times so prevent echoing the same styles multiple times. |
||
| 233 | * @parameter string $sIDPrefix The id selector embedded in the script tag. |
||
| 234 | * @parameter string $sClassName The class name that identify the call group. This is important for the meta-box class because it can be instantiated multiple times in one particular page. |
||
| 235 | */ |
||
| 236 | protected function _printCommonStyles( $sIDPrefix, $sClassName ) { |
||
| 248 | /** |
||
| 249 | * @internal |
||
| 250 | * @since 3.5.7 |
||
| 251 | * @return string |
||
| 252 | */ |
||
| 253 | private function _getStyleTag( $oCaller, $sIDPrefix ) { |
||
| 274 | /** |
||
| 275 | * @internal |
||
| 276 | * @since 3.5.7 |
||
| 277 | * @return string |
||
| 278 | */ |
||
| 279 | private function _getIEStyleTag( $oCaller, $sIDPrefix ) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Flags whether the common styles are loaded or not. |
||
| 303 | * |
||
| 304 | * @since 3.2.0 |
||
| 305 | * @internal |
||
| 306 | */ |
||
| 307 | static private $_bCommonScriptLoaded = false; |
||
|
1 ignored issue
–
show
|
|||
| 308 | |||
| 309 | /** |
||
| 310 | * Prints the inline scripts of the meta-box common scripts. |
||
| 311 | * |
||
| 312 | * @internal |
||
| 313 | * @since 3.0.0 |
||
| 314 | * @since 3.2.0 Moved to the base class from the meta box class. |
||
| 315 | * @remark The meta box class may be instantiated multiple times so prevent echoing the same styles multiple times. |
||
| 316 | * @parametr string $sIDPrefix The id selector embedded in the script tag. |
||
| 317 | * @parametr string $sClassName The class name that identify the call group. This is important for the meta-box class because it can be instantiated multiple times in one particular page. |
||
| 318 | */ |
||
| 319 | protected function _printCommonScripts( $sIDPrefix, $sClassName ) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Prints the inline stylesheet of this class stored in this class property. |
||
| 345 | * |
||
| 346 | * @since 3.0.0 |
||
| 347 | * @since 3.2.0 Made the properties storing styles empty. Moved to the base class. |
||
| 348 | * @internal |
||
| 349 | * @return void |
||
| 350 | */ |
||
| 351 | protected function _printClassSpecificStyles( $sIDPrefix ) { |
||
| 363 | /** |
||
| 364 | * |
||
| 365 | * @internal |
||
| 366 | * @since 3.5.7 |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | private function _getClassSpecificStyleTag( $_oCaller, $sIDPrefix ) { |
||
| 389 | /** |
||
| 390 | * |
||
| 391 | * @internal |
||
| 392 | * @since 3.5.7 |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | private function _getClassSpecificIEStyleTag( $_oCaller, $sIDPrefix ) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Prints the inline scripts of this class stored in this class property. |
||
| 418 | * |
||
| 419 | * @since 3.0.0 |
||
| 420 | * @since 3.2.0 Made the property empty that stores scripts. Moved to the base class. |
||
| 421 | * @internal |
||
| 422 | */ |
||
| 423 | protected function _printClassSpecificScripts( $sIDPrefix ) { |
||
| 448 | |||
| 449 | |||
| 450 | /** |
||
| 451 | * Appends the CSS rules of the framework in the head tag. |
||
| 452 | * |
||
| 453 | * @since 2.0.0 |
||
| 454 | * @since 2.1.5 Moved from `AdminPageFramework_MetaBox`. Changed the name from `addAtyle()` to `replyToAddStyle()`. |
||
| 455 | * @callback action admin_head |
||
| 456 | * @internal |
||
| 457 | */ |
||
| 458 | public function _replyToAddStyle() { |
||
| 469 | /** |
||
| 470 | * Appends the JavaScript script of the framework in the head tag. |
||
| 471 | * |
||
| 472 | * @callback action admin_head |
||
| 473 | * @since 2.0.0 |
||
| 474 | * @since 2.1.5 Moved from AdminPageFramework_MetaBox. Changed the name from `addScript()` to `replyToAddScript()`. |
||
| 475 | * @since 3.2.0 Moved from AdminPageFramework_Resource_MetaBox. |
||
| 476 | * @internal |
||
| 477 | */ |
||
| 478 | public function _replyToAddScript() { |
||
| 489 | |||
| 490 | |||
| 491 | /** |
||
| 492 | * Performs actual enqueuing items. |
||
| 493 | * |
||
| 494 | * @since 2.1.2 |
||
| 495 | * @since 2.1.5 Moved from the main class. |
||
| 496 | * @internal |
||
| 497 | */ |
||
| 498 | protected function _enqueueSRC( $aEnqueueItem ) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Takes care of added enqueuing scripts by checking the currently loading page. |
||
| 529 | * |
||
| 530 | * @remark A callback for the admin_enqueue_scripts hook. |
||
| 531 | * @since 2.1.2 |
||
| 532 | * @since 2.1.5 Moved from the main class. Changed the name from `enqueueStylesCalback` to `replyToEnqueueStyles()`. |
||
| 533 | * @since 3.0.0 Changed the name to `_replyToEnqueueStyles()`. |
||
| 534 | * @since 3.2.0 Changed it unset the enqueued item so that the method can be called multiple times. |
||
| 535 | * @internal |
||
| 536 | */ |
||
| 537 | public function _replyToEnqueueStyles() { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Takes care of added enqueuing scripts by page slug and tab slug. |
||
| 546 | * |
||
| 547 | * @remark A callback for the admin_enqueue_scripts hook. |
||
| 548 | * @since 2.1.2 |
||
| 549 | * @since 2.1.5 Moved from the main class. Changed the name from `enqueueScriptsCallback` to `callbackEnqueueScripts()`. |
||
| 550 | * @since 3.0.0 Changed the name to `_replyToEnqueueScripts()`. |
||
| 551 | * @since 3.2.0 Changed it unset the enqueued item so that the method can be called multiple times. |
||
| 552 | * @internal |
||
| 553 | */ |
||
| 554 | public function _replyToEnqueueScripts() { |
||
| 560 | |||
| 561 | } |
||
|
1 ignored issue
–
show
|
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.