| Conditions | 28 |
| Paths | 2304 |
| Total Lines | 100 |
| Code Lines | 57 |
| Lines | 30 |
| Ratio | 30 % |
| 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 |
||
| 168 | public function preSave($formData) |
||
| 169 | { |
||
| 170 | $counter = Session::read('answer_count'); |
||
| 171 | $answerList = Session::read('answer_list'); |
||
| 172 | |||
| 173 | if (empty($answerList)) { |
||
| 174 | $answerList = isset($formData['answers']) ? $formData['answers'] : array(); |
||
| 175 | Session::write('answer_list', $answerList); |
||
| 176 | } |
||
| 177 | |||
| 178 | if (isset($_POST['answers'])) { |
||
| 179 | $formData['answers'] = $_POST['answers']; |
||
| 180 | } |
||
| 181 | |||
| 182 | if (empty($counter)) { |
||
| 183 | $counter = count($answerList) - 1; |
||
| 184 | Session::write('answer_count', $counter); |
||
| 185 | } |
||
| 186 | |||
| 187 | // Moving an answer up |
||
| 188 | View Code Duplication | if (isset($_POST['move_up']) && $_POST['move_up']) { |
|
| 189 | foreach ($_POST['move_up'] as $key => & $value) { |
||
| 190 | $id1 = $key; |
||
| 191 | $content1 = $formData['answers'][$id1]; |
||
| 192 | $id2 = $key - 1; |
||
| 193 | $content2 = $formData['answers'][$id2]; |
||
| 194 | $formData['answers'][$id1] = $content2; |
||
| 195 | $formData['answers'][$id2] = $content1; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | // Moving an answer down |
||
| 200 | View Code Duplication | if (isset($_POST['move_down']) && $_POST['move_down']) { |
|
| 201 | foreach ($_POST['move_down'] as $key => & $value) { |
||
| 202 | $id1 = $key; |
||
| 203 | $content1 = $formData['answers'][$id1]; |
||
| 204 | $id2 = $key + 1; |
||
| 205 | $content2 = $formData['answers'][$id2]; |
||
| 206 | $formData['answers'][$id1] = $content2; |
||
| 207 | $formData['answers'][$id2] = $content1; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * This solution is a little bit strange but I could not find a different solution. |
||
| 213 | */ |
||
| 214 | if (isset($_POST['delete_answer'])) { |
||
| 215 | $deleted = false; |
||
| 216 | foreach ($_POST['delete_answer'] as $key => & $value) { |
||
| 217 | $deleted = $key; |
||
| 218 | $counter--; |
||
| 219 | Session::write('answer_count', $counter); |
||
| 220 | } |
||
| 221 | |||
| 222 | foreach ($formData['answers'] as $key => & $value) { |
||
| 223 | if ($key > $deleted) { |
||
| 224 | $formData['answers'][$key - 1] = $formData['answers'][$key]; |
||
| 225 | unset($formData['answers'][$key]); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | // Adding an answer |
||
| 231 | if (isset($_POST['buttons']) && isset($_POST['buttons']['add_answer'])) { |
||
| 232 | $counter++; |
||
| 233 | Session::write('answer_count', $counter); |
||
| 234 | } |
||
| 235 | |||
| 236 | // Removing an answer |
||
| 237 | if (isset($_POST['buttons']) && isset($_POST['buttons']['remove_answer'])) { |
||
| 238 | $counter--; |
||
| 239 | Session::write('answer_count', $counter); |
||
| 240 | View Code Duplication | foreach ($formData['answers'] as $index => &$data) { |
|
| 241 | if ($index > $counter) { |
||
| 242 | unset($formData['answers'][$index]); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | if (!isset($_POST['delete_answer'])) { |
||
| 248 | if (isset($formData['answers'])) { |
||
| 249 | View Code Duplication | foreach ($formData['answers'] as $index => $data) { |
|
| 250 | if ($index > $counter) { |
||
| 251 | unset($formData['answers'][$index]); |
||
| 252 | } |
||
| 253 | } |
||
| 254 | |||
| 255 | for ($i = 0; $i <= $counter; $i++) { |
||
| 256 | if (!isset($formData['answers'][$i])) { |
||
| 257 | $formData['answers'][$i] = ''; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | $formData['answers'] = isset($formData['answers']) ? $formData['answers'] : []; |
||
| 264 | Session::write('answer_list', $formData['answers']); |
||
| 265 | |||
| 266 | return $formData; |
||
| 267 | } |
||
| 268 | |||
| 335 |