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_FrameworkUtility { |
||
| 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( |
||
| 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'; |
||
| 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'; |
||
| 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(); |
||
| 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 | * @var AdminPageFramework_WPUtility |
||
| 94 | */ |
||
| 95 | public $oUtil; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Sets up properties and hooks. |
||
| 99 | * @internal |
||
| 100 | */ |
||
| 101 | public function __construct( $oProp ) { |
||
| 140 | |||
| 141 | /* |
||
| 142 | * Methods that should be overridden in extended classes. |
||
| 143 | * @internal |
||
| 144 | */ |
||
| 145 | |||
| 146 | public function _forceToEnqueueStyle( $sSRC, $aCustomArgs=array() ) {} |
||
| 148 | |||
| 149 | /** |
||
| 150 | * A helper function for the _replyToEnqueueScripts() and the `_replyToEnqueueStyle()` methods. |
||
| 151 | * |
||
| 152 | * @since 2.1.5 |
||
| 153 | * @since 3.7.0 Fixed a typo in the method name. |
||
| 154 | * @internal |
||
| 155 | * @remark The widget fields type does not have conditions unlike the meta-box type that requires to check currently loaded post type. |
||
| 156 | * @remark This method should be redefined in the extended class. |
||
| 157 | */ |
||
| 158 | protected function _enqueueSRCByCondition( $aEnqueueItem ) { |
||
| 161 | |||
| 162 | /* |
||
| 163 | * Shared methods |
||
| 164 | */ |
||
| 165 | /** |
||
| 166 | * Checks the src url of the enqueued script/style to determine whether or not to set up a attribute modification callback. |
||
| 167 | * |
||
| 168 | * If it is one of the framework added item, the method sets up a hook to modify the url to add custom attributes. |
||
| 169 | * |
||
| 170 | * @since 3.3.0 |
||
| 171 | * @internal |
||
| 172 | * @callback action script_loader_src |
||
| 173 | * @callback action style_loader_src |
||
| 174 | */ |
||
| 175 | public function _replyToSetupArgumentCallback( $sSRC, $sHandleID ) { |
||
| 185 | /** |
||
| 186 | * Modifies the attributes of the enqueued script tag. |
||
| 187 | * |
||
| 188 | * @since 3.3.0 |
||
| 189 | * @internal |
||
| 190 | */ |
||
| 191 | public function _replyToModifyEnqueuedAttributes( $sSanitizedURL, $sOriginalURL, $sContext ) { |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Prints the inline stylesheet of the meta-box common CSS rules with the style tag. |
||
| 218 | * |
||
| 219 | * @internal |
||
| 220 | * @since 3.0.0 |
||
| 221 | * @since 3.2.0 Moved to the base class from the meta box class. |
||
| 222 | * @remark The meta box class may be instantiated multiple times so prevent echoing the same styles multiple times. |
||
| 223 | * @parameter string $sIDPrefix The id selector embedded in the script tag. |
||
| 224 | * @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. |
||
| 225 | */ |
||
| 226 | protected function _printCommonStyles( $sIDPrefix, $sClassName ) { |
||
| 236 | /** |
||
| 237 | * @internal |
||
| 238 | * @since 3.5.7 |
||
| 239 | * @since 3.8.22 Renamed from `_getStyleTag()`. |
||
| 240 | * @return string |
||
| 241 | */ |
||
| 242 | private function ___getCommonStyleTag( $oCaller, $sIDPrefix ) { |
||
| 262 | /** |
||
| 263 | * @internal |
||
| 264 | * @since 3.5.7 |
||
| 265 | * @since 3.8.22 Renamed from `_getIEStyleTag()`. |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | private function ___getCommonIEStyleTag( $oCaller, $sIDPrefix ) { |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Prints the inline scripts of the meta-box common scripts. |
||
| 290 | * |
||
| 291 | * @internal |
||
| 292 | * @since 3.0.0 |
||
| 293 | * @since 3.2.0 Moved to the base class from the meta box class. |
||
| 294 | * @remark The meta box class may be instantiated multiple times so prevent echoing the same styles multiple times. |
||
| 295 | * @parametr string $sIDPrefix The id selector embedded in the script tag. |
||
| 296 | * @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. |
||
| 297 | */ |
||
| 298 | protected function _printCommonScripts( $sIDPrefix, $sClassName ) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Prints the inline stylesheet of this class stored in this class property. |
||
| 326 | * |
||
| 327 | * @since 3.0.0 |
||
| 328 | * @since 3.2.0 Made the properties storing styles empty. Moved to the base class. |
||
| 329 | * @internal |
||
| 330 | * @return void |
||
| 331 | */ |
||
| 332 | protected function _printClassSpecificStyles( $sIDPrefix ) { |
||
| 344 | /** |
||
| 345 | * |
||
| 346 | * @internal |
||
| 347 | * @since 3.5.7 |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | private function _getClassSpecificStyleTag( $_oCaller, $sIDPrefix ) { |
||
| 371 | /** |
||
| 372 | * |
||
| 373 | * @internal |
||
| 374 | * @since 3.5.7 |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | private function _getClassSpecificIEStyleTag( $_oCaller, $sIDPrefix ) { |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Prints the inline scripts of this class stored in this class property. |
||
| 401 | * |
||
| 402 | * @since 3.0.0 |
||
| 403 | * @since 3.2.0 Made the property empty that stores scripts. Moved to the base class. |
||
| 404 | * @internal |
||
| 405 | */ |
||
| 406 | protected function _printClassSpecificScripts( $sIDPrefix ) { |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * Appends the CSS rules of the framework in the head tag. |
||
| 436 | * |
||
| 437 | * @since 2.0.0 |
||
| 438 | * @since 2.1.5 Moved from `AdminPageFramework_MetaBox`. Changed the name from `addAtyle()` to `replyToAddStyle()`. |
||
| 439 | * @callback action admin_head |
||
| 440 | * @internal |
||
| 441 | */ |
||
| 442 | public function _replyToAddStyle() { |
||
| 453 | /** |
||
| 454 | * Appends the JavaScript script of the framework in the head tag. |
||
| 455 | * |
||
| 456 | * @callback action admin_head |
||
| 457 | * @since 2.0.0 |
||
| 458 | * @since 2.1.5 Moved from AdminPageFramework_MetaBox. Changed the name from `addScript()` to `replyToAddScript()`. |
||
| 459 | * @since 3.2.0 Moved from AdminPageFramework_Resource_post_meta_box. |
||
| 460 | * @internal |
||
| 461 | */ |
||
| 462 | public function _replyToAddScript() { |
||
| 473 | |||
| 474 | |||
| 475 | /** |
||
| 476 | * Performs actual enqueuing items. |
||
| 477 | * |
||
| 478 | * @since 2.1.2 |
||
| 479 | * @since 2.1.5 Moved from the main class. |
||
| 480 | * @internal |
||
| 481 | */ |
||
| 482 | protected function _enqueueSRC( $aEnqueueItem ) { |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Takes care of added enqueuing scripts by checking the currently loading page. |
||
| 513 | * |
||
| 514 | * @remark A callback for the admin_enqueue_scripts hook. |
||
| 515 | * @since 2.1.2 |
||
| 516 | * @since 2.1.5 Moved from the main class. Changed the name from `enqueueStylesCalback` to `replyToEnqueueStyles()`. |
||
| 517 | * @since 3.0.0 Changed the name to `_replyToEnqueueStyles()`. |
||
| 518 | * @since 3.2.0 Changed it unset the enqueued item so that the method can be called multiple times. |
||
| 519 | * @internal |
||
| 520 | */ |
||
| 521 | public function _replyToEnqueueStyles() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Takes care of added enqueuing scripts by page slug and tab slug. |
||
| 530 | * |
||
| 531 | * @remark A callback for the admin_enqueue_scripts hook. |
||
| 532 | * @since 2.1.2 |
||
| 533 | * @since 2.1.5 Moved from the main class. Changed the name from `enqueueScriptsCallback` to `callbackEnqueueScripts()`. |
||
| 534 | * @since 3.0.0 Changed the name to `_replyToEnqueueScripts()`. |
||
| 535 | * @since 3.2.0 Changed it unset the enqueued item so that the method can be called multiple times. |
||
| 536 | * @internal |
||
| 537 | */ |
||
| 538 | public function _replyToEnqueueScripts() { |
||
| 544 | |||
| 545 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.