Complex classes like TActiveFileUpload 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 TActiveFileUpload, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
54 | class TActiveFileUpload extends TFileUpload implements IActiveControl, ICallbackEventHandler, INamingContainer |
||
55 | { |
||
56 | |||
57 | const SCRIPT_PATH = 'prado/activefileupload'; |
||
58 | |||
59 | /** |
||
60 | * @var THiddenField a flag to tell which component is doing the callback. |
||
61 | */ |
||
62 | private $_flag; |
||
63 | /** |
||
64 | * @var TImage that spins to show that the file is being uploaded. |
||
65 | */ |
||
66 | private $_busy; |
||
67 | /** |
||
68 | * @var TImage that shows a green check mark for completed upload. |
||
69 | */ |
||
70 | private $_success; |
||
71 | /** |
||
72 | * @var TImage that shows a red X for incomplete upload. |
||
73 | */ |
||
74 | private $_error; |
||
75 | /** |
||
76 | * @var TInlineFrame used to submit the data in an "asynchronous" fashion. |
||
77 | */ |
||
78 | private $_target; |
||
79 | |||
80 | |||
81 | /** |
||
82 | * Creates a new callback control, sets the adapter to |
||
83 | * TActiveControlAdapter. If you override this class, be sure to set the |
||
84 | * adapter appropriately by, for example, by calling this constructor. |
||
85 | */ |
||
86 | public function __construct(){ |
||
90 | |||
91 | |||
92 | /** |
||
93 | * @param string asset file in the self::SCRIPT_PATH directory. |
||
94 | * @return string asset file url. |
||
95 | */ |
||
96 | protected function getAssetUrl($file='') |
||
101 | |||
102 | |||
103 | /** |
||
104 | * This method is invoked when a file is uploaded. |
||
105 | * If you override this method, be sure to call the parent implementation to ensure |
||
106 | * the invocation of the attached event handlers. |
||
107 | * @param TEventParameter event parameter to be passed to the event handlers |
||
108 | */ |
||
109 | public function onFileUpload($param) |
||
142 | |||
143 | /** |
||
144 | * @return string the path where the uploaded file will be stored temporarily, in namespace format |
||
145 | * default "Application.runtime.*" |
||
146 | */ |
||
147 | public function getTempPath(){ |
||
150 | |||
151 | /** |
||
152 | * @param string the path where the uploaded file will be stored temporarily in namespace format |
||
153 | * default "Application.runtime.*" |
||
154 | */ |
||
155 | public function setTempPath($value){ |
||
158 | |||
159 | /** |
||
160 | * @return boolean a value indicating whether an automatic callback to the server will occur whenever the user modifies the text in the TTextBox control and then tabs out of the component. Defaults to true. |
||
161 | * Note: When set to false, you will need to trigger the callback yourself. |
||
162 | */ |
||
163 | public function getAutoPostBack(){ |
||
166 | |||
167 | /** |
||
168 | * @param boolean a value indicating whether an automatic callback to the server will occur whenever the user modifies the text in the TTextBox control and then tabs out of the component. Defaults to true. |
||
169 | * Note: When set to false, you will need to trigger the callback yourself. |
||
170 | */ |
||
171 | public function setAutoPostBack($value){ |
||
174 | |||
175 | /** |
||
176 | * @return string A chuck of javascript that will need to be called if {{@link getAutoPostBack AutoPostBack} is set to false} |
||
177 | */ |
||
178 | public function getCallbackJavascript(){ |
||
181 | |||
182 | /** |
||
183 | * @throws TInvalidDataValueException if the {@link getTempPath TempPath} is not writable. |
||
184 | */ |
||
185 | public function onInit($sender){ |
||
196 | |||
197 | /** |
||
198 | * Raises <b>OnFileUpload</b> event. |
||
199 | * |
||
200 | * This method is required by {@link ICallbackEventHandler} interface. |
||
201 | * This method is mainly used by framework and control developers. |
||
202 | * @param TCallbackEventParameter the event parameter |
||
203 | */ |
||
204 | public function raiseCallbackEvent($param){ |
||
220 | |||
221 | /** |
||
222 | * Raises postdata changed event. |
||
223 | * This method calls {@link onFileUpload} method |
||
224 | * This method is primarily used by framework developers. |
||
225 | */ |
||
226 | public function raisePostDataChangedEvent() |
||
230 | |||
231 | protected function pushParamsAndGetToken(TActiveFileUploadCallbackParams $params) |
||
250 | |||
251 | protected function popParamsByToken($token) |
||
273 | |||
274 | /** |
||
275 | * Publish the javascript |
||
276 | */ |
||
277 | public function onPreRender($param) |
||
310 | |||
311 | public function createChildControls() |
||
342 | |||
343 | /** |
||
344 | * Removes localfile on ending of the callback. |
||
345 | */ |
||
346 | public function onUnload($param){ |
||
354 | |||
355 | /** |
||
356 | * @return TBaseActiveCallbackControl standard callback control options. |
||
357 | */ |
||
358 | public function getActiveControl(){ |
||
361 | |||
362 | /** |
||
363 | * @return TCallbackClientSide client side request options. |
||
364 | */ |
||
365 | public function getClientSide() |
||
369 | |||
370 | /** |
||
371 | * Adds ID attribute, and renders the javascript for active component. |
||
372 | * @param THtmlWriter the writer used for the rendering purpose |
||
373 | */ |
||
374 | public function addAttributesToRender($writer){ |
||
381 | |||
382 | /** |
||
383 | * @return string corresponding javascript class name for this control. |
||
384 | */ |
||
385 | protected function getClientClassName(){ |
||
388 | |||
389 | /** |
||
390 | * Gets the client side options for this control. |
||
391 | * @return array ( inputID => input client ID, |
||
392 | * flagID => flag client ID, |
||
393 | * targetName => target unique ID, |
||
394 | * formID => form client ID, |
||
395 | * indicatorID => upload indicator client ID, |
||
396 | * completeID => complete client ID, |
||
397 | * errorID => error client ID) |
||
398 | */ |
||
399 | protected function getClientOptions(){ |
||
413 | |||
414 | /** |
||
415 | * Saves the uploaded file. |
||
416 | * @param string the file name used to save the uploaded file |
||
417 | * @param boolean whether to delete the temporary file after saving. |
||
418 | * If true, you will not be able to save the uploaded file again. |
||
419 | * @return boolean true if the file saving is successful |
||
420 | */ |
||
421 | public function saveAs($fileName,$deleteTempFile=true){ |
||
430 | |||
431 | /** |
||
432 | * @return TImage the image displayed when an upload |
||
433 | * completes successfully. |
||
434 | */ |
||
435 | public function getSuccessImage(){ |
||
439 | |||
440 | /** |
||
441 | * @return TImage the image displayed when an upload |
||
442 | * does not complete successfully. |
||
443 | */ |
||
444 | public function getErrorImage(){ |
||
448 | |||
449 | /** |
||
450 | * @return TImage the image displayed when an upload |
||
451 | * is in progress. |
||
452 | */ |
||
453 | public function getBusyImage(){ |
||
457 | } |
||
458 |
An exit expression should only be used in rare cases. For example, if you write a short command line script.
In most cases however, using an
exit
expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.