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:
1 | <?php |
||
37 | class Jaxon |
||
38 | { |
||
39 | use \Jaxon\Utils\Traits\Config; |
||
40 | use \Jaxon\Utils\Traits\Manager; |
||
41 | use \Jaxon\Utils\Traits\Translator; |
||
42 | use \Jaxon\Utils\Traits\Paginator; |
||
43 | use \Jaxon\Utils\Traits\Template; |
||
44 | |||
45 | use Traits\Autoload; |
||
46 | use Traits\Config; |
||
47 | use Traits\Plugin; |
||
48 | use Traits\Upload; |
||
49 | use Traits\Sentry; |
||
50 | |||
51 | /** |
||
52 | * Package version number |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | private $sVersion = 'Jaxon 2.2.4'; |
||
57 | |||
58 | /* |
||
59 | * Processing events |
||
60 | */ |
||
61 | const PROCESSING_EVENT = 'ProcessingEvent'; |
||
62 | const PROCESSING_EVENT_BEFORE = 'BeforeProcessing'; |
||
63 | const PROCESSING_EVENT_AFTER = 'AfterProcessing'; |
||
64 | const PROCESSING_EVENT_INVALID = 'InvalidRequest'; |
||
65 | const PROCESSING_EVENT_ERROR = 'ProcessingError'; |
||
66 | |||
67 | /* |
||
68 | * Request methods |
||
69 | */ |
||
70 | const METHOD_UNKNOWN = 0; |
||
71 | const METHOD_GET = 1; |
||
72 | const METHOD_POST = 2; |
||
73 | |||
74 | /* |
||
75 | * Request plugins |
||
76 | */ |
||
77 | // For objects who's methods will be callable from the browser. |
||
78 | const CALLABLE_OBJECT = 'CallableObject'; |
||
79 | // For functions available at global scope, or from an instance of an object. |
||
80 | const USER_FUNCTION = 'UserFunction'; |
||
81 | // For browser events. |
||
82 | const BROWSER_EVENT = 'BrowserEvent'; |
||
83 | // For event handlers. |
||
84 | const EVENT_HANDLER = 'EventHandler'; |
||
85 | // For uploaded files. |
||
86 | const FILE_UPLOAD = 'FileUpload'; |
||
87 | |||
88 | /* |
||
89 | * Request parameters |
||
90 | */ |
||
91 | // Specifies that the parameter will consist of an array of form values. |
||
92 | const FORM_VALUES = 'FormValues'; |
||
93 | // Specifies that the parameter will contain the value of an input control. |
||
94 | const INPUT_VALUE = 'InputValue'; |
||
95 | // Specifies that the parameter will consist of a boolean value of a checkbox. |
||
96 | const CHECKED_VALUE = 'CheckedValue'; |
||
97 | // Specifies that the parameter value will be the innerHTML value of the element. |
||
98 | const ELEMENT_INNERHTML = 'ElementInnerHTML'; |
||
99 | // Specifies that the parameter will be a quoted value (string). |
||
100 | const QUOTED_VALUE = 'QuotedValue'; |
||
101 | // Specifies that the parameter will be a boolean value (true or false). |
||
102 | const BOOL_VALUE = 'BoolValue'; |
||
103 | // Specifies that the parameter will be a numeric, non-quoted value. |
||
104 | const NUMERIC_VALUE = 'NumericValue'; |
||
105 | // Specifies that the parameter will be a non-quoted value |
||
106 | // (evaluated by the browsers javascript engine at run time). |
||
107 | const JS_VALUE = 'UnquotedValue'; |
||
108 | // Specifies that the parameter will be an integer used to generate pagination links. |
||
109 | const PAGE_NUMBER = 'PageNumber'; |
||
110 | |||
111 | /** |
||
112 | * Processing event handlers that have been assigned during this run of the script |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | private $aProcessingEvents; |
||
117 | |||
118 | public function __construct() |
||
123 | |||
124 | /** |
||
125 | * The current Jaxon version |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | public function getVersion() |
||
133 | |||
134 | /** |
||
135 | * Register request handlers, including functions, callable objects and events. |
||
136 | * |
||
137 | * New plugins can be added that support additional registration methods and request processors. |
||
138 | * |
||
139 | * @param string $sType The type of request handler being registered |
||
140 | * Options include: |
||
141 | * - Jaxon::USER_FUNCTION: a function declared at global scope |
||
142 | * - Jaxon::CALLABLE_OBJECT: an object who's methods are to be registered |
||
143 | * - Jaxon::BROWSER_EVENT: an event which will cause zero or more event handlers to be called |
||
144 | * - Jaxon::EVENT_HANDLER: register an event handler function. |
||
145 | * @param mixed $sFunction | $objObject | $sEvent |
||
146 | * When registering a function, this is the name of the function |
||
147 | * When registering a callable object, this is the object being registered |
||
148 | * When registering an event or event handler, this is the name of the event |
||
149 | * @param mixed $sIncludeFile | $aCallOptions | $sEventHandler |
||
150 | * When registering a function, this is the (optional) include file |
||
151 | * When registering a callable object, this is an (optional) array |
||
152 | * of call options for the functions being registered |
||
153 | * When registering an event handler, this is the name of the function |
||
154 | * |
||
155 | * @return mixed |
||
156 | */ |
||
157 | public function register($sType, $xArgs) |
||
181 | |||
182 | /** |
||
183 | * Add a path to the class directories |
||
184 | * |
||
185 | * @param string $sDirectory The path to the directory |
||
186 | * @param string|null $sNamespace The associated namespace |
||
187 | * @param string $sSeparator The character to use as separator in javascript class names |
||
188 | * @param array $aProtected The functions that are not to be exported |
||
189 | * |
||
190 | * @return boolean |
||
191 | */ |
||
192 | public function addClassDir($sDirectory, $sNamespace = null, $sSeparator = '.', array $aProtected = array()) |
||
196 | |||
197 | /** |
||
198 | * Register callable objects from all class directories |
||
199 | * |
||
200 | * @param array $aOptions The options to register the classes with |
||
201 | * |
||
202 | * @return void |
||
203 | */ |
||
204 | public function registerClasses(array $aOptions = array()) |
||
208 | |||
209 | /** |
||
210 | * Register a callable object from one of the class directories |
||
211 | * |
||
212 | * The class name can be dot, slash or anti-slash separated. |
||
213 | * If the $bGetObject parameter is set to true, the registered instance of the class is returned. |
||
214 | * |
||
215 | * @param string $sClassName The name of the class to register |
||
216 | * @param array $aOptions The options to register the class with |
||
217 | * @param boolean $bGetObject Return the registered instance of the class |
||
218 | * |
||
219 | * @return void |
||
220 | */ |
||
221 | public function registerClass($sClassName, array $aOptions = array(), $bGetObject = false) |
||
226 | |||
227 | /** |
||
228 | * Returns the Jaxon Javascript header and wrapper code to be printed into the page |
||
229 | * |
||
230 | * The javascript code returned by this function is dependent on the plugins |
||
231 | * that are included and the functions and classes that are registered. |
||
232 | * |
||
233 | * @param boolean $bIncludeJs Also get the JS files |
||
234 | * @param boolean $bIncludeCss Also get the CSS files |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | public function getScript($bIncludeJs = false, $bIncludeCss = false) |
||
256 | |||
257 | /** |
||
258 | * Print the jaxon Javascript header and wrapper code into your page |
||
259 | * |
||
260 | * The javascript code returned by this function is dependent on the plugins |
||
261 | * that are included and the functions and classes that are registered. |
||
262 | * |
||
263 | * @param boolean $bIncludeJs Also print the JS files |
||
264 | * @param boolean $bIncludeCss Also print the CSS files |
||
265 | * |
||
266 | * @return void |
||
267 | */ |
||
268 | public function printScript($bIncludeJs = false, $bIncludeCss = false) |
||
272 | |||
273 | /** |
||
274 | * Return the javascript header code and file includes |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | public function getJs() |
||
282 | |||
283 | /** |
||
284 | * Return the CSS header code and file includes |
||
285 | * |
||
286 | * @return string |
||
287 | */ |
||
288 | public function getCss() |
||
292 | |||
293 | /** |
||
294 | * Determine if a call is a jaxon request or a page load request |
||
295 | * |
||
296 | * @return boolean |
||
297 | */ |
||
298 | public function canProcessRequest() |
||
302 | |||
303 | /** |
||
304 | * If this is a jaxon request, call the requested PHP function, build the response and send it back to the browser |
||
305 | * |
||
306 | * This is the main server side engine for Jaxon. |
||
307 | * It handles all the incoming requests, including the firing of events and handling of the response. |
||
308 | * If your RequestURI is the same as your web page, then this function should be called before ANY |
||
309 | * headers or HTML is output from your script. |
||
310 | * |
||
311 | * This function may exit after the request is processed, if the 'core.process.exit' option is set to true. |
||
312 | * |
||
313 | * @return void |
||
314 | * |
||
315 | * @see <Jaxon\Jaxon->canProcessRequest> |
||
316 | */ |
||
317 | public function processRequest() |
||
419 | |||
420 | /** |
||
421 | * Send the response output back to the browser |
||
422 | * |
||
423 | * @return void |
||
424 | */ |
||
425 | public function sendResponse() |
||
429 | |||
430 | /** |
||
431 | * Send the HTTP headers back to the browser |
||
432 | * |
||
433 | * @return void |
||
434 | */ |
||
435 | public function sendHeaders() |
||
439 | |||
440 | /** |
||
441 | * Get the response output |
||
442 | * |
||
443 | * @return string |
||
444 | */ |
||
445 | public function getOutput() |
||
449 | |||
450 | /** |
||
451 | * Get the DI container |
||
452 | * |
||
453 | * @return Jaxon\DI\Container |
||
454 | */ |
||
455 | public function di() |
||
459 | } |
||
460 |