| Conditions | 12 |
| Paths | 18 |
| Total Lines | 97 |
| Code Lines | 66 |
| 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 |
||
| 80 | #[Route('/open_answer_grade', name: 'chamilo_core_ai_open_answer_grade', methods: ['POST'])] |
||
| 81 | public function openAnswerGrade(Request $request): JsonResponse |
||
| 82 | { |
||
| 83 | $exeId = $request->request->getInt('exeId', 0); |
||
| 84 | $questionId = $request->request->getInt('questionId', 0); |
||
| 85 | $courseId = $request->request->getInt('courseId', 0); |
||
| 86 | |||
| 87 | if (0 === $exeId || 0 === $questionId || 0 === $courseId) { |
||
| 88 | return $this->json(['error' => 'Missing parameters'], 400); |
||
| 89 | } |
||
| 90 | |||
| 91 | /** @var TrackEExercise $trackExercise */ |
||
| 92 | $trackExercise = $this->em |
||
| 93 | ->getRepository(TrackEExercise::class) |
||
| 94 | ->find($exeId) |
||
| 95 | ; |
||
| 96 | if (null === $trackExercise) { |
||
| 97 | return $this->json(['error' => 'Exercise attempt not found'], 404); |
||
| 98 | } |
||
| 99 | |||
| 100 | $attempt = $this->attemptRepo->findOneBy([ |
||
| 101 | 'trackExercise' => $trackExercise, |
||
| 102 | 'questionId' => $questionId, |
||
| 103 | 'user' => $trackExercise->getUser(), |
||
| 104 | ]); |
||
| 105 | if (null === $attempt) { |
||
| 106 | return $this->json(['error' => 'Attempt not found'], 404); |
||
| 107 | } |
||
| 108 | |||
| 109 | $answerText = $attempt->getAnswer(); |
||
| 110 | |||
| 111 | if (ctype_digit($answerText)) { |
||
| 112 | $cqa = $this->em |
||
| 113 | ->getRepository(CQuizAnswer::class) |
||
| 114 | ->find((int) $answerText) |
||
| 115 | ; |
||
| 116 | if ($cqa) { |
||
| 117 | $answerText = $cqa->getAnswer(); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | $courseInfo = api_get_course_info_by_id($courseId); |
||
| 122 | if (empty($courseInfo['real_id'])) { |
||
| 123 | return $this->json(['error' => 'Course info not found'], 500); |
||
| 124 | } |
||
| 125 | |||
| 126 | $question = Question::read($questionId, $courseInfo); |
||
| 127 | if (false === $question) { |
||
| 128 | return $this->json(['error' => 'Question not found'], 404); |
||
| 129 | } |
||
| 130 | |||
| 131 | $language = $courseInfo['language'] ?? 'en'; |
||
| 132 | $courseTitle = $courseInfo['title'] ?? ''; |
||
| 133 | $maxScore = $question->selectWeighting(); |
||
| 134 | $questionText = $question->selectTitle(); |
||
| 135 | $prompt = \sprintf( |
||
| 136 | "In language %s, for the question: '%s', in the context of %s, provide a score between 0 and %d on one line, then feedback on the next line for the following answer: '%s'.", |
||
| 137 | $language, |
||
| 138 | $questionText, |
||
| 139 | $courseTitle, |
||
| 140 | $maxScore, |
||
| 141 | $answerText |
||
| 142 | ); |
||
| 143 | |||
| 144 | $raw = trim((string) $this->aiProviderFactory |
||
| 145 | ->getProvider() |
||
| 146 | ->gradeOpenAnswer($prompt, 'open_answer_grade')); |
||
| 147 | |||
| 148 | if ('' === $raw) { |
||
| 149 | return $this->json(['error' => 'AI request failed'], 500); |
||
| 150 | } |
||
| 151 | |||
| 152 | if (str_contains($raw, "\n")) { |
||
| 153 | [$scoreLine, $feedback] = explode("\n", $raw, 2); |
||
| 154 | } else { |
||
| 155 | $scoreLine = (string) $maxScore; |
||
| 156 | $feedback = $raw; |
||
| 157 | } |
||
| 158 | |||
| 159 | $score = (int) filter_var($scoreLine, FILTER_SANITIZE_NUMBER_INT); |
||
| 160 | |||
| 161 | $track = new TrackEDefault(); |
||
| 162 | $track |
||
| 163 | ->setDefaultUserId($this->getUser()->getId()) |
||
| 164 | ->setDefaultEventType('ai_use_question_feedback') |
||
| 165 | ->setDefaultValueType('attempt_id') |
||
| 166 | ->setDefaultValue((string) $attempt->getId()) |
||
| 167 | ->setDefaultDate(new DateTime()) |
||
| 168 | ->setCId($courseId) |
||
| 169 | ->setSessionId(api_get_session_id()) |
||
| 170 | ; |
||
| 171 | $this->em->persist($track); |
||
| 172 | $this->em->flush(); |
||
| 173 | |||
| 174 | return $this->json([ |
||
| 175 | 'score' => $score, |
||
| 176 | 'feedback' => $feedback, |
||
| 177 | ]); |
||
| 222 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.