| Conditions | 16 |
| Paths | 5184 |
| Total Lines | 157 |
| Code Lines | 94 |
| Lines | 5 |
| Ratio | 3.18 % |
| 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 |
||
| 49 | public function __construct($target) |
||
| 50 | { |
||
| 51 | global $moduleHelper; |
||
| 52 | $this->targetObject = $target; |
||
| 53 | |||
| 54 | $title = $this->targetObject->isNew() ? sprintf(AM_EQUIPMENT_EQUIPMENT_ADD) : sprintf(AM_EQUIPMENT_EQUIPMENT_EDIT); |
||
| 55 | parent::__construct($title, 'form', xoops_getenv('PHP_SELF'), 'post', true); |
||
| 56 | $this->setExtra('enctype="multipart/form-data"'); |
||
| 57 | |||
| 58 | //include ID field, it's needed so the module knows if it is a new form or an edited form |
||
| 59 | |||
| 60 | $hidden = new XoopsFormHidden('id', $this->targetObject->getVar('id')); |
||
| 61 | $this->addElement($hidden); |
||
| 62 | unset($hidden); |
||
| 63 | |||
| 64 | // Id |
||
| 65 | $this->addElement(new XoopsFormLabel(AM_EQUIPMENT_EQUIPMENT_ID, $this->targetObject->getVar('id'), 'id')); |
||
| 66 | // Owner |
||
| 67 | $this->addElement(new XoopsFormText(AM_EQUIPMENT_EQUIPMENT_OWNER, 'owner', 50, 255, $this->targetObject->getVar('owner')), false); |
||
| 68 | // Name |
||
| 69 | $this->addElement(new XoopsFormText(AM_EQUIPMENT_EQUIPMENT_NAME, 'name', 50, 255, $this->targetObject->getVar('name')), false); |
||
| 70 | // Amount |
||
| 71 | $this->addElement(new XoopsFormText(AM_EQUIPMENT_EQUIPMENT_AMOUNT, 'amount', 50, 255, $this->targetObject->getVar('amount')), false); |
||
| 72 | // Total |
||
| 73 | $this->addElement(new XoopsFormText(AM_EQUIPMENT_EQUIPMENT_TOTAL, 'total', 50, 255, $this->targetObject->getVar('total')), false); |
||
| 74 | // Image |
||
| 75 | $image = $this->targetObject->getVar('image') ?: 'blank.png'; |
||
| 76 | |||
| 77 | $uploadDir = '/uploads/equipment/images/'; |
||
| 78 | $imgtray = new XoopsFormElementTray(AM_EQUIPMENT_EQUIPMENT_IMAGE, '<br>'); |
||
| 79 | $imgpath = sprintf(AM_EQUIPMENT_FORMIMAGE_PATH, $uploadDir); |
||
| 80 | $imageselect = new XoopsFormSelect($imgpath, 'image', $image); |
||
| 81 | $imageArray = XoopsLists::getImgListAsArray(XOOPS_ROOT_PATH . $uploadDir); |
||
| 82 | foreach ($imageArray as $image) { |
||
| 83 | $imageselect->addOption("$image", $image); |
||
| 84 | } |
||
| 85 | $imageselect->setExtra("onchange='showImgSelected(\"image_image\", \"image\", \"" . $uploadDir . '", "", "' . XOOPS_URL . "\")'"); |
||
| 86 | $imgtray->addElement($imageselect); |
||
| 87 | $imgtray->addElement(new XoopsFormLabel('', "<br><img src='" . XOOPS_URL . '/' . $uploadDir . '/' . $image . "' name='image_image' id='image_image' alt='' />")); |
||
| 88 | $fileseltray = new XoopsFormElementTray('', '<br>'); |
||
| 89 | $fileseltray->addElement(new XoopsFormFile(AM_EQUIPMENT_FORMUPLOAD, 'image', xoops_getModuleOption('maxsize'))); |
||
| 90 | $fileseltray->addElement(new XoopsFormLabel('')); |
||
| 91 | $imgtray->addElement($fileseltray); |
||
| 92 | $this->addElement($imgtray); |
||
| 93 | |||
| 94 | //permissions |
||
| 95 | /** @var XoopsMemberHandler $memberHandler */ |
||
| 96 | $memberHandler = xoops_getHandler('member'); |
||
| 97 | $groupList = $memberHandler->getGroupList(); |
||
| 98 | /** @var XoopsGroupPermHandler $gpermHandler */ |
||
| 99 | $gpermHandler = xoops_getHandler('groupperm'); |
||
| 100 | $fullList = array_keys($groupList); |
||
|
|
|||
| 101 | |||
| 102 | //======================================================================== |
||
| 103 | |||
| 104 | $mid = $GLOBALS['xoopsModule']->mid(); |
||
| 105 | $groupIdAdmin = 0; |
||
| 106 | $groupNameAdmin = ''; |
||
| 107 | |||
| 108 | // create admin checkbox |
||
| 109 | foreach ($groupList as $groupId => $groupName) { |
||
| 110 | if ($groupId == XOOPS_GROUP_ADMIN) { |
||
| 111 | $groupIdAdmin = $groupId; |
||
| 112 | $groupNameAdmin = $groupName; |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $selectPermAdmin = new XoopsFormCheckBox('', 'admin', XOOPS_GROUP_ADMIN); |
||
| 117 | $selectPermAdmin->addOption($groupIdAdmin, $groupNameAdmin); |
||
| 118 | $selectPermAdmin->setExtra("disabled='disabled'"); //comment it out, if you want to allow to remove permissions for the admin |
||
| 119 | |||
| 120 | // ******************************************************** |
||
| 121 | // permission view items |
||
| 122 | $cat_gperms_read = $gpermHandler->getGroupIds('equipment_view', $this->targetObject->getVar('id'), $mid); |
||
| 123 | $arr_cat_gperms_read = $this->targetObject->isNew() ? '0' : $cat_gperms_read; |
||
| 124 | |||
| 125 | $permsTray = new XoopsFormElementTray(AM_EQUIPMENT_PERMISSIONS_VIEW, ''); |
||
| 126 | |||
| 127 | $selectAllReadCheckbox = new XoopsFormCheckBox('', 'adminbox1', 1); |
||
| 128 | $selectAllReadCheckbox->addOption('allbox', _AM_SYSTEM_ALL); |
||
| 129 | $selectAllReadCheckbox->setExtra(" onclick='xoopsCheckGroup(\"form\", \"adminbox1\" , \"groupsRead[]\");' "); |
||
| 130 | $selectAllReadCheckbox->setClass('xo-checkall'); |
||
| 131 | $permsTray->addElement($selectAllReadCheckbox); |
||
| 132 | |||
| 133 | // checkbox webmaster |
||
| 134 | $permsTray->addElement($selectPermAdmin, false); |
||
| 135 | // checkboxes other groups |
||
| 136 | //$selectPerm = new XoopsFormCheckBox('', 'cat_gperms_read', $arr_cat_gperms_read); |
||
| 137 | //$selectPerm = new XoopsFormCheckBox('', 'groupsRead[]', $this->targetObject->getGroupsRead()); |
||
| 138 | $selectPerm = new XoopsFormCheckBox('', 'groupsRead[]', $arr_cat_gperms_read); |
||
| 139 | foreach ($groupList as $groupId => $groupName) { |
||
| 140 | if ($groupId != XOOPS_GROUP_ADMIN) { |
||
| 141 | $selectPerm->addOption($groupId, $groupName); |
||
| 142 | } |
||
| 143 | } |
||
| 144 | $permsTray->addElement($selectPerm, false); |
||
| 145 | $this->addElement($permsTray, false); |
||
| 146 | unset($permsTray, $selectPerm); |
||
| 147 | |||
| 148 | // ******************************************************** |
||
| 149 | // permission submit item |
||
| 150 | $cat_gperms_create = $gpermHandler->getGroupIds('equipment_submit', $this->targetObject->getVar('id'), $mid); |
||
| 151 | $arr_cat_gperms_create = $this->targetObject->isNew() ? '0' : $cat_gperms_create; |
||
| 152 | |||
| 153 | $permsTray = new XoopsFormElementTray(AM_EQUIPMENT_PERMISSIONS_SUBMIT, ''); |
||
| 154 | |||
| 155 | $selectAllSubmitCheckbox = new XoopsFormCheckBox('', 'adminbox2', 1); |
||
| 156 | $selectAllSubmitCheckbox->addOption('allbox', _AM_SYSTEM_ALL); |
||
| 157 | $selectAllSubmitCheckbox->setExtra(" onclick='xoopsCheckGroup(\"form\", \"adminbox2\" , \"groupsSubmit[]\");' "); |
||
| 158 | $selectAllSubmitCheckbox->setClass('xo-checkall'); |
||
| 159 | $permsTray->addElement($selectAllSubmitCheckbox); |
||
| 160 | |||
| 161 | // checkbox webmaster |
||
| 162 | $permsTray->addElement($selectPermAdmin, false); |
||
| 163 | // checkboxes other groups |
||
| 164 | //$selectPerm = new XoopsFormCheckBox('', 'cat_gperms_create', $arr_cat_gperms_create); |
||
| 165 | $selectPerm = new XoopsFormCheckBox('', 'groupsSubmit[]', $arr_cat_gperms_create); |
||
| 166 | foreach ($groupList as $groupId => $groupName) { |
||
| 167 | if ($groupId != XOOPS_GROUP_ADMIN) { |
||
| 168 | $selectPerm->addOption($groupId, $groupName); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | $permsTray->addElement($selectPerm, false); |
||
| 172 | $this->addElement($permsTray, false); |
||
| 173 | unset($permsTray, $selectPerm); |
||
| 174 | |||
| 175 | // ******************************************************** |
||
| 176 | // permission approve items |
||
| 177 | $cat_gperms_admin = $gpermHandler->getGroupIds('equipment_approve', $this->targetObject->getVar('id'), $mid); |
||
| 178 | $arr_cat_gperms_admin = $this->targetObject->isNew() ? '0' : $cat_gperms_admin; |
||
| 179 | |||
| 180 | $permsTray = new XoopsFormElementTray(AM_EQUIPMENT_PERMISSIONS_APPROVE, ''); |
||
| 181 | |||
| 182 | $selectAllModerateCheckbox = new XoopsFormCheckBox('', 'adminbox3', 1); |
||
| 183 | $selectAllModerateCheckbox->addOption('allbox', _AM_SYSTEM_ALL); |
||
| 184 | $selectAllModerateCheckbox->setExtra(" onclick='xoopsCheckGroup(\"form\", \"adminbox3\" , \"groupsModeration[]\");' "); |
||
| 185 | $selectAllModerateCheckbox->setClass('xo-checkall'); |
||
| 186 | $permsTray->addElement($selectAllModerateCheckbox); |
||
| 187 | |||
| 188 | // checkbox webmaster |
||
| 189 | $permsTray->addElement($selectPermAdmin, false); |
||
| 190 | // checkboxes other groups |
||
| 191 | //$selectPerm = new XoopsFormCheckBox('', 'cat_gperms_admin', $arr_cat_gperms_admin); |
||
| 192 | $selectPerm = new XoopsFormCheckBox('', 'groupsModeration[]', $arr_cat_gperms_admin); |
||
| 193 | View Code Duplication | foreach ($groupList as $groupId => $groupName) { |
|
| 194 | if ($groupId != XOOPS_GROUP_ADMIN && $groupId != XOOPS_GROUP_ANONYMOUS) { |
||
| 195 | $selectPerm->addOption($groupId, $groupName); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | $permsTray->addElement($selectPerm, false); |
||
| 199 | $this->addElement($permsTray, false); |
||
| 200 | unset($permsTray, $selectPerm); |
||
| 201 | |||
| 202 | //========================================================================= |
||
| 203 | $this->addElement(new XoopsFormHidden('op', 'save')); |
||
| 204 | $this->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit')); |
||
| 205 | } |
||
| 206 | } |
||
| 207 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.