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 |
||
19 | class AbstractApi |
||
20 | { |
||
21 | /** |
||
22 | * @var ClientInterface HTTP-client from Guzzle |
||
23 | */ |
||
24 | protected $client; |
||
25 | |||
26 | /** |
||
27 | * @var Serializer |
||
28 | */ |
||
29 | protected $serializer; |
||
30 | |||
31 | /** |
||
32 | * @var LoggerInterface |
||
33 | */ |
||
34 | protected $logger; |
||
35 | |||
36 | /** |
||
37 | * @var string Authentication token for API |
||
38 | */ |
||
39 | protected $authToken; |
||
40 | |||
41 | /** |
||
42 | * @var string CSRF-token for API |
||
43 | */ |
||
44 | protected $csRfToken; |
||
45 | |||
46 | |||
47 | public function __construct(ClientInterface $httpClient, Serializer $serializer, LoggerInterface $logger) |
||
53 | |||
54 | /** |
||
55 | * @param string $path Request path |
||
56 | * @param array $parameters Key => Value array of query parameters |
||
57 | * |
||
58 | * @return ResponseInterface |
||
59 | * |
||
60 | * @throws NetworkException |
||
61 | */ |
||
62 | View Code Duplication | public function sendGetRequest(string $path, array $parameters = []): ResponseInterface |
|
72 | |||
73 | /** |
||
74 | * @param string $path Request path |
||
75 | * @param array $parameters Key => Value array of request data |
||
76 | * |
||
77 | * @return ResponseInterface |
||
78 | * |
||
79 | * @throws NetworkException |
||
80 | */ |
||
81 | View Code Duplication | public function sendPostRequest(string $path, array $parameters = []): ResponseInterface |
|
91 | |||
92 | /** |
||
93 | * Make GET request and return response body |
||
94 | */ |
||
95 | public function getGetResponseBody($path, array $parameters = []): StreamInterface |
||
103 | |||
104 | /** |
||
105 | * Make POST request and return response body |
||
106 | */ |
||
107 | public function getPostResponseBody(string $path, array $parameters = []): StreamInterface |
||
115 | |||
116 | /** |
||
117 | * Make GET request and return DTO objects |
||
118 | * |
||
119 | * @return array|object |
||
120 | */ |
||
121 | public function getGetJsonData(string $path, array $parameters = [], string $type, DeserializationContext $context = null) |
||
130 | |||
131 | /** |
||
132 | * Make POST request and return DTO objects |
||
133 | * |
||
134 | * @return array|object |
||
135 | */ |
||
136 | public function getPostJsonData(string $path, array $parameters = [], string $type, DeserializationContext $context = null) |
||
145 | |||
146 | /** |
||
147 | * @throws ForbiddenException |
||
148 | * @throws NotFoundException |
||
149 | * @throws ServerProblemException |
||
150 | * @throws UnauthorizedException |
||
151 | */ |
||
152 | private function checkResponse(ResponseInterface $response): void |
||
176 | } |
||
177 |
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.