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 |
||
| 25 | class Webhook extends HttpApi |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var string |
||
| 29 | */ |
||
| 30 | private $apiKey; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @param HttpClient $httpClient |
||
| 34 | * @param RequestBuilder $requestBuilder |
||
| 35 | * @param Hydrator $hydrator |
||
| 36 | * @param string $apiKey |
||
| 37 | */ |
||
| 38 | 8 | public function __construct(HttpClient $httpClient, RequestBuilder $requestBuilder, Hydrator $hydrator, $apiKey) |
|
| 43 | |||
| 44 | /** |
||
| 45 | * This function verifies the webhook signature with your API key to to see if it is authentic. |
||
| 46 | * |
||
| 47 | * If this function returns FALSE, you must not process the request. |
||
| 48 | * You should reject the request with status code 403 Forbidden. |
||
| 49 | * |
||
| 50 | * @param int $timestamp |
||
| 51 | * @param string $token |
||
| 52 | * @param string $signature |
||
| 53 | * |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | 3 | public function verifyWebhookSignature($timestamp, $token, $signature) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $domain |
||
| 74 | * |
||
| 75 | * @return IndexResponse |
||
| 76 | */ |
||
| 77 | 1 | public function index($domain) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param string $domain |
||
| 87 | * @param string $webhook |
||
| 88 | * |
||
| 89 | * @return ShowResponse |
||
| 90 | */ |
||
| 91 | 1 | public function show($domain, $webhook) |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param string $domain |
||
| 102 | * @param string $id |
||
| 103 | * @param string $url |
||
| 104 | * |
||
| 105 | * @return CreateResponse |
||
| 106 | */ |
||
| 107 | 1 | View Code Duplication | public function create($domain, $id, $url) |
| 122 | |||
| 123 | /** |
||
| 124 | * @param string $domain |
||
| 125 | * @param string $id |
||
| 126 | * @param string $url |
||
| 127 | * |
||
| 128 | * @return UpdateResponse |
||
| 129 | */ |
||
| 130 | 1 | View Code Duplication | public function update($domain, $id, $url) |
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $domain |
||
| 147 | * @param string $id |
||
| 148 | * |
||
| 149 | * @return DeleteResponse |
||
| 150 | */ |
||
| 151 | 1 | public function delete($domain, $id) |
|
| 160 | } |
||
| 161 |
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.