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