Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 32 | class Manager |
||
| 33 | { |
||
| 34 | use \Jaxon\Utils\Traits\Manager; |
||
| 35 | use \Jaxon\Utils\Traits\Config; |
||
| 36 | use \Jaxon\Utils\Traits\Cache; |
||
| 37 | use \Jaxon\Utils\Traits\Event; |
||
| 38 | use \Jaxon\Utils\Traits\Minifier; |
||
| 39 | use \Jaxon\Utils\Traits\Template; |
||
| 40 | use \Jaxon\Utils\Traits\Translator; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The response type. |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | const RESPONSE_TYPE = 'JSON'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * All plugins, indexed by priority |
||
| 51 | * |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private $aPlugins = []; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Request plugins, indexed by name |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | private $aRequestPlugins = []; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Response plugins, indexed by name |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | private $aResponsePlugins = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Directories where Jaxon classes to be registered are found |
||
| 72 | * |
||
| 73 | * @var array |
||
| 74 | */ |
||
| 75 | private $aClassDirs = []; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * An array of package names |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | private $aPackages = []; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * True if the Composer autoload is enabled |
||
| 86 | * |
||
| 87 | * @var boolean |
||
| 88 | */ |
||
| 89 | private $bAutoloadEnabled; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The Composer autoloader |
||
| 93 | * |
||
| 94 | * @var Autoloader |
||
| 95 | */ |
||
| 96 | private $xAutoloader; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Javascript confirm function |
||
| 100 | * |
||
| 101 | * @var \Jaxon\Request\Interfaces\Confirm |
||
| 102 | */ |
||
| 103 | private $xConfirm; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Default javascript confirm function |
||
| 107 | * |
||
| 108 | * @var \Jaxon\Request\Support\Confirm |
||
| 109 | */ |
||
| 110 | private $xDefaultConfirm; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Javascript alert function |
||
| 114 | * |
||
| 115 | * @var \Jaxon\Request\Interfaces\Alert |
||
| 116 | */ |
||
| 117 | private $xAlert; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Default javascript alert function |
||
| 121 | * |
||
| 122 | * @var \Jaxon\Request\Support\Alert |
||
| 123 | */ |
||
| 124 | private $xDefaultAlert; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Initialize the Jaxon Plugin Manager |
||
| 128 | */ |
||
| 129 | public function __construct() |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Use the Composer autoloader |
||
| 145 | * |
||
| 146 | * @return void |
||
| 147 | */ |
||
| 148 | public function useComposerAutoloader() |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Disable the autoloader in the library |
||
| 156 | * |
||
| 157 | * The user shall provide an alternative autoload system. |
||
| 158 | * |
||
| 159 | * @return void |
||
| 160 | */ |
||
| 161 | public function disableAutoload() |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Set the javascript confirm function |
||
| 169 | * |
||
| 170 | * @param \Jaxon\Request\Interfaces\Confirm $xConfirm The javascript confirm function |
||
| 171 | * |
||
| 172 | * @return void |
||
| 173 | */ |
||
| 174 | public function setConfirm(\Jaxon\Request\Interfaces\Confirm $xConfirm) |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Get the javascript confirm function |
||
| 181 | * |
||
| 182 | * @return \Jaxon\Request\Interfaces\Confirm |
||
| 183 | */ |
||
| 184 | public function getConfirm() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Get the default javascript confirm function |
||
| 191 | * |
||
| 192 | * @return \Jaxon\Request\Support\Confirm |
||
| 193 | */ |
||
| 194 | public function getDefaultConfirm() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Set the javascript alert function |
||
| 201 | * |
||
| 202 | * @param \Jaxon\Request\Interfaces\Alert $xAlert The javascript alert function |
||
| 203 | * |
||
| 204 | * @return void |
||
| 205 | */ |
||
| 206 | public function setAlert(\Jaxon\Request\Interfaces\Alert $xAlert) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the javascript alert function |
||
| 213 | * |
||
| 214 | * @return \Jaxon\Request\Interfaces\Alert |
||
| 215 | */ |
||
| 216 | public function getAlert() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Get the default javascript alert function |
||
| 223 | * |
||
| 224 | * @return \Jaxon\Request\Support\Alert |
||
| 225 | */ |
||
| 226 | public function getDefaultAlert() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Inserts an entry into an array given the specified priority number |
||
| 233 | * |
||
| 234 | * If a plugin already exists with the given priority, the priority is automatically incremented until a free spot is found. |
||
| 235 | * The plugin is then inserted into the empty spot in the array. |
||
| 236 | * |
||
| 237 | * @param Plugin $xPlugin An instance of a plugin |
||
| 238 | * @param integer $nPriority The desired priority, used to order the plugins |
||
| 239 | * |
||
| 240 | * @return void |
||
| 241 | */ |
||
| 242 | private function setPluginPriority(Plugin $xPlugin, $nPriority) |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Register a plugin |
||
| 255 | * |
||
| 256 | * Below is a table for priorities and their description: |
||
| 257 | * - 0 thru 999: Plugins that are part of or extensions to the jaxon core |
||
| 258 | * - 1000 thru 8999: User created plugins, typically, these plugins don't care about order |
||
| 259 | * - 9000 thru 9999: Plugins that generally need to be last or near the end of the plugin list |
||
| 260 | * |
||
| 261 | * @param Plugin $xPlugin An instance of a plugin |
||
| 262 | * @param integer $nPriority The plugin priority, used to order the plugins |
||
| 263 | * |
||
| 264 | * @return void |
||
| 265 | */ |
||
| 266 | public function registerPlugin(Plugin $xPlugin, $nPriority = 1000) |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Register a package |
||
| 305 | * |
||
| 306 | * @param string $sPackageClass The package class name |
||
| 307 | * @param Closure $xClosure A closure to create package instance |
||
| 308 | * |
||
| 309 | * @return void |
||
| 310 | */ |
||
| 311 | public function registerPackage(string $sPackageClass, Closure $xClosure) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Generate a hash for all the javascript code generated by the library |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | private function generateHash() |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Check if the current request can be processed |
||
| 334 | * |
||
| 335 | * Calls each of the request plugins and determines if the current request can be processed by one of them. |
||
| 336 | * If no processor identifies the current request, then the request must be for the initial page load. |
||
| 337 | * |
||
| 338 | * @return boolean |
||
| 339 | */ |
||
| 340 | public function canProcessRequest() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Process the current request |
||
| 354 | * |
||
| 355 | * Calls each of the request plugins to request that they process the current request. |
||
| 356 | * If any plugin processes the request, it will return true. |
||
| 357 | * |
||
| 358 | * @return boolean |
||
| 359 | */ |
||
| 360 | public function processRequest() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Register a function, event or callable object |
||
| 382 | * |
||
| 383 | * Call each of the request plugins and give them the opportunity to handle the |
||
| 384 | * registration of the specified function, event or callable object. |
||
| 385 | * |
||
| 386 | * @param array $aArgs The registration data |
||
| 387 | * |
||
| 388 | * @return mixed |
||
| 389 | */ |
||
| 390 | public function register($aArgs) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Add a path to the class directories |
||
| 405 | * |
||
| 406 | * @param string $sDirectory The path to the directory |
||
| 407 | * @param string|null $sNamespace The associated namespace |
||
| 408 | * @param string $sSeparator The character to use as separator in javascript class names |
||
| 409 | * @param array $aProtected The functions that are not to be exported |
||
| 410 | * |
||
| 411 | * @return boolean |
||
| 412 | */ |
||
| 413 | public function addClassDir($sDirectory, $sNamespace = '', $sSeparator = '.', array $aProtected = []) |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Register an instance of a given class from a file |
||
| 463 | * |
||
| 464 | * @param object $xFile The PHP file containing the class |
||
| 465 | * @param string $sDirectory The path to the directory |
||
| 466 | * @param string|'' $sNamespace The associated namespace |
||
|
|
|||
| 467 | * @param string $sSeparator The character to use as separator in javascript class names |
||
| 468 | * @param array $aProtected The functions that are not to be exported |
||
| 469 | * @param array $aOptions The options to register the class with |
||
| 470 | * |
||
| 471 | * @return void |
||
| 472 | */ |
||
| 473 | protected function registerClassFromFile($xFile, $sDirectory, $sNamespace = '', $sSeparator = '.', |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Register callable objects from all class directories |
||
| 516 | * |
||
| 517 | * @param array $aOptions The options to register the classes with |
||
| 518 | * |
||
| 519 | * @return void |
||
| 520 | */ |
||
| 521 | public function registerClasses(array $aOptions = []) |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Register an instance of a given class |
||
| 577 | * |
||
| 578 | * @param string $sClassName The name of the class to be registered |
||
| 579 | * @param array $aOptions The options to register the class with |
||
| 580 | * |
||
| 581 | * @return bool |
||
| 582 | */ |
||
| 583 | public function registerClass($sClassName, array $aOptions = []) |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Find a user registered callable object by class name |
||
| 635 | * |
||
| 636 | * @param string $sClassName The class name of the callable object |
||
| 637 | * |
||
| 638 | * @return object |
||
| 639 | */ |
||
| 640 | public function getRegisteredObject($sClassName) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Get the base URI of the Jaxon library javascript files |
||
| 653 | * |
||
| 654 | * @return string |
||
| 655 | */ |
||
| 656 | private function getJsLibUri() |
||
| 666 | |||
| 667 | /** |
||
| 668 | * Get the extension of the Jaxon library javascript files |
||
| 669 | * |
||
| 670 | * The returned string is '.min.js' if the files are minified. |
||
| 671 | * |
||
| 672 | * @return string |
||
| 673 | */ |
||
| 674 | private function getJsLibExt() |
||
| 687 | |||
| 688 | /** |
||
| 689 | * Check if the javascript code generated by Jaxon can be exported to an external file |
||
| 690 | * |
||
| 691 | * @return boolean |
||
| 692 | */ |
||
| 693 | public function canExportJavascript() |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Set the cache directory for the template engine |
||
| 716 | * |
||
| 717 | * @return void |
||
| 718 | */ |
||
| 719 | private function setTemplateCacheDir() |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Get the HTML tags to include Jaxon javascript files into the page |
||
| 729 | * |
||
| 730 | * @return string |
||
| 731 | */ |
||
| 732 | public function getJs() |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
| 779 | * |
||
| 780 | * @return string |
||
| 781 | */ |
||
| 782 | View Code Duplication | public function getCss() |
|
| 805 | |||
| 806 | /** |
||
| 807 | * Get the correspondances between previous and current config options |
||
| 808 | * |
||
| 809 | * They are used to keep the deprecated config options working. |
||
| 810 | * They will be removed when the deprecated options will lot be supported anymore. |
||
| 811 | * |
||
| 812 | * @return array |
||
| 813 | */ |
||
| 814 | private function getOptionVars() |
||
| 834 | |||
| 835 | /** |
||
| 836 | * Get the javascript code for Jaxon client side configuration |
||
| 837 | * |
||
| 838 | * @return string |
||
| 839 | */ |
||
| 840 | private function getConfigScript() |
||
| 850 | |||
| 851 | /** |
||
| 852 | * Get the javascript code to be run after page load |
||
| 853 | * |
||
| 854 | * Also call each of the response plugins giving them the opportunity |
||
| 855 | * to output some javascript to the page being generated. |
||
| 856 | * |
||
| 857 | * @return string |
||
| 858 | */ |
||
| 859 | View Code Duplication | private function getReadyScript() |
|
| 880 | |||
| 881 | /** |
||
| 882 | * Get the javascript code to be sent to the browser |
||
| 883 | * |
||
| 884 | * Also call each of the request plugins giving them the opportunity |
||
| 885 | * to output some javascript to the page being generated. |
||
| 886 | * This is called only when the page is being loaded initially. |
||
| 887 | * This is not called when processing a request. |
||
| 888 | * |
||
| 889 | * @return string |
||
| 890 | */ |
||
| 891 | private function getAllScripts() |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Get the javascript code to be sent to the browser |
||
| 904 | * |
||
| 905 | * Also call each of the request plugins giving them the opportunity |
||
| 906 | * to output some javascript to the page being generated. |
||
| 907 | * This is called only when the page is being loaded initially. |
||
| 908 | * This is not called when processing a request. |
||
| 909 | * |
||
| 910 | * @return string |
||
| 911 | */ |
||
| 912 | public function getScript() |
||
| 955 | |||
| 956 | /** |
||
| 957 | * Find the specified response plugin by name and return a reference to it if one exists |
||
| 958 | * |
||
| 959 | * @param string $sName The name of the plugin |
||
| 960 | * |
||
| 961 | * @return \Jaxon\Plugin\Response |
||
| 962 | */ |
||
| 963 | public function getResponsePlugin($sName) |
||
| 971 | |||
| 972 | /** |
||
| 973 | * Find the specified request plugin by name and return a reference to it if one exists |
||
| 974 | * |
||
| 975 | * @param string $sName The name of the plugin |
||
| 976 | * |
||
| 977 | * @return \Jaxon\Plugin\Request |
||
| 978 | */ |
||
| 979 | public function getRequestPlugin($sName) |
||
| 987 | } |
||
| 988 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.