| Conditions | 16 |
| Paths | 8712 |
| Total Lines | 74 |
| 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 |
||
| 68 | public function getForm($action = false, $system = 0) |
||
| 69 | { |
||
| 70 | global $xoopsDB; |
||
| 71 | |||
| 72 | if (false === $action) { |
||
| 73 | $action = $_SERVER['REQUEST_URI']; |
||
| 74 | } |
||
| 75 | |||
| 76 | $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_MAILINGLIST_ADD) : sprintf(_AM_XNEWSLETTER_MAILINGLIST_EDIT); |
||
| 77 | |||
| 78 | require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
| 79 | $form = new \XoopsThemeForm($title, 'xn_ml_form', $action, 'post', true); |
||
| 80 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 81 | |||
| 82 | $form->addElement(new \XoopsFormHidden('mailinglist_system', $system)); |
||
| 83 | |||
| 84 | if ($system === _XNEWSLETTER_MAILINGLIST_TYPE_MAILMAN_VAL) { |
||
| 85 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_MAILINGLIST_SYSTEM, _AM_XNEWSLETTER_MAILINGLIST_SYSTEM_MAILMAN)); |
||
| 86 | |||
| 87 | $mailinglist_name = $this->isNew() ? 'myname' : $this->getVar('mailinglist_name'); |
||
| 88 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_NAME, 'mailinglist_name', 50, 255, $mailinglist_name), true); |
||
| 89 | |||
| 90 | $mailinglist_listname = $this->isNew() ? 'nameofmylist' : $this->getVar('mailinglist_listname'); |
||
| 91 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_LISTNAME, 'mailinglist_listname', 50, 255, $mailinglist_listname), true); |
||
| 92 | |||
| 93 | $form->addElement(new \XoopsFormHidden('mailinglist_email', '')); |
||
| 94 | $form->addElement(new \XoopsFormHidden('mailinglist_subscribe', '')); |
||
| 95 | $form->addElement(new \XoopsFormHidden('mailinglist_unsubscribe', '')); |
||
| 96 | |||
| 97 | $mailinglist_target = $this->isNew() ? 'https://lists.mydomain.com' : $this->getVar('mailinglist_target'); |
||
| 98 | $form->addElement(new \XoopsFormText( _AM_XNEWSLETTER_MAILINGLIST_TARGET, 'mailinglist_target', 50, 255, $mailinglist_target)); |
||
| 99 | |||
| 100 | $mailinglist_pwd = $this->isNew() ? '' : $this->getVar('mailinglist_pwd'); |
||
| 101 | $form->addElement(new \XoopsFormText( _AM_XNEWSLETTER_MAILINGLIST_PWD, 'mailinglist_pwd', 50, 255, $mailinglist_pwd)); |
||
| 102 | |||
| 103 | $mailinglist_notifyowner = $this->isNew() ? 1 : $this->getVar('mailinglist_notifyowner'); |
||
| 104 | $form->addElement(new \XoopsFormRadioYN(_AM_XNEWSLETTER_MAILINGLIST_NOTIFYOWNER, 'mailinglist_notifyowner', $mailinglist_notifyowner, _YES, _NO)); |
||
| 105 | } |
||
| 106 | if ($system === _XNEWSLETTER_MAILINGLIST_TYPE_MAJORDOMO_VAL) { |
||
| 107 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_MAILINGLIST_SYSTEM, _AM_XNEWSLETTER_MAILINGLIST_SYSTEM_MAJORDOMO)); |
||
| 108 | |||
| 109 | $mailinglist_name = $this->isNew() ? 'myname' : $this->getVar('mailinglist_name'); |
||
| 110 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_NAME, 'mailinglist_name', 50, 255, $mailinglist_name), true); |
||
| 111 | |||
| 112 | $mailinglist_listname = $this->isNew() ? 'nameofmylist' : $this->getVar('mailinglist_listname'); |
||
| 113 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_LISTNAME, 'mailinglist_listname', 50, 255, $mailinglist_listname), true); |
||
| 114 | |||
| 115 | $mailinglist_email = $this->isNew() ? '[email protected]' : $this->getVar('mailinglist_email'); |
||
| 116 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_EMAIL_DESC, 'mailinglist_email', 50, 255, $mailinglist_email)); |
||
| 117 | |||
| 118 | $mailinglist_subscribe = $this->isNew() ? 'subscribe nameofmylist {email}' : $this->getVar('mailinglist_subscribe'); |
||
| 119 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_SUBSCRIBE . "<br><span style='font-size:0,75em'>" . _AM_XNEWSLETTER_MAILINGLIST_SUBSCRIBE_DESC . '</span>', 'mailinglist_subscribe', 50, 255, $mailinglist_subscribe)); |
||
| 120 | |||
| 121 | $mailinglist_unsubscribe = $this->isNew() ? 'unsubscribe nameofmylist {email}' : $this->getVar('mailinglist_unsubscribe'); |
||
| 122 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_UNSUBSCRIBE . "<br><span style='font-size:0,75em'>" . _AM_XNEWSLETTER_MAILINGLIST_SUBSCRIBE_DESC . '</span>', 'mailinglist_unsubscribe', 50, 255, $mailinglist_unsubscribe)); |
||
| 123 | |||
| 124 | $form->addElement(new \XoopsFormHidden('mailinglist_target', '')); |
||
| 125 | $form->addElement(new \XoopsFormHidden('mailinglist_pwd', '')); |
||
| 126 | $form->addElement(new \XoopsFormHidden('mailinglist_notifyowner', 0)); |
||
| 127 | } |
||
| 128 | |||
| 129 | $time = $this->isNew() ? time() : $this->getVar('mailinglist_created'); |
||
| 130 | $form->addElement(new \XoopsFormHidden('mailinglist_submitter', $GLOBALS['xoopsUser']->uid())); |
||
| 131 | $form->addElement(new \XoopsFormHidden('mailinglist_created', $time)); |
||
| 132 | |||
| 133 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBMITTER, $GLOBALS['xoopsUser']->uname())); |
||
| 134 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_CREATED, formatTimestamp($time, 's'))); |
||
| 135 | |||
| 136 | $form->addElement(new \XoopsFormHidden('mailinglist_id', $this->getVar('mailinglist_id'))); |
||
| 137 | $form->addElement(new \XoopsFormHidden('op', 'save_mailinglist')); |
||
| 138 | $form->addElement(new \XoopsFormButtonTray('save', _SUBMIT, 'submit', '', false)); |
||
| 139 | |||
| 140 | return $form; |
||
| 141 | } |
||
| 142 | |||
| 180 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.