Conditions | 11 |
Paths | 28 |
Total Lines | 46 |
Code Lines | 26 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
71 | public function processCreation($form, $exercise) |
||
72 | { |
||
73 | $listFile = $form->getSubmitValue('list_file'); |
||
74 | $listText = $form->getSubmitValue('list_text'); |
||
75 | |||
76 | parent::processCreation($form, $exercise); |
||
77 | |||
78 | $questionId = 0; |
||
79 | if (!empty($this->iid)) { |
||
80 | $questionId = (int) $this->iid; |
||
81 | } elseif (property_exists($this, 'iId') && !empty($this->iId)) { |
||
82 | $questionId = (int) $this->iId; |
||
83 | } elseif (!empty($this->id)) { |
||
84 | // Fallback in case your build exposes "id" |
||
85 | $questionId = (int) $this->id; |
||
86 | } |
||
87 | |||
88 | // Safety net: if we still don't have a question id, abort with a clear message |
||
89 | if ($questionId <= 0) { |
||
90 | throw new \RuntimeException('Question id was not generated; cannot save dropdown options.'); |
||
91 | } |
||
92 | |||
93 | // Build the lines array (either CSV first column, or textarea lines) |
||
94 | $lines = []; |
||
95 | if (is_array($listFile) && isset($listFile['error']) && (int) $listFile['error'] === UPLOAD_ERR_OK) { |
||
96 | $lines = Import::csvColumnToArray($listFile['tmp_name']); // reads first column only |
||
97 | } elseif (!empty($listText)) { |
||
98 | $lines = explode("\n", $listText); |
||
99 | } |
||
100 | // Normalize: trim, drop empty |
||
101 | $lines = array_values(array_filter(array_map('trim', $lines), static fn ($v) => $v !== '')); |
||
102 | |||
103 | // Create Answer bound to this question + course + exercise |
||
104 | $course = api_get_course_info(); |
||
105 | $courseId = (int) ($course['real_id'] ?? 0); |
||
106 | $objAnswer = new Answer($questionId, $courseId, $exercise, false); |
||
107 | |||
108 | // Fill answers (dropdown typically has no "correct" by default) |
||
109 | $pos = 1; |
||
110 | foreach ($lines as $line) { |
||
111 | $objAnswer->createAnswer($line, 0, '', 0, $pos); |
||
112 | $pos++; |
||
113 | } |
||
114 | |||
115 | // 6) Persist answers |
||
116 | $objAnswer->save(); |
||
117 | } |
||
156 |