| Conditions | 21 |
| Paths | 7 |
| Total Lines | 104 |
| Code Lines | 77 |
| 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 |
||
| 202 | protected static function getSequence(array $objItems, ?int $categoryId = null, ?string $itemType = null, ?string $coursecode = null, ?array $itemQuestions = null) |
||
| 203 | { |
||
| 204 | $sequences = []; |
||
| 205 | switch ($itemType) { |
||
| 206 | case 'quiz': |
||
| 207 | $sequence = []; |
||
| 208 | foreach ($objItems as $objItem) { |
||
| 209 | if ($categoryId === 0) { |
||
| 210 | $questions = []; |
||
| 211 | foreach ($objItem->obj->question_ids as $questionId) { |
||
| 212 | if (isset($itemQuestions[$questionId])) { |
||
| 213 | $questions[$questionId] = $itemQuestions[$questionId]; |
||
| 214 | } |
||
| 215 | } |
||
| 216 | // As weird as this may sound, the constructors for the different object types (found in |
||
| 217 | // src/CourseBundle/Component/CourseCopy/Resources/[objecttype].php) store the object property |
||
| 218 | // in the "obj" attribute. Old code... |
||
| 219 | // So here we recover properties from the quiz object, including questions (array built above). |
||
| 220 | $sequence[$categoryId][$objItem->obj->iid] = [ |
||
| 221 | 'title' => $objItem->obj->title, |
||
| 222 | 'comment' => $objItem->obj->description, |
||
| 223 | 'cc_type' => 'quiz', |
||
| 224 | 'source_id' => $objItem->obj->iid, |
||
| 225 | 'questions' => $questions, |
||
| 226 | 'max_attempt' => $objItem->obj->max_attempt, |
||
| 227 | 'expired_time' => $objItem->obj->expired_time, |
||
| 228 | 'pass_percentage' => $objItem->obj->pass_percentage, |
||
| 229 | 'random_answers' => $objItem->obj->random_answers, |
||
| 230 | 'course_code' => $coursecode, |
||
| 231 | ]; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | $sequences = $sequence[$categoryId]; |
||
| 235 | break; |
||
| 236 | case 'document': |
||
| 237 | $sequence = []; |
||
| 238 | foreach ($objItems as $objItem) { |
||
| 239 | if ($categoryId === 0) { |
||
| 240 | $sequence[$categoryId][$objItem->source_id] = [ |
||
| 241 | 'title' => $objItem->title, |
||
| 242 | 'comment' => $objItem->comment, |
||
| 243 | 'cc_type' => ($objItem->file_type == 'folder' ? 'folder' : 'resource'), |
||
| 244 | 'source_id' => $objItem->source_id, |
||
| 245 | 'path' => $objItem->path, |
||
| 246 | 'file_type' => $objItem->file_type, |
||
| 247 | 'course_code' => $coursecode, |
||
| 248 | ]; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | $sequences = $sequence[$categoryId]; |
||
| 252 | break; |
||
| 253 | case 'forum': |
||
| 254 | foreach ($objItems as $objItem) { |
||
| 255 | if ($categoryId == $objItem->obj->forum_category) { |
||
| 256 | $sequence[$categoryId][$objItem->obj->forum_id] = [ |
||
| 257 | 'title' => $objItem->obj->forum_title, |
||
| 258 | 'comment' => $objItem->obj->forum_comment, |
||
| 259 | 'cc_type' => 'forum', |
||
| 260 | 'source_id' => $objItem->obj->iid, |
||
| 261 | ]; |
||
| 262 | } |
||
| 263 | } |
||
| 264 | $sequences = $sequence[$categoryId]; |
||
| 265 | break; |
||
| 266 | case 'page': |
||
| 267 | foreach ($objItems as $objItem) { |
||
| 268 | if ($categoryId === 0) { |
||
| 269 | $sequence[$categoryId][$objItem->page_id] = [ |
||
| 270 | 'title' => $objItem->title, |
||
| 271 | 'comment' => $objItem->content, |
||
| 272 | 'cc_type' => 'page', |
||
| 273 | 'source_id' => $objItem->page_id, |
||
| 274 | 'reflink' => $objItem->reflink, |
||
| 275 | ]; |
||
| 276 | } |
||
| 277 | } |
||
| 278 | $sequences = $sequence[$categoryId]; |
||
| 279 | break; |
||
| 280 | case 'link': |
||
| 281 | if (!isset($categoryId)) { |
||
| 282 | $categories = []; |
||
| 283 | foreach ($objItems as $objItem) { |
||
| 284 | $categories[$objItem->category_id] = self::getSequence($objItems, $objItem->category_id, $itemType); |
||
| 285 | } |
||
| 286 | $sequences = $categories; |
||
| 287 | } else { |
||
| 288 | foreach ($objItems as $objItem) { |
||
| 289 | if ($categoryId == $objItem->category_id) { |
||
| 290 | $sequence[$categoryId][$objItem->source_id] = [ |
||
| 291 | 'title' => $objItem->title, |
||
| 292 | 'comment' => $objItem->description, |
||
| 293 | 'cc_type' => 'url', |
||
| 294 | 'source_id' => $objItem->source_id, |
||
| 295 | 'url' => $objItem->url, |
||
| 296 | 'target' => $objItem->target, |
||
| 297 | ]; |
||
| 298 | } |
||
| 299 | } |
||
| 300 | $sequences = $sequence[$categoryId]; |
||
| 301 | } |
||
| 302 | break; |
||
| 303 | } |
||
| 304 | |||
| 305 | return $sequences; |
||
| 306 | } |
||
| 363 |