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() |
||
322 | { |
||
323 | foreach($this->aRequestPlugins as $xPlugin) |
||
324 | { |
||
325 | if($xPlugin->getName() != Jaxon::FILE_UPLOAD && $xPlugin->canProcessRequest()) |
||
326 | { |
||
327 | return true; |
||
328 | } |
||
329 | } |
||
330 | return false; |
||
331 | } |
||
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() |
||
342 | { |
||
343 | $xUploadPlugin = $this->getRequestPlugin(Jaxon::FILE_UPLOAD); |
||
344 | foreach($this->aRequestPlugins as $xPlugin) |
||
345 | { |
||
346 | if($xPlugin->getName() != Jaxon::FILE_UPLOAD && $xPlugin->canProcessRequest()) |
||
347 | { |
||
348 | // Process uploaded files |
||
349 | if($xUploadPlugin != null) |
||
350 | { |
||
351 | $xUploadPlugin->processRequest(); |
||
352 | } |
||
353 | // Process the request |
||
354 | return $xPlugin->processRequest(); |
||
355 | } |
||
356 | } |
||
357 | // Todo: throw an exception |
||
358 | return false; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Register a function, event or callable object |
||
363 | * |
||
364 | * Call each of the request plugins and give them the opportunity to handle the |
||
365 | * registration of the specified function, event or callable object. |
||
366 | * |
||
367 | * @param array $aArgs The registration data |
||
368 | * |
||
369 | * @return mixed |
||
370 | */ |
||
371 | public function register($aArgs) |
||
383 | |||
384 | /** |
||
385 | * Add a path to the class directories |
||
386 | * |
||
387 | * @param string $sDirectory The path to the directory |
||
388 | * @param string|null $sNamespace The associated namespace |
||
389 | * @param string $sSeparator The character to use as separator in javascript class names |
||
390 | * @param array $aProtected The functions that are not to be exported |
||
391 | * |
||
392 | * @return boolean |
||
393 | */ |
||
394 | public function addClassDir($sDirectory, $sNamespace = '', $sSeparator = '.', array $aProtected = array()) |
||
441 | |||
442 | /** |
||
443 | * Register an instance of a given class from a file |
||
444 | * |
||
445 | * @param object $xFile The PHP file containing the class |
||
446 | * @param string $sDirectory The path to the directory |
||
447 | * @param string|'' $sNamespace The associated namespace |
||
|
|||
448 | * @param string $sSeparator The character to use as separator in javascript class names |
||
449 | * @param array $aProtected The functions that are not to be exported |
||
450 | * @param array $aOptions The options to register the class with |
||
451 | * |
||
452 | * @return void |
||
453 | */ |
||
454 | protected function registerClassFromFile($xFile, $sDirectory, $sNamespace = '', $sSeparator = '.', |
||
494 | |||
495 | /** |
||
496 | * Register callable objects from all class directories |
||
497 | * |
||
498 | * @param array $aOptions The options to register the classes with |
||
499 | * |
||
500 | * @return void |
||
501 | */ |
||
502 | public function registerClasses(array $aOptions = array()) |
||
551 | |||
552 | /** |
||
553 | * Register an instance of a given class |
||
554 | * |
||
555 | * @param string $sClassName The name of the class to be registered |
||
556 | * @param array $aOptions The options to register the class with |
||
557 | * |
||
558 | * @return bool |
||
559 | */ |
||
560 | public function registerClass($sClassName, array $aOptions = array()) |
||
609 | |||
610 | /** |
||
611 | * Find a user registered callable object by class name |
||
612 | * |
||
613 | * @param string $sClassName The class name of the callable object |
||
614 | * |
||
615 | * @return object |
||
616 | */ |
||
617 | public function getRegisteredObject($sClassName) |
||
627 | |||
628 | /** |
||
629 | * Get the base URI of the Jaxon library javascript files |
||
630 | * |
||
631 | * @return string |
||
632 | */ |
||
633 | private function getJsLibUri() |
||
642 | |||
643 | /** |
||
644 | * Get the extension of the Jaxon library javascript files |
||
645 | * |
||
646 | * The returned string is '.min.js' if the files are minified. |
||
647 | * |
||
648 | * @return string |
||
649 | */ |
||
650 | private function getJsLibExt() |
||
661 | |||
662 | /** |
||
663 | * Check if the javascript code generated by Jaxon can be exported to an external file |
||
664 | * |
||
665 | * @return boolean |
||
666 | */ |
||
667 | public function canExportJavascript() |
||
687 | |||
688 | /** |
||
689 | * Set the cache directory for the template engine |
||
690 | * |
||
691 | * @return void |
||
692 | */ |
||
693 | private function setTemplateCacheDir() |
||
700 | |||
701 | /** |
||
702 | * Get the HTML tags to include Jaxon javascript files into the page |
||
703 | * |
||
704 | * @return string |
||
705 | */ |
||
706 | public function getJs() |
||
707 | { |
||
708 | $sJsLibUri = $this->getJsLibUri(); |
||
709 | $sJsLibExt = $this->getJsLibExt(); |
||
710 | $sJsCoreUrl = $sJsLibUri . 'jaxon.core' . $sJsLibExt; |
||
711 | $sJsDebugUrl = $sJsLibUri . 'jaxon.debug' . $sJsLibExt; |
||
712 | $sJsVerboseUrl = $sJsLibUri . 'jaxon.verbose' . $sJsLibExt; |
||
713 | $sJsLanguageUrl = $sJsLibUri . 'lang/jaxon.' . $this->getOption('core.language') . $sJsLibExt; |
||
714 | |||
715 | // Add component files to the javascript file array; |
||
716 | $aJsFiles = array($sJsCoreUrl); |
||
717 | if($this->getOption('core.debug.on')) |
||
718 | { |
||
719 | $aJsFiles[] = $sJsDebugUrl; |
||
720 | $aJsFiles[] = $sJsLanguageUrl; |
||
721 | /*if($this->getOption('core.debug.verbose')) |
||
722 | { |
||
723 | $aJsFiles[] = $sJsVerboseUrl; |
||
724 | }*/ |
||
725 | } |
||
726 | |||
727 | // Set the template engine cache dir |
||
728 | $this->setTemplateCacheDir(); |
||
729 | $sCode = $this->render('jaxon::plugins/includes.js', array( |
||
730 | 'sJsOptions' => $this->getOption('js.app.options'), |
||
731 | 'aUrls' => $aJsFiles, |
||
732 | )); |
||
733 | foreach($this->aResponsePlugins as $xPlugin) |
||
734 | { |
||
735 | $sCode .= rtrim($xPlugin->getJs(), " \n") . "\n"; |
||
736 | } |
||
737 | return $sCode; |
||
738 | } |
||
739 | |||
740 | /** |
||
741 | * Get the HTML tags to include Jaxon CSS code and files into the page |
||
742 | * |
||
743 | * @return string |
||
744 | */ |
||
745 | public function getCss() |
||
757 | |||
758 | /** |
||
759 | * Get the correspondances between previous and current config options |
||
760 | * |
||
761 | * They are used to keep the deprecated config options working. |
||
762 | * They will be removed when the deprecated options will lot be supported anymore. |
||
763 | * |
||
764 | * @return array |
||
765 | */ |
||
766 | private function getOptionVars() |
||
786 | |||
787 | /** |
||
788 | * Get the javascript code for Jaxon client side configuration |
||
789 | * |
||
790 | * @return string |
||
791 | */ |
||
792 | private function getConfigScript() |
||
802 | |||
803 | /** |
||
804 | * Get the javascript code to be run after page load |
||
805 | * |
||
806 | * Also call each of the response plugins giving them the opportunity |
||
807 | * to output some javascript to the page being generated. |
||
808 | * |
||
809 | * @return string |
||
810 | */ |
||
811 | private function getReadyScript() |
||
853 | |||
854 | /** |
||
855 | * Get the javascript code to be sent to the browser |
||
856 | * |
||
857 | * Also call each of the request plugins giving them the opportunity |
||
858 | * to output some javascript to the page being generated. |
||
859 | * This is called only when the page is being loaded initially. |
||
860 | * This is not called when processing a request. |
||
861 | * |
||
862 | * @return string |
||
863 | */ |
||
864 | private function getAllScripts() |
||
874 | |||
875 | /** |
||
876 | * Get the javascript code to be sent to the browser |
||
877 | * |
||
878 | * Also call each of the request plugins giving them the opportunity |
||
879 | * to output some javascript to the page being generated. |
||
880 | * This is called only when the page is being loaded initially. |
||
881 | * This is not called when processing a request. |
||
882 | * |
||
883 | * @return string |
||
884 | */ |
||
885 | public function getScript() |
||
928 | |||
929 | /** |
||
930 | * Find the specified response plugin by name and return a reference to it if one exists |
||
931 | * |
||
932 | * @param string $sName The name of the plugin |
||
933 | * |
||
934 | * @return \Jaxon\Plugin\Response |
||
935 | */ |
||
936 | public function getResponsePlugin($sName) |
||
944 | |||
945 | /** |
||
946 | * Find the specified request plugin by name and return a reference to it if one exists |
||
947 | * |
||
948 | * @param string $sName The name of the plugin |
||
949 | * |
||
950 | * @return \Jaxon\Plugin\Request |
||
951 | */ |
||
952 | public function getRequestPlugin($sName) |
||
960 | } |
||
961 |
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.