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