| Conditions | 10 |
| Paths | 256 |
| Total Lines | 107 |
| Code Lines | 60 |
| 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 |
||
| 145 | public function returnForm($url, $action, $sessionInfo = []) |
||
| 146 | { |
||
| 147 | // Setting the form elements |
||
| 148 | $header = get_lang('Add'); |
||
| 149 | |||
| 150 | if ($action == 'edit') { |
||
| 151 | $header = get_lang('Modify'); |
||
| 152 | } |
||
| 153 | |||
| 154 | $form = new FormValidator( |
||
| 155 | 'announcement', |
||
| 156 | 'post', |
||
| 157 | $url |
||
| 158 | ); |
||
| 159 | |||
| 160 | $form->addHeader($header); |
||
| 161 | if ($action == 'add') { |
||
| 162 | $form->addHtml( |
||
| 163 | Display::return_message( |
||
| 164 | nl2br(get_lang('ScheduleAnnouncementDescription')), |
||
| 165 | 'normal', |
||
| 166 | false |
||
| 167 | ) |
||
| 168 | ); |
||
| 169 | } |
||
| 170 | $form->addHidden('session_id', $sessionInfo['id']); |
||
| 171 | |||
| 172 | $useBaseDate = false; |
||
| 173 | $startDate = $sessionInfo['access_start_date']; |
||
| 174 | $endDate = $sessionInfo['access_end_date']; |
||
| 175 | |||
| 176 | if (!empty($startDate) || !empty($endDate)) { |
||
| 177 | $useBaseDate = true; |
||
| 178 | } |
||
| 179 | |||
| 180 | $typeOptions = [ |
||
| 181 | 'specific_date' => get_lang('SpecificDate') |
||
| 182 | ]; |
||
| 183 | |||
| 184 | if ($useBaseDate) { |
||
| 185 | $typeOptions['base_date'] = get_lang('BaseDate'); |
||
| 186 | } |
||
| 187 | |||
| 188 | $form->addSelect( |
||
| 189 | 'type', |
||
| 190 | get_lang('Type'), |
||
| 191 | $typeOptions, |
||
| 192 | [ |
||
| 193 | 'onchange' => "javascript: |
||
| 194 | if (this.options[this.selectedIndex].value == 'base_date') { |
||
| 195 | document.getElementById('options').style.display = 'block'; |
||
| 196 | document.getElementById('specific_date').style.display = 'none'; |
||
| 197 | } else { |
||
| 198 | document.getElementById('options').style.display = 'none'; |
||
| 199 | document.getElementById('specific_date').style.display = 'block'; |
||
| 200 | } |
||
| 201 | "] |
||
| 202 | ); |
||
| 203 | |||
| 204 | $form->addElement('html', '<div id="specific_date">'); |
||
| 205 | $form->addDateTimePicker('date', get_lang('Date')); |
||
| 206 | $form->addElement('html', '</div>'); |
||
| 207 | $form->addElement('html', '<div id="options" style="display:none">'); |
||
| 208 | |||
| 209 | $startDate = $sessionInfo['access_start_date']; |
||
| 210 | $endDate = $sessionInfo['access_end_date']; |
||
| 211 | |||
| 212 | $form->addText( |
||
| 213 | 'days', |
||
| 214 | get_lang('Days'), |
||
| 215 | false |
||
| 216 | ); |
||
| 217 | |||
| 218 | $form->addSelect( |
||
| 219 | 'moment_type', |
||
| 220 | get_lang('AfterOrBefore'), |
||
| 221 | [ |
||
| 222 | 'after' => get_lang('After'), |
||
| 223 | 'before' => get_lang('Before'), |
||
| 224 | ] |
||
| 225 | ); |
||
| 226 | |||
| 227 | if (!empty($startDate)) { |
||
| 228 | $options['start_date'] = get_lang('StartDate').' - '.$startDate; |
||
| 229 | } |
||
| 230 | |||
| 231 | if (!empty($endDate)) { |
||
| 232 | $options['end_date'] = get_lang('EndDate').' - '.$endDate; |
||
| 233 | } |
||
| 234 | if (!empty($options)) { |
||
| 235 | $form->addSelect('base_date', get_lang('BaseDate'), $options); |
||
| 236 | } |
||
| 237 | |||
| 238 | $form->addElement('html', '</div>'); |
||
| 239 | |||
| 240 | $form->addText('subject', get_lang('Subject')); |
||
| 241 | $form->addHtmlEditor('message', get_lang('Message')); |
||
| 242 | $this->setTagsInForm($form); |
||
| 243 | |||
| 244 | if ($action == 'edit') { |
||
| 245 | $form->addButtonUpdate(get_lang('Modify')); |
||
| 246 | } else { |
||
| 247 | $form->addButtonCreate(get_lang('Add')); |
||
| 248 | } |
||
| 249 | |||
| 250 | return $form; |
||
| 251 | } |
||
| 252 | |||
| 376 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.