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