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 Jaxon 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 Jaxon, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class Jaxon |
||
36 | { |
||
37 | use \Jaxon\Utils\Traits\Config; |
||
38 | use \Jaxon\Utils\Traits\Manager; |
||
39 | use \Jaxon\Utils\Traits\Translator; |
||
40 | use \Jaxon\Utils\Traits\Paginator; |
||
41 | use \Jaxon\Utils\Traits\Template; |
||
42 | |||
43 | /** |
||
44 | * Package version number |
||
45 | * |
||
46 | * @var string |
||
47 | */ |
||
48 | private $sVersion = 'Jaxon 2.0-beta.25'; |
||
49 | |||
50 | /* |
||
51 | * Processing events |
||
52 | */ |
||
53 | const PROCESSING_EVENT = 'ProcessingEvent'; |
||
54 | const PROCESSING_EVENT_BEFORE = 'BeforeProcessing'; |
||
55 | const PROCESSING_EVENT_AFTER = 'AfterProcessing'; |
||
56 | const PROCESSING_EVENT_INVALID = 'InvalidRequest'; |
||
57 | const PROCESSING_EVENT_ERROR = 'ProcessingError'; |
||
58 | |||
59 | /* |
||
60 | * Request methods |
||
61 | */ |
||
62 | const METHOD_UNKNOWN = 0; |
||
63 | const METHOD_GET = 1; |
||
64 | const METHOD_POST = 2; |
||
65 | |||
66 | /* |
||
67 | * Request plugins |
||
68 | */ |
||
69 | // An object who's methods will be callable from the browser. |
||
70 | const CALLABLE_OBJECT = 'CallableObject'; |
||
71 | // A php function available at global scope, or a specific function from an instance of an object. |
||
72 | const USER_FUNCTION = 'UserFunction'; |
||
73 | // A browser event. |
||
74 | const BROWSER_EVENT = 'BrowserEvent'; |
||
75 | // An event handler. |
||
76 | const EVENT_HANDLER = 'EventHandler'; |
||
77 | |||
78 | /* |
||
79 | * Request parameters |
||
80 | */ |
||
81 | // Specifies that the parameter will consist of an array of form values. |
||
82 | const FORM_VALUES = 'FormValues'; |
||
83 | // Specifies that the parameter will contain the value of an input control. |
||
84 | const INPUT_VALUE = 'InputValue'; |
||
85 | // Specifies that the parameter will consist of a boolean value of a checkbox. |
||
86 | const CHECKED_VALUE = 'CheckedValue'; |
||
87 | // Specifies that the parameter value will be the innerHTML value of the element. |
||
88 | const ELEMENT_INNERHTML = 'ElementInnerHTML'; |
||
89 | // Specifies that the parameter will be a quoted value (string). |
||
90 | const QUOTED_VALUE = 'QuotedValue'; |
||
91 | // Specifies that the parameter will be a boolean value (true or false). |
||
92 | const BOOL_VALUE = 'BoolValue'; |
||
93 | // Specifies that the parameter will be a numeric, non-quoted value. |
||
94 | const NUMERIC_VALUE = 'NumericValue'; |
||
95 | // Specifies that the parameter will be a non-quoted value |
||
96 | // (evaluated by the browsers javascript engine at run time). |
||
97 | const JS_VALUE = 'UnquotedValue'; |
||
98 | // Specifies that the parameter will be an integer used to generate pagination links. |
||
99 | const PAGE_NUMBER = 'PageNumber'; |
||
100 | |||
101 | /** |
||
102 | * Processing event handlers that have been assigned during this run of the script |
||
103 | * |
||
104 | * @var array |
||
105 | */ |
||
106 | private $aProcessingEvents; |
||
107 | |||
108 | public function __construct() |
||
113 | |||
114 | /** |
||
115 | * Set the default options of all components of the library |
||
116 | * |
||
117 | * @return void |
||
118 | */ |
||
119 | private function setDefaultOptions() |
||
150 | |||
151 | /** |
||
152 | * Set Jaxon to use the Composer autoloader |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | public function useComposerAutoloader() |
||
160 | |||
161 | /** |
||
162 | * Disable Jaxon classes autoloading |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | public function disableAutoload() |
||
170 | |||
171 | /** |
||
172 | * The current Jaxon version |
||
173 | * |
||
174 | * @return string |
||
175 | */ |
||
176 | public function getVersion() |
||
180 | |||
181 | /** |
||
182 | * Register request handlers, including functions, callable objects and events. |
||
183 | * |
||
184 | * New plugins can be added that support additional registration methods and request processors. |
||
185 | * |
||
186 | * @param string $sType The type of request handler being registered |
||
187 | * Options include: |
||
188 | * - Jaxon::USER_FUNCTION: a function declared at global scope |
||
189 | * - Jaxon::CALLABLE_OBJECT: an object who's methods are to be registered |
||
190 | * - Jaxon::BROWSER_EVENT: an event which will cause zero or more event handlers to be called |
||
191 | * - Jaxon::EVENT_HANDLER: register an event handler function. |
||
192 | * @param mixed $sFunction | $objObject | $sEvent |
||
193 | * When registering a function, this is the name of the function |
||
194 | * When registering a callable object, this is the object being registered |
||
195 | * When registering an event or event handler, this is the name of the event |
||
196 | * @param mixed $sIncludeFile | $aCallOptions | $sEventHandler |
||
197 | * When registering a function, this is the (optional) include file |
||
198 | * When registering a callable object, this is an (optional) array |
||
199 | * of call options for the functions being registered |
||
200 | * When registering an event handler, this is the name of the function |
||
201 | * |
||
202 | * @return mixed |
||
203 | */ |
||
204 | public function register($sType, $xArgs) |
||
2 ignored issues
–
show
|
|||
205 | { |
||
206 | $aArgs = func_get_args(); |
||
207 | $nArgs = func_num_args(); |
||
208 | |||
209 | if(self::PROCESSING_EVENT == $aArgs[0]) |
||
210 | { |
||
211 | if($nArgs > 2) |
||
212 | { |
||
213 | $sEvent = $aArgs[1]; |
||
214 | $xUserFunction = $aArgs[2]; |
||
215 | if(!is_a($xUserFunction, 'Request\\Support\\UserFunction')) |
||
216 | $xUserFunction = new Request\Support\UserFunction($xUserFunction); |
||
217 | $this->aProcessingEvents[$sEvent] = $xUserFunction; |
||
218 | return true; |
||
219 | } |
||
220 | /*else |
||
221 | { |
||
222 | // Todo: return error |
||
223 | }*/ |
||
224 | } |
||
225 | |||
226 | return $this->getPluginManager()->register($aArgs); |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Add a path to the class directories |
||
231 | * |
||
232 | * @param string $sDirectory The path to the directory |
||
233 | * @param string|null $sNamespace The associated namespace |
||
234 | * @param string $sSeparator The character to use as separator in javascript class names |
||
235 | * @param array $aProtected The functions that are not to be exported |
||
236 | * |
||
237 | * @return boolean |
||
238 | */ |
||
239 | public function addClassDir($sDirectory, $sNamespace = null, $sSeparator = '.', array $aProtected = array()) |
||
243 | |||
244 | /** |
||
245 | * Register callable objects from all class directories |
||
246 | * |
||
247 | * @return void |
||
248 | */ |
||
249 | public function registerClasses() |
||
253 | |||
254 | /** |
||
255 | * Register a callable object from one of the class directories |
||
256 | * |
||
257 | * The class name can be dot, slash or anti-slash separated. |
||
258 | * If the $bGetObject parameter is set to true, the registered instance of the class is returned. |
||
259 | * |
||
260 | * @param string $sClassName The name of the class to register |
||
261 | * @param array $aOptions The options to register the class with |
||
262 | * @param boolean $bGetObject Return the registered instance of the class |
||
263 | * |
||
264 | * @return void |
||
265 | */ |
||
266 | public function registerClass($sClassName, array $aOptions = array(), $bGetObject = false) |
||
271 | |||
272 | /** |
||
273 | * Determine if a call is a jaxon request or a page load request |
||
274 | * |
||
275 | * @return boolean |
||
276 | */ |
||
277 | public function canProcessRequest() |
||
281 | |||
282 | /** |
||
283 | * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser |
||
284 | * |
||
285 | * This is the main server side engine for Jaxon. |
||
286 | * It handles all the incoming requests, including the firing of events and handling of the response. |
||
287 | * If your RequestURI is the same as your web page, then this function should be called before ANY |
||
288 | * headers or HTML is output from your script. |
||
289 | * |
||
290 | * This function may exit after the request is processed, if the 'core.process.exit' option is set to true. |
||
291 | * |
||
292 | * @return void |
||
293 | * |
||
294 | * @see <Jaxon\Jaxon->canProcessRequest> |
||
295 | */ |
||
296 | public function processRequest() |
||
393 | |||
394 | /** |
||
395 | * Send the response output back to the browser |
||
396 | * |
||
397 | * @return void |
||
398 | */ |
||
399 | public function sendResponse() |
||
403 | |||
404 | /** |
||
405 | * Send the HTTP headers back to the browser |
||
406 | * |
||
407 | * @return void |
||
408 | */ |
||
409 | public function sendHeaders() |
||
413 | |||
414 | /** |
||
415 | * Get the response output |
||
416 | * |
||
417 | * @return string |
||
418 | */ |
||
419 | public function getOutput() |
||
423 | |||
424 | /** |
||
425 | * Returns the Jaxon Javascript header and wrapper code to be printed into the page |
||
426 | * |
||
427 | * The javascript code returned by this function is dependent on the plugins |
||
428 | * that are included and the functions and classes that are registered. |
||
429 | * |
||
430 | * @param boolean $bIncludeJs Also get the JS files |
||
431 | * @param boolean $bIncludeCss Also get the CSS files |
||
432 | * |
||
433 | * @return string |
||
434 | */ |
||
435 | public function getScript($bIncludeJs = false, $bIncludeCss = false) |
||
453 | |||
454 | /** |
||
455 | * Print the jaxon Javascript header and wrapper code into your page |
||
456 | * |
||
457 | * The javascript code returned by this function is dependent on the plugins |
||
458 | * that are included and the functions and classes that are registered. |
||
459 | * |
||
460 | * @param boolean $bIncludeJs Also print the JS files |
||
461 | * @param boolean $bIncludeCss Also print the CSS files |
||
462 | * |
||
463 | * @return void |
||
464 | */ |
||
465 | public function printScript($bIncludeJs = false, $bIncludeCss = false) |
||
469 | |||
470 | /** |
||
471 | * Return the javascript header code and file includes |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | public function getJs() |
||
479 | |||
480 | /** |
||
481 | * Return the CSS header code and file includes |
||
482 | * |
||
483 | * @return string |
||
484 | */ |
||
485 | public function getCss() |
||
489 | |||
490 | /** |
||
491 | * Read and set Jaxon options from a PHP config file |
||
492 | * |
||
493 | * @param string $sConfigFile The full path to the config file |
||
494 | * @param string $sLibKey The key of the library options in the file |
||
495 | * @param string|null $sAppKey The key of the application options in the file |
||
496 | * |
||
497 | * @return array |
||
498 | */ |
||
499 | public function readPhpConfigFile($sConfigFile, $sLibKey = '', $sAppKey = null) |
||
503 | |||
504 | /** |
||
505 | * Read and set Jaxon options from a YAML config file |
||
506 | * |
||
507 | * @param string $sConfigFile The full path to the config file |
||
508 | * @param string $sLibKey The key of the library options in the file |
||
509 | * @param string|null $sAppKey The key of the application options in the file |
||
510 | * |
||
511 | * @return array |
||
512 | */ |
||
513 | public function readYamlConfigFile($sConfigFile, $sLibKey = '', $sAppKey = null) |
||
517 | |||
518 | /** |
||
519 | * Read and set Jaxon options from a JSON config file |
||
520 | * |
||
521 | * @param string $sConfigFile The full path to the config file |
||
522 | * @param string $sLibKey The key of the library options in the file |
||
523 | * @param string|null $sAppKey The key of the application options in the file |
||
524 | * |
||
525 | * @return array |
||
526 | */ |
||
527 | public function readJsonConfigFile($sConfigFile, $sLibKey = '', $sAppKey = null) |
||
531 | |||
532 | /** |
||
533 | * Read and set Jaxon options from a config file |
||
534 | * |
||
535 | * @param string $sConfigFile The full path to the config file |
||
536 | * @param string $sLibKey The key of the library options in the file |
||
537 | * @param string|null $sAppKey The key of the application options in the file |
||
538 | * |
||
539 | * @return array |
||
540 | */ |
||
541 | public function readConfigFile($sConfigFile, $sLibKey = '', $sAppKey = null) |
||
558 | |||
559 | /** |
||
560 | * Register a plugin |
||
561 | * |
||
562 | * Below is a table for priorities and their description: |
||
563 | * - 0 thru 999: Plugins that are part of or extensions to the jaxon core |
||
564 | * - 1000 thru 8999: User created plugins, typically, these plugins don't care about order |
||
565 | * - 9000 thru 9999: Plugins that generally need to be last or near the end of the plugin list |
||
566 | * |
||
567 | * @param Plugin $xPlugin An instance of a plugin |
||
568 | * @param integer $nPriority The plugin priority, used to order the plugins |
||
569 | * |
||
570 | * @return void |
||
571 | */ |
||
572 | public function registerPlugin(\Jaxon\Plugin\Plugin $xPlugin, $nPriority = 1000) |
||
576 | |||
577 | /** |
||
578 | * Register the Jaxon request plugins |
||
579 | * |
||
580 | * @return void |
||
581 | */ |
||
582 | public function registerRequestPlugins() |
||
588 | |||
589 | /** |
||
590 | * Register the Jaxon response plugins |
||
591 | * |
||
592 | * @return void |
||
593 | */ |
||
594 | public function registerResponsePlugins() |
||
599 | |||
600 | /** |
||
601 | * Set a new directory for pagination templates |
||
602 | * |
||
603 | * @param string $sDirectory The directory path |
||
604 | * |
||
605 | * @return void |
||
606 | */ |
||
607 | public function setPaginationDir($sDirectory) |
||
611 | |||
612 | /** |
||
613 | * Get the Sentry instance |
||
614 | * |
||
615 | * @return \Jaxon\Sentry\Sentry |
||
616 | */ |
||
617 | public function sentry() |
||
621 | |||
622 | /** |
||
623 | * Get the Armada instance |
||
624 | * |
||
625 | * @return \Jaxon\Sentry\Traits\Armada |
||
626 | */ |
||
627 | public function armada() |
||
631 | } |
||
632 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.