Conditions | 6 |
Paths | 24 |
Total Lines | 95 |
Code Lines | 59 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
27 | function editfile($showmenu = false, $fileid = 0, $id = 0) |
||
28 | { |
||
29 | global $smartPartnerFileHandler, $xoopsModule; |
||
30 | |||
31 | include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
32 | // if there is a parameter, and the id exists, retrieve data: we're editing a file |
||
33 | if ($fileid != 0) { |
||
34 | |||
35 | // Creating the File object |
||
36 | $fileObj = new smartpartnerfile($fileid); |
||
37 | |||
38 | if ($fileObj->notLoaded()) { |
||
39 | redirect_header('javascript:history.go(-1)', 1, _AM_SPARTNER_NOFILESELECTED); |
||
40 | } |
||
41 | |||
42 | echo "<br>\n"; |
||
43 | echo "<span style='color: #2F5376; font-weight: bold; font-size: 16px; margin: 6px 6px 0 0; '>" . _AM_SPARTNER_FILE_EDITING . '</span>'; |
||
44 | echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . _AM_SPARTNER_FILE_EDITING_DSC . '</span>'; |
||
45 | smartpartner_collapsableBar('editfile', 'editfileicon', _AM_SPARTNER_FILE_INFORMATIONS); |
||
46 | } else { |
||
47 | // there's no parameter, so we're adding an item |
||
48 | $fileObj = $smartPartnerFileHandler->create(); |
||
49 | $fileObj->setVar('id', $id); |
||
50 | |||
51 | echo "<span style='color: #2F5376; font-weight: bold; font-size: 16px; margin: 6px 6px 0 0; '>" . _AM_SPARTNER_FILE_ADDING . '</span>'; |
||
52 | echo "<span style=\"color: #567; margin: 3px 0 12px 0; font-size: small; display: block; \">" . _AM_SPARTNER_FILE_ADDING_DSC . '</span>'; |
||
53 | smartpartner_collapsableBar('addfile', 'addfileicon', _AM_SPARTNER_FILE_INFORMATIONS); |
||
54 | } |
||
55 | |||
56 | // FILES UPLOAD FORM |
||
57 | $files_form = new XoopsThemeForm(_AM_SPARTNER_UPLOAD_FILE, 'files_form', xoops_getenv('PHP_SELF')); |
||
58 | $files_form->setExtra("enctype='multipart/form-data'"); |
||
59 | |||
60 | // NAME |
||
61 | $name_text = new XoopsFormText(_AM_SPARTNER_FILE_NAME, 'name', 50, 255, $fileObj->name()); |
||
62 | $name_text->setDescription(_AM_SPARTNER_FILE_NAME_DSC); |
||
63 | $files_form->addElement($name_text, true); |
||
64 | |||
65 | // DESCRIPTION |
||
66 | $description_text = new XoopsFormTextArea(_AM_SPARTNER_FILE_DESCRIPTION, 'description', $fileObj->description()); |
||
67 | $description_text->setDescription(_AM_SPARTNER_FILE_DESCRIPTION_DSC); |
||
68 | $files_form->addElement($description_text); |
||
69 | |||
70 | // FILE TO UPLOAD |
||
71 | if ($fileid == 0) { |
||
72 | $file_box = new XoopsFormFile(_AM_SPARTNER_FILE_TO_UPLOAD, 'userfile', 0); |
||
73 | $file_box->setExtra("size ='50'"); |
||
74 | $files_form->addElement($file_box); |
||
75 | } |
||
76 | |||
77 | $status_select = new XoopsFormRadioYN(_AM_SPARTNER_FILE_STATUS, 'file_status', $fileObj->status()); |
||
78 | $status_select->setDescription(_AM_SPARTNER_FILE_STATUS_DSC); |
||
79 | $files_form->addElement($status_select); |
||
80 | |||
81 | $files_button_tray = new XoopsFormElementTray('', ''); |
||
82 | $files_hidden = new XoopsFormHidden('op', 'uploadfile'); |
||
83 | $files_button_tray->addElement($files_hidden); |
||
84 | |||
85 | if ($fileid == 0) { |
||
86 | $files_butt_create = new XoopsFormButton('', '', _AM_SPARTNER_UPLOAD, 'submit'); |
||
87 | $files_butt_create->setExtra('onclick="this.form.elements.op.value=\'uploadfile\'"'); |
||
88 | $files_button_tray->addElement($files_butt_create); |
||
89 | |||
90 | $files_butt_another = new XoopsFormButton('', '', _AM_SPARTNER_FILE_UPLOAD_ANOTHER, 'submit'); |
||
91 | $files_butt_another->setExtra('onclick="this.form.elements.op.value=\'uploadanother\'"'); |
||
92 | $files_button_tray->addElement($files_butt_another); |
||
93 | } else { |
||
94 | $files_butt_create = new XoopsFormButton('', '', _AM_SPARTNER_MODIFY, 'submit'); |
||
95 | $files_butt_create->setExtra('onclick="this.form.elements.op.value=\'modify\'"'); |
||
96 | $files_button_tray->addElement($files_butt_create); |
||
97 | } |
||
98 | |||
99 | $files_butt_clear = new XoopsFormButton('', '', _AM_SPARTNER_CLEAR, 'reset'); |
||
100 | $files_button_tray->addElement($files_butt_clear); |
||
101 | |||
102 | $butt_cancel = new XoopsFormButton('', '', _AM_SPARTNER_CANCEL, 'button'); |
||
103 | $butt_cancel->setExtra('onclick="history.go(-1)"'); |
||
104 | $files_button_tray->addElement($butt_cancel); |
||
105 | |||
106 | $files_form->addElement($files_button_tray); |
||
107 | |||
108 | // fileid |
||
109 | $files_form->addElement(new XoopsFormHidden('fileid', $fileid)); |
||
110 | |||
111 | // id |
||
112 | $files_form->addElement(new XoopsFormHidden('id', $id)); |
||
113 | |||
114 | $files_form->display(); |
||
115 | |||
116 | if ($fileid != 0) { |
||
117 | smartpartner_close_collapsable('editfile', 'editfileicon'); |
||
118 | } else { |
||
119 | smartpartner_close_collapsable('addfile', 'addfileicon'); |
||
120 | } |
||
121 | } |
||
122 | |||
305 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.