| Conditions | 10 |
| Paths | 66 |
| Total Lines | 73 |
| Code Lines | 51 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 48 | public function send(string $identifier, string $message, array $extra = []): void { |
||
| 49 | $this->logger->debug("Sending WhatsApp Cloud API message to $identifier"); |
||
| 50 | |||
| 51 | try { |
||
| 52 | $phoneNumberId = $this->getConfig('phone_number_id'); |
||
| 53 | $apiKey = $this->getConfig('api_key'); |
||
| 54 | $apiEndpoint = $this->getConfig('api_endpoint') ?? self::API_BASE_URL; |
||
| 55 | |||
| 56 | if (!$phoneNumberId || !$apiKey) { |
||
| 57 | throw new ConfigurationException('Missing required Cloud API configuration'); |
||
| 58 | } |
||
| 59 | |||
| 60 | // Normaliza o número de telefone removendo caracteres especiais |
||
| 61 | $phoneNumber = preg_replace('/\D/', '', $identifier); |
||
| 62 | if (strlen($phoneNumber) < 10) { |
||
| 63 | throw new MessageTransmissionException('Invalid phone number format'); |
||
| 64 | } |
||
| 65 | |||
| 66 | $url = sprintf( |
||
| 67 | '%s/%s/%s/messages', |
||
| 68 | rtrim($apiEndpoint, '/'), |
||
| 69 | self::API_VERSION, |
||
| 70 | $phoneNumberId |
||
| 71 | ); |
||
| 72 | |||
| 73 | $response = $this->client->post($url, [ |
||
| 74 | 'headers' => [ |
||
| 75 | 'Authorization' => "Bearer $apiKey", |
||
| 76 | 'Content-Type' => 'application/json', |
||
| 77 | ], |
||
| 78 | 'json' => [ |
||
| 79 | 'messaging_product' => 'whatsapp', |
||
| 80 | 'recipient_type' => 'individual', |
||
| 81 | 'to' => $phoneNumber, |
||
| 82 | 'type' => 'text', |
||
| 83 | 'text' => [ |
||
| 84 | 'body' => $message, |
||
| 85 | ], |
||
| 86 | ], |
||
| 87 | ]); |
||
| 88 | |||
| 89 | $statusCode = $response->getStatusCode(); |
||
| 90 | $responseBody = json_decode((string)$response->getBody(), true); |
||
| 91 | |||
| 92 | if ($statusCode >= 200 && $statusCode < 300) { |
||
| 93 | $this->logger->debug('WhatsApp message sent successfully', [ |
||
| 94 | 'phone' => $phoneNumber, |
||
| 95 | 'message_id' => $responseBody['messages'][0]['id'] ?? 'unknown', |
||
| 96 | ]); |
||
| 97 | } else { |
||
| 98 | throw new MessageTransmissionException( |
||
| 99 | 'Failed to send WhatsApp message: ' . ($responseBody['error']['message'] ?? 'Unknown error') |
||
| 100 | ); |
||
| 101 | } |
||
| 102 | } catch (ConnectException $e) { |
||
| 103 | $this->logger->error('Connection error sending WhatsApp message', ['exception' => $e]); |
||
| 104 | throw new MessageTransmissionException('Failed to connect to WhatsApp API'); |
||
| 105 | } catch (ClientException | ServerException $e) { |
||
| 106 | $errorMsg = 'Unknown error'; |
||
| 107 | try { |
||
| 108 | $body = json_decode((string)$e->getResponse()->getBody(), true); |
||
| 109 | $errorMsg = $body['error']['message'] ?? $body['message'] ?? $errorMsg; |
||
| 110 | } catch (\Exception) { |
||
| 111 | // Use default error message |
||
| 112 | } |
||
| 113 | $this->logger->error('WhatsApp API error', [ |
||
| 114 | 'status' => $e->getResponse()->getStatusCode() ?? 'unknown', |
||
| 115 | 'error' => $errorMsg, |
||
| 116 | ]); |
||
| 117 | throw new MessageTransmissionException("WhatsApp API error: $errorMsg"); |
||
| 118 | } catch (RequestException $e) { |
||
| 119 | $this->logger->error('Request error sending WhatsApp message', ['exception' => $e]); |
||
| 120 | throw new MessageTransmissionException('Failed to send WhatsApp message'); |
||
| 121 | } |
||
| 236 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths