| Conditions | 6 |
| Paths | 15 |
| Total Lines | 62 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 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 |
||
| 64 | private function requestGrokAI(string $prompt, string $toolName, array $options = []): ?string |
||
| 65 | { |
||
| 66 | $userId = $this->getUserId(); |
||
| 67 | if (!$userId) { |
||
|
|
|||
| 68 | throw new RuntimeException('User not authenticated.'); |
||
| 69 | } |
||
| 70 | |||
| 71 | $payload = [ |
||
| 72 | 'model' => $this->model, |
||
| 73 | 'prompt' => $prompt, // Direct prompt string, no messages array |
||
| 74 | ...array_merge($this->defaultOptions, $options), |
||
| 75 | ]; |
||
| 76 | |||
| 77 | try { |
||
| 78 | $response = $this->httpClient->request('POST', $this->apiUrl, [ |
||
| 79 | 'headers' => [ |
||
| 80 | 'Authorization' => 'Bearer '.$this->apiKey, |
||
| 81 | 'Content-Type' => 'application/json', |
||
| 82 | ], |
||
| 83 | 'json' => $payload, |
||
| 84 | ]); |
||
| 85 | |||
| 86 | $statusCode = $response->getStatusCode(); |
||
| 87 | if (200 !== $statusCode) { |
||
| 88 | throw new RuntimeException('API request failed with status: ' . $statusCode); |
||
| 89 | } |
||
| 90 | |||
| 91 | $data = $response->toArray(); |
||
| 92 | |||
| 93 | // Check for error key first |
||
| 94 | if (isset($data['error'])) { |
||
| 95 | throw new RuntimeException('API error: ' . $data['error']['message']); |
||
| 96 | } |
||
| 97 | |||
| 98 | // Proper access: assuming response_format 'b64_json' |
||
| 99 | if (isset($data['data'][0]['b64_json'])) { |
||
| 100 | $generatedContent = $data['data'][0]['b64_json']; |
||
| 101 | |||
| 102 | // Usage might not exist for images; default to 0 |
||
| 103 | $usage = $data['usage'] ?? ['prompt_tokens' => 0, 'completion_tokens' => 0, 'total_tokens' => 0]; |
||
| 104 | |||
| 105 | // Log request |
||
| 106 | $aiRequest = new AiRequests(); |
||
| 107 | $aiRequest->setUserId($userId) |
||
| 108 | ->setToolName($toolName) |
||
| 109 | ->setRequestText($prompt) |
||
| 110 | ->setPromptTokens($usage['prompt_tokens']) |
||
| 111 | ->setCompletionTokens($usage['completion_tokens']) |
||
| 112 | ->setTotalTokens($usage['total_tokens']) |
||
| 113 | ->setAiProvider('grok') |
||
| 114 | ; |
||
| 115 | |||
| 116 | $this->aiRequestsRepository->save($aiRequest); |
||
| 117 | |||
| 118 | return $generatedContent; |
||
| 119 | } |
||
| 120 | |||
| 121 | return null; |
||
| 122 | } catch (Exception $e) { |
||
| 123 | error_log('[AI][Grok] Exception: '.$e->getMessage()); |
||
| 124 | |||
| 125 | return null; |
||
| 126 | } |
||
| 136 |
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: