| Conditions | 9 |
| Paths | 9 |
| Total Lines | 87 |
| Code Lines | 59 |
| 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 |
||
| 77 | public function generateLearnPath(string $topic, int $chaptersCount, string $language, int $wordsCount, bool $addTests, int $numQuestions): ?array |
||
| 78 | { |
||
| 79 | // Step 1: Generate the Table of Contents |
||
| 80 | $tableOfContentsPrompt = \sprintf( |
||
| 81 | 'Generate a structured table of contents for a course in "%s" with %d chapters on "%s". |
||
| 82 | Return a numbered list, each chapter on a new line. No conclusion.', |
||
| 83 | $language, |
||
| 84 | $chaptersCount, |
||
| 85 | $topic |
||
| 86 | ); |
||
| 87 | |||
| 88 | $lpStructure = $this->requestOpenAI($tableOfContentsPrompt, 'learnpath'); |
||
| 89 | if (!$lpStructure) { |
||
| 90 | return ['success' => false, 'message' => 'Failed to generate course structure.']; |
||
| 91 | } |
||
| 92 | |||
| 93 | // Step 2: Generate content for each chapter |
||
| 94 | $lpItems = []; |
||
| 95 | $chapters = explode("\n", trim($lpStructure)); |
||
| 96 | foreach ($chapters as $index => $chapterTitle) { |
||
| 97 | $chapterTitle = trim($chapterTitle); |
||
| 98 | if (empty($chapterTitle)) { |
||
| 99 | continue; |
||
| 100 | } |
||
| 101 | |||
| 102 | $chapterPrompt = \sprintf( |
||
| 103 | 'Create a learning chapter in HTML for "%s" in "%s" with %d words. |
||
| 104 | Title: "%s". Assume the reader already knows the context.', |
||
| 105 | $topic, |
||
| 106 | $language, |
||
| 107 | $wordsCount, |
||
| 108 | $chapterTitle |
||
| 109 | ); |
||
| 110 | |||
| 111 | $chapterContent = $this->requestOpenAI($chapterPrompt, 'learnpath'); |
||
| 112 | if (!$chapterContent) { |
||
| 113 | continue; |
||
| 114 | } |
||
| 115 | |||
| 116 | $lpItems[] = [ |
||
| 117 | 'title' => $chapterTitle, |
||
| 118 | 'content' => "<html><head><title>{$chapterTitle}</title></head><body>{$chapterContent}</body></html>", |
||
| 119 | ]; |
||
| 120 | } |
||
| 121 | |||
| 122 | // Step 3: Generate quizzes if enabled |
||
| 123 | $quizItems = []; |
||
| 124 | if ($addTests) { |
||
| 125 | foreach ($lpItems as &$chapter) { |
||
| 126 | $quizPrompt = \sprintf( |
||
| 127 | 'Generate %d multiple-choice questions in Aiken format in %s about "%s". |
||
| 128 | Ensure each question follows this format: |
||
| 129 | |||
| 130 | 1. The question text. |
||
| 131 | A. Option A |
||
| 132 | B. Option B |
||
| 133 | C. Option C |
||
| 134 | D. Option D |
||
| 135 | ANSWER: (Correct answer letter) |
||
| 136 | |||
| 137 | Each question must have exactly 4 options and one answer line. |
||
| 138 | Return only valid questions without extra text.', |
||
| 139 | $numQuestions, |
||
| 140 | $language, |
||
| 141 | $chapter['title'] |
||
| 142 | ); |
||
| 143 | |||
| 144 | $quizContent = $this->requestOpenAI($quizPrompt, 'learnpath'); |
||
| 145 | |||
| 146 | if ($quizContent) { |
||
| 147 | $validQuestions = $this->filterValidAikenQuestions($quizContent); |
||
| 148 | |||
| 149 | if (!empty($validQuestions)) { |
||
| 150 | $quizItems[] = [ |
||
| 151 | 'title' => 'Quiz: '.$chapter['title'], |
||
| 152 | 'content' => implode("\n\n", $validQuestions), |
||
| 153 | ]; |
||
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | return [ |
||
| 160 | 'success' => true, |
||
| 161 | 'topic' => $topic, |
||
| 162 | 'lp_items' => $lpItems, |
||
| 163 | 'quiz_items' => $quizItems, |
||
| 164 | ]; |
||
| 253 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: