Complex classes like Manager 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 Manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Manager |
||
| 31 | { |
||
| 32 | use \Jaxon\Utils\Traits\Manager; |
||
| 33 | use \Jaxon\Utils\Traits\Config; |
||
| 34 | use \Jaxon\Utils\Traits\Cache; |
||
| 35 | use \Jaxon\Utils\Traits\Minifier; |
||
| 36 | use \Jaxon\Utils\Traits\Template; |
||
| 37 | use \Jaxon\Utils\Traits\Translator; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The response type. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | const RESPONSE_TYPE = 'JSON'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * All plugins, indexed by priority |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | private $aPlugins; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Request plugins, indexed by name |
||
| 55 | * |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private $aRequestPlugins; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Response plugins, indexed by name |
||
| 62 | * |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $aResponsePlugins; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Directories where Jaxon classes to be registered are found |
||
| 69 | * |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | private $aClassDirs; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * True if the Composer autoload is enabled |
||
| 76 | * |
||
| 77 | * @var boolean |
||
| 78 | */ |
||
| 79 | private $bAutoloadEnabled; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * The Composer autoloader |
||
| 83 | * |
||
| 84 | * @var Autoloader |
||
| 85 | */ |
||
| 86 | private $xAutoloader; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Javascript confirm function |
||
| 90 | * |
||
| 91 | * @var \Jaxon\Request\Interfaces\Confirm |
||
| 92 | */ |
||
| 93 | private $xConfirm; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Default javascript confirm function |
||
| 97 | * |
||
| 98 | * @var \Jaxon\Request\Support\Confirm |
||
| 99 | */ |
||
| 100 | private $xDefaultConfirm; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Javascript alert function |
||
| 104 | * |
||
| 105 | * @var \Jaxon\Request\Interfaces\Alert |
||
| 106 | */ |
||
| 107 | private $xAlert; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Default javascript alert function |
||
| 111 | * |
||
| 112 | * @var \Jaxon\Request\Support\Alert |
||
| 113 | */ |
||
| 114 | private $xDefaultAlert; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Initialize the Jaxon Plugin Manager |
||
| 118 | */ |
||
| 119 | public function __construct() |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Use the Composer autoloader |
||
| 140 | * |
||
| 141 | * @return void |
||
| 142 | */ |
||
| 143 | public function useComposerAutoloader() |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Disable the autoloader in the library |
||
| 151 | * |
||
| 152 | * The user shall provide an alternative autoload system. |
||
| 153 | * |
||
| 154 | * @return void |
||
| 155 | */ |
||
| 156 | public function disableAutoload() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Set the javascript confirm function |
||
| 164 | * |
||
| 165 | * @param \Jaxon\Request\Interfaces\Confirm $xConfirm The javascript confirm function |
||
| 166 | * |
||
| 167 | * @return void |
||
| 168 | */ |
||
| 169 | public function setConfirm(\Jaxon\Request\Interfaces\Confirm $xConfirm) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Get the javascript confirm function |
||
| 176 | * |
||
| 177 | * @return \Jaxon\Request\Interfaces\Confirm |
||
| 178 | */ |
||
| 179 | public function getConfirm() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the default javascript confirm function |
||
| 186 | * |
||
| 187 | * @return \Jaxon\Request\Support\Confirm |
||
| 188 | */ |
||
| 189 | public function getDefaultConfirm() |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Set the javascript alert function |
||
| 196 | * |
||
| 197 | * @param \Jaxon\Request\Interfaces\Alert $xAlert The javascript alert function |
||
| 198 | * |
||
| 199 | * @return void |
||
| 200 | */ |
||
| 201 | public function setAlert(\Jaxon\Request\Interfaces\Alert $xAlert) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Get the javascript alert function |
||
| 208 | * |
||
| 209 | * @return \Jaxon\Request\Interfaces\Alert |
||
| 210 | */ |
||
| 211 | public function getAlert() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get the default javascript alert function |
||
| 218 | * |
||
| 219 | * @return \Jaxon\Request\Support\Alert |
||
| 220 | */ |
||
| 221 | public function getDefaultAlert() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Inserts an entry into an array given the specified priority number |
||
| 228 | * |
||
| 229 | * If a plugin already exists with the given priority, the priority is automatically incremented until a free spot is found. |
||
| 230 | * The plugin is then inserted into the empty spot in the array. |
||
| 231 | * |
||
| 232 | * @param Plugin $xPlugin An instance of a plugin |
||
| 233 | * @param integer $nPriority The desired priority, used to order the plugins |
||
| 234 | * |
||
| 235 | * @return void |
||
| 236 | */ |
||
| 237 | private function setPluginPriority(Plugin $xPlugin, $nPriority) |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Register a plugin |
||
| 250 | * |
||
| 251 | * Below is a table for priorities and their description: |
||
| 252 | * - 0 thru 999: Plugins that are part of or extensions to the jaxon core |
||
| 253 | * - 1000 thru 8999: User created plugins, typically, these plugins don't care about order |
||
| 254 | * - 9000 thru 9999: Plugins that generally need to be last or near the end of the plugin list |
||
| 255 | * |
||
| 256 | * @param Plugin $xPlugin An instance of a plugin |
||
| 257 | * @param integer $nPriority The plugin priority, used to order the plugins |
||
| 258 | * |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | public function registerPlugin(Plugin $xPlugin, $nPriority = 1000) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Generate a hash for all the javascript code generated by the library |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | private function generateHash() |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Check if the current request can be processed |
||
| 315 | * |
||
| 316 | * Calls each of the request plugins and determines if the current request can be processed by one of them. |
||
| 317 | * If no processor identifies the current request, then the request must be for the initial page load. |
||
| 318 | * |
||
| 319 | * @return boolean |
||
| 320 | */ |
||
| 321 | public function canProcessRequest() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Process the current request |
||
| 335 | * |
||
| 336 | * Calls each of the request plugins to request that they process the current request. |
||
| 337 | * If any plugin processes the request, it will return true. |
||
| 338 | * |
||
| 339 | * @return boolean |
||
| 340 | */ |
||
| 341 | public function processRequest() |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Register a function, event or callable object |
||
| 356 | * |
||
| 357 | * Call each of the request plugins and give them the opportunity to handle the |
||
| 358 | * registration of the specified function, event or callable object. |
||
| 359 | * |
||
| 360 | * @param array $aArgs The registration data |
||
| 361 | * |
||
| 362 | * @return mixed |
||
| 363 | */ |
||
| 364 | public function register($aArgs) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Add a path to the class directories |
||
| 379 | * |
||
| 380 | * @param string $sDirectory The path to the directory |
||
| 381 | * @param string|null $sNamespace The associated namespace |
||
| 382 | * @param string $sSeparator The character to use as separator in javascript class names |
||
| 383 | * @param array $aProtected The functions that are not to be exported |
||
| 384 | * |
||
| 385 | * @return boolean |
||
| 386 | */ |
||
| 387 | public function addClassDir($sDirectory, $sNamespace = null, $sSeparator = '.', array $aProtected = array()) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Register an instance of a given class from a file |
||
| 437 | * |
||
| 438 | * @param object $xFile The PHP file containing the class |
||
| 439 | * @param string $sDirectory The path to the directory |
||
| 440 | * @param string|null $sNamespace The associated namespace |
||
| 441 | * @param string $sSeparator The character to use as separator in javascript class names |
||
| 442 | * @param array $aProtected The functions that are not to be exported |
||
| 443 | * @param array $aOptions The options to register the class with |
||
| 444 | * |
||
| 445 | * @return void |
||
| 446 | */ |
||
| 447 | protected function registerClassFromFile($xFile, $sDirectory, $sNamespace = null, $sSeparator = '.', |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Register callable objects from all class directories |
||
| 489 | * |
||
| 490 | * @param array $aOptions The options to register the classes with |
||
| 491 | * |
||
| 492 | * @return void |
||
| 493 | */ |
||
| 494 | public function registerClasses(array $aOptions = array()) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * Register an instance of a given class |
||
| 547 | * |
||
| 548 | * @param string $sClassName The name of the class to be registered |
||
| 549 | * @param array $aOptions The options to register the class with |
||
| 550 | * |
||
| 551 | * @return bool |
||
| 552 | */ |
||
| 553 | public function registerClass($sClassName, array $aOptions = array()) |
||
| 602 | |||
| 603 | /** |
||
| 604 | * Find a user registered callable object by class name |
||
| 605 | * |
||
| 606 | * @param string $sClassName The class name of the callable object |
||
| 607 | * |
||
| 608 | * @return object |
||
| 609 | */ |
||
| 610 | public function getRegisteredObject($sClassName) |
||
| 620 | |||
| 621 | /** |
||
| 622 | * Get the base URI of the Jaxon library javascript files |
||
| 623 | * |
||
| 624 | * @return string |
||
| 625 | */ |
||
| 626 | private function getJsLibUri() |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Get the extension of the Jaxon library javascript files |
||
| 638 | * |
||
| 639 | * The returned string is '.min.js' if the files are minified. |
||
| 640 | * |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | private function getJsLibExt() |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Check if the javascript code generated by Jaxon can be exported to an external file |
||
| 657 | * |
||
| 658 | * @return boolean |
||
| 659 | */ |
||
| 660 | public function canExportJavascript() |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Set the cache directory for the template engine |
||
| 683 | * |
||
| 684 | * @return void |
||
| 685 | */ |
||
| 686 | private function setTemplateCacheDir() |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 696 | * |
||
| 697 | * @return string |
||
| 698 | */ |
||
| 699 | public function getJs() |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
| 735 | * |
||
| 736 | * @return string |
||
| 737 | */ |
||
| 738 | public function getCss() |
||
| 750 | |||
| 751 | /** |
||
| 752 | * Get the correspondances between previous and current config options |
||
| 753 | * |
||
| 754 | * They are used to keep the deprecated config options working. |
||
| 755 | * They will be removed when the deprecated options will lot be supported anymore. |
||
| 756 | * |
||
| 757 | * @return array |
||
| 758 | */ |
||
| 759 | private function getOptionVars() |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Get the javascript code for Jaxon client side configuration |
||
| 782 | * |
||
| 783 | * @return string |
||
| 784 | */ |
||
| 785 | private function getConfigScript() |
||
| 795 | |||
| 796 | /** |
||
| 797 | * Get the javascript code to be run after page load |
||
| 798 | * |
||
| 799 | * Also call each of the response plugins giving them the opportunity |
||
| 800 | * to output some javascript to the page being generated. |
||
| 801 | * |
||
| 802 | * @return string |
||
| 803 | */ |
||
| 804 | private function getReadyScript() |
||
| 846 | |||
| 847 | /** |
||
| 848 | * Get the javascript code to be sent to the browser |
||
| 849 | * |
||
| 850 | * Also call each of the request plugins giving them the opportunity |
||
| 851 | * to output some javascript to the page being generated. |
||
| 852 | * This is called only when the page is being loaded initially. |
||
| 853 | * This is not called when processing a request. |
||
| 854 | * |
||
| 855 | * @return string |
||
| 856 | */ |
||
| 857 | public function getScript() |
||
| 906 | |||
| 907 | /** |
||
| 908 | * Find the specified response plugin by name and return a reference to it if one exists |
||
| 909 | * |
||
| 910 | * @param string $sName The name of the plugin |
||
| 911 | * |
||
| 912 | * @return \Jaxon\Plugin\Response |
||
| 913 | */ |
||
| 914 | public function getResponsePlugin($sName) |
||
| 922 | |||
| 923 | /** |
||
| 924 | * Find the specified request plugin by name and return a reference to it if one exists |
||
| 925 | * |
||
| 926 | * @param string $sName The name of the plugin |
||
| 927 | * |
||
| 928 | * @return \Jaxon\Plugin\Request |
||
| 929 | */ |
||
| 930 | public function getRequestPlugin($sName) |
||
| 938 | } |
||
| 939 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: