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 |
||
69 | public function getForm($action = false, $system = 0) |
||
70 | { |
||
71 | global $xoopsDB; |
||
72 | |||
73 | if (false === $action) { |
||
74 | $action = $_SERVER['REQUEST_URI']; |
||
75 | } |
||
76 | |||
77 | $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_MAILINGLIST_ADD) : sprintf(_AM_XNEWSLETTER_MAILINGLIST_EDIT); |
||
78 | |||
79 | require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
80 | $form = new \XoopsThemeForm($title, 'xn_ml_form', $action, 'post', true); |
||
81 | $form->setExtra('enctype="multipart/form-data"'); |
||
82 | |||
83 | $form->addElement(new \XoopsFormHidden('mailinglist_system', $system)); |
||
84 | |||
85 | if ($system === _XNEWSLETTER_MAILINGLIST_TYPE_MAILMAN_VAL) { |
||
86 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_MAILINGLIST_SYSTEM, _AM_XNEWSLETTER_MAILINGLIST_SYSTEM_MAILMAN)); |
||
87 | |||
88 | $mailinglist_name = $this->isNew() ? 'myname' : $this->getVar('mailinglist_name'); |
||
89 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_NAME, 'mailinglist_name', 50, 255, $mailinglist_name), true); |
||
90 | |||
91 | $mailinglist_listname = $this->isNew() ? 'nameofmylist' : $this->getVar('mailinglist_listname'); |
||
92 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_LISTNAME, 'mailinglist_listname', 50, 255, $mailinglist_listname), true); |
||
93 | |||
94 | $form->addElement(new \XoopsFormHidden('mailinglist_email', '')); |
||
95 | $form->addElement(new \XoopsFormHidden('mailinglist_subscribe', '')); |
||
96 | $form->addElement(new \XoopsFormHidden('mailinglist_unsubscribe', '')); |
||
97 | |||
98 | $mailinglist_target = $this->isNew() ? 'https://lists.mydomain.com' : $this->getVar('mailinglist_target'); |
||
99 | $form->addElement(new \XoopsFormText( _AM_XNEWSLETTER_MAILINGLIST_TARGET, 'mailinglist_target', 50, 255, $mailinglist_target)); |
||
100 | |||
101 | $mailinglist_pwd = $this->isNew() ? '' : $this->getVar('mailinglist_pwd'); |
||
102 | $form->addElement(new \XoopsFormText( _AM_XNEWSLETTER_MAILINGLIST_PWD, 'mailinglist_pwd', 50, 255, $mailinglist_pwd)); |
||
103 | |||
104 | $mailinglist_notifyowner = $this->isNew() ? 1 : $this->getVar('mailinglist_notifyowner'); |
||
105 | $form->addElement(new \XoopsFormRadioYN(_AM_XNEWSLETTER_MAILINGLIST_NOTIFYOWNER, 'mailinglist_notifyowner', $mailinglist_notifyowner, _YES, _NO)); |
||
106 | } |
||
107 | if ($system === _XNEWSLETTER_MAILINGLIST_TYPE_MAJORDOMO_VAL) { |
||
108 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_MAILINGLIST_SYSTEM, _AM_XNEWSLETTER_MAILINGLIST_SYSTEM_MAJORDOMO)); |
||
109 | |||
110 | $mailinglist_name = $this->isNew() ? 'myname' : $this->getVar('mailinglist_name'); |
||
111 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_NAME, 'mailinglist_name', 50, 255, $mailinglist_name), true); |
||
112 | |||
113 | $mailinglist_listname = $this->isNew() ? 'nameofmylist' : $this->getVar('mailinglist_listname'); |
||
114 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_LISTNAME, 'mailinglist_listname', 50, 255, $mailinglist_listname), true); |
||
115 | |||
116 | $mailinglist_email = $this->isNew() ? '[email protected]' : $this->getVar('mailinglist_email'); |
||
117 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_MAILINGLIST_EMAIL_DESC, 'mailinglist_email', 50, 255, $mailinglist_email)); |
||
118 | |||
119 | $mailinglist_subscribe = $this->isNew() ? 'subscribe nameofmylist {email}' : $this->getVar('mailinglist_subscribe'); |
||
120 | $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)); |
||
121 | |||
122 | $mailinglist_unsubscribe = $this->isNew() ? 'unsubscribe nameofmylist {email}' : $this->getVar('mailinglist_unsubscribe'); |
||
123 | $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)); |
||
124 | |||
125 | $form->addElement(new \XoopsFormHidden('mailinglist_target', '')); |
||
126 | $form->addElement(new \XoopsFormHidden('mailinglist_pwd', '')); |
||
127 | $form->addElement(new \XoopsFormHidden('mailinglist_notifyowner', 0)); |
||
128 | } |
||
129 | |||
130 | $time = $this->isNew() ? time() : $this->getVar('mailinglist_created'); |
||
131 | $form->addElement(new \XoopsFormHidden('mailinglist_submitter', $GLOBALS['xoopsUser']->uid())); |
||
132 | $form->addElement(new \XoopsFormHidden('mailinglist_created', $time)); |
||
133 | |||
134 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBMITTER, $GLOBALS['xoopsUser']->uname())); |
||
135 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_CREATED, formatTimestamp($time, 's'))); |
||
136 | |||
137 | $form->addElement(new \XoopsFormHidden('mailinglist_id', $this->getVar('mailinglist_id'))); |
||
138 | $form->addElement(new \XoopsFormHidden('op', 'save_mailinglist')); |
||
139 | $form->addElement(new \XoopsFormButtonTray('save', _SUBMIT, 'submit', '', false)); |
||
140 | |||
141 | return $form; |
||
142 | } |
||
143 | |||
181 |
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key 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.