Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 24 | class AuthnetWebhooksRequest |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * @var string Base URL for processing a webhook |
||
| 28 | */ |
||
| 29 | private $url; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string Endpoint for processing a webhook |
||
| 33 | */ |
||
| 34 | private $endpoint; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var string JSON formatted API request |
||
| 38 | */ |
||
| 39 | private $requestJson; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var object Wrapper object representing an endpoint |
||
| 43 | */ |
||
| 44 | private $processor; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Creates the request object by setting the Authorize.Net credentials and URL of the endpoint to be used |
||
| 48 | * for the API call |
||
| 49 | * |
||
| 50 | * @param string $api_url URL endpoint for processing a transaction |
||
| 51 | */ |
||
| 52 | public function __construct($api_url) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Outputs the account credentials, endpoint URL, and request JSON in a human readable format |
||
| 59 | * |
||
| 60 | * @return string HTML table containing debugging information |
||
| 61 | */ |
||
| 62 | public function __toString() |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Creates a new webhook |
||
| 82 | * |
||
| 83 | * @param array $webhooks Array of webhooks to be created or modified |
||
| 84 | * @param string $webhookUrl URL of where webhook notifications will be sent |
||
| 85 | * @param string $status Status of webhooks to be created or modified [active/inactive] |
||
| 86 | * @return \JohnConde\Authnet\AuthnetWebhooksResponse |
||
| 87 | * @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
||
| 88 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 89 | */ |
||
| 90 | View Code Duplication | public function createWebhooks(array $webhooks, string $webhookUrl, string $status = 'active') : object |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Sends a test ping to a URL for (a) designated webhook(s) |
||
| 106 | * |
||
| 107 | * @param string $webhookId Webhook ID to be tested |
||
| 108 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 109 | */ |
||
| 110 | public function testWebhook(string $webhookId) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Gets all of the available event types |
||
| 120 | * |
||
| 121 | * @return \JohnConde\Authnet\AuthnetWebhooksResponse |
||
| 122 | */ |
||
| 123 | public function getEventTypes() : object |
||
| 129 | |||
| 130 | /** |
||
| 131 | * List all of your webhooks |
||
| 132 | * |
||
| 133 | * @return \JohnConde\Authnet\AuthnetWebhooksResponse |
||
| 134 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 135 | * @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
||
| 136 | */ |
||
| 137 | public function getWebhooks() : object |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Get a webhook |
||
| 146 | * |
||
| 147 | * @param string $webhookId Webhook ID to be retrieved |
||
| 148 | * @return \JohnConde\Authnet\AuthnetWebhooksResponse |
||
| 149 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 150 | * @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
||
| 151 | */ |
||
| 152 | public function getWebhook(string $webhookId) : object |
||
| 158 | |||
| 159 | /** |
||
| 160 | * GET API request |
||
| 161 | * |
||
| 162 | * @param string $url API endpoint to hit |
||
| 163 | * @return object |
||
| 164 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 165 | * @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
||
| 166 | */ |
||
| 167 | private function getByUrl(string $url) : object |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Updates webhook event types |
||
| 175 | * |
||
| 176 | * @param string $webhookId Webhook ID to be modified |
||
| 177 | * @param string $webhookUrl URL of where webhook notifications will be sent |
||
| 178 | * @param array $eventTypes Array of event types to be added/removed |
||
| 179 | * @param string $status Status of webhooks to be modified [active/inactive] |
||
| 180 | * @return \JohnConde\Authnet\AuthnetWebhooksResponse |
||
| 181 | * @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
||
| 182 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 183 | */ |
||
| 184 | View Code Duplication | public function updateWebhook(string $webhookId, string $webhookUrl, array $eventTypes, string $status = 'active') : object |
|
| 197 | |||
| 198 | /** |
||
| 199 | * Delete a webhook |
||
| 200 | * |
||
| 201 | * @param string $webhookId Webhook ID to be deleted |
||
| 202 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 203 | */ |
||
| 204 | public function deleteWebhook(string $webhookId) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Retrieve Notification History |
||
| 213 | * |
||
| 214 | * @param integer $limit Default: 1000 |
||
| 215 | * @param integer $offset Default: 0 |
||
| 216 | * @return \JohnConde\Authnet\AuthnetWebhooksResponse |
||
| 217 | * @throws \JohnConde\Authnet\AuthnetInvalidJsonException |
||
| 218 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 219 | */ |
||
| 220 | public function getNotificationHistory(int $limit = 1000, int $offset = 0) : object |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Tells the handler to make the API call to Authorize.Net |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 236 | */ |
||
| 237 | private function handleResponse() : string |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Make GET request via Curl |
||
| 257 | * |
||
| 258 | * @param string $url |
||
| 259 | * @param array $params |
||
| 260 | * @return string |
||
| 261 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 262 | * |
||
| 263 | * @codeCoverageIgnore |
||
| 264 | */ |
||
| 265 | private function get(string $url, array $params = []) : string |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Make POST request via Curl |
||
| 273 | * |
||
| 274 | * @param string $url API endpoint |
||
| 275 | * @param string $request JSON request payload |
||
| 276 | * @return string |
||
| 277 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 278 | * |
||
| 279 | * @codeCoverageIgnore |
||
| 280 | */ |
||
| 281 | private function post(string $url, string $request) : string |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Make PUT request via Curl |
||
| 289 | * |
||
| 290 | * @param string $url API endpoint |
||
| 291 | * @param string $request JSON request payload |
||
| 292 | * @return string |
||
| 293 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 294 | * |
||
| 295 | * @codeCoverageIgnore |
||
| 296 | */ |
||
| 297 | private function put(string $url, string $request) : string |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Make DELETE request via Curl |
||
| 305 | * |
||
| 306 | * @param string $url API endpoint |
||
| 307 | * @return string |
||
| 308 | * @throws \JohnConde\Authnet\AuthnetCurlException |
||
| 309 | * |
||
| 310 | * @codeCoverageIgnore |
||
| 311 | */ |
||
| 312 | private function delete(string $url) : string |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Sets the handler to be used to handle our API call. Mainly used for unit testing as Curl is used by default. |
||
| 320 | * |
||
| 321 | * @param \Curl\Curl $processor |
||
| 322 | */ |
||
| 323 | public function setProcessHandler(\Curl\Curl $processor) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Gets the request sent to Authorize.Net in JSON format for logging purposes |
||
| 330 | * |
||
| 331 | * @return string transaction request sent to Authorize.Net in JSON format |
||
| 332 | */ |
||
| 333 | public function getRawRequest() : string |
||
| 337 | } |
||
| 338 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.