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 Client |
||
25 | { |
||
26 | const DEFAULT_OPTIONS = [ |
||
27 | Options::SCHEMA => 'https', |
||
28 | Options::PATH => '/', |
||
29 | Options::USER_AGENT => 'WyriHaximus/php-api-client', |
||
30 | Options::HEADERS => [], |
||
31 | ]; |
||
32 | |||
33 | /** |
||
34 | * @var GuzzleClient |
||
35 | */ |
||
36 | protected $handler; |
||
37 | |||
38 | /** |
||
39 | * @var LoopInterface |
||
40 | */ |
||
41 | protected $loop; |
||
42 | |||
43 | /** |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $options = []; |
||
47 | |||
48 | /** |
||
49 | * @var Hydrator |
||
50 | */ |
||
51 | protected $hydrator; |
||
52 | |||
53 | /** |
||
54 | * @var CacheInterface |
||
55 | */ |
||
56 | protected $cache; |
||
57 | |||
58 | /** |
||
59 | * @param LoopInterface $loop |
||
60 | * @param GuzzleClient $handler |
||
61 | * @param array $options |
||
62 | */ |
||
63 | 12 | public function __construct(LoopInterface $loop, GuzzleClient $handler, array $options = []) |
|
73 | |||
74 | /** |
||
75 | * @return Hydrator |
||
76 | * @throws \Exception |
||
77 | */ |
||
78 | 12 | protected function determineHydrator(): Hydrator |
|
96 | |||
97 | /** |
||
98 | * @param string $path |
||
99 | * @param bool $refresh |
||
100 | * @return PromiseInterface |
||
101 | */ |
||
102 | 5 | public function request(string $path, bool $refresh = false): PromiseInterface |
|
108 | |||
109 | /** |
||
110 | * @param string $path |
||
111 | * @param bool $refresh |
||
112 | * @return PromiseInterface |
||
113 | */ |
||
114 | 5 | public function requestRaw(string $path, bool $refresh = false): PromiseInterface |
|
123 | |||
124 | /** |
||
125 | * @param UriInterface $uri |
||
126 | * @return PromiseInterface |
||
127 | */ |
||
128 | 3 | protected function checkCache(UriInterface $uri): PromiseInterface |
|
148 | |||
149 | /** |
||
150 | * @param RequestInterface $request |
||
151 | * @param ResponseInterface $response |
||
152 | */ |
||
153 | 2 | protected function storeCache(RequestInterface $request, ResponseInterface $response) |
|
171 | |||
172 | /** |
||
173 | * @param UriInterface $uri |
||
174 | * @return string |
||
175 | */ |
||
176 | 3 | protected function determineCacheKey(UriInterface $uri): string |
|
191 | |||
192 | /** |
||
193 | * @param string $string |
||
194 | * @return string |
||
195 | */ |
||
196 | 3 | protected function stripExtraSlashes(string $string): string |
|
200 | |||
201 | /** |
||
202 | * @param RequestInterface $request |
||
203 | * @param bool $refresh |
||
204 | * @return PromiseInterface |
||
205 | */ |
||
206 | 5 | public function requestPsr7(RequestInterface $request, bool $refresh = false): PromiseInterface |
|
247 | |||
248 | /** |
||
249 | * @param string $path |
||
250 | * @return RequestInterface |
||
251 | */ |
||
252 | 5 | protected function createRequest(string $path): RequestInterface |
|
258 | |||
259 | /** |
||
260 | * @return array |
||
261 | */ |
||
262 | 5 | public function getHeaders(): array |
|
270 | |||
271 | /** |
||
272 | * @param string $json |
||
273 | * @return PromiseInterface |
||
274 | */ |
||
275 | public function jsonDecode(string $json): PromiseInterface |
||
281 | |||
282 | /** |
||
283 | * @return Hydrator |
||
284 | */ |
||
285 | 1 | public function getHydrator(): Hydrator |
|
289 | |||
290 | /** |
||
291 | * @return LoopInterface |
||
292 | */ |
||
293 | 3 | public function getLoop(): LoopInterface |
|
297 | |||
298 | /** |
||
299 | * @return string |
||
300 | */ |
||
301 | 8 | public function getBaseURL(): string |
|
305 | } |
||
306 |
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.