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 |
||
11 | class Client |
||
12 | { |
||
13 | |||
14 | /** @var Guzzle */ |
||
15 | protected $guzzle; |
||
16 | |||
17 | /** |
||
18 | * @param string $clientId |
||
19 | * @param string $clientSecret |
||
20 | */ |
||
21 | public function __construct($clientId, $clientSecret) |
||
36 | |||
37 | /** |
||
38 | * @param string $uri |
||
39 | * @param array $query |
||
40 | * @param array $options |
||
41 | * |
||
42 | * @return Response |
||
43 | */ |
||
44 | View Code Duplication | public function get($uri, array $query = [], array $options = []) |
|
51 | |||
52 | /** |
||
53 | * @param string $uri |
||
54 | * @param array $query |
||
55 | * @param array $options |
||
56 | * |
||
57 | * @return Promise |
||
58 | */ |
||
59 | View Code Duplication | public function getAsync($uri, array $query = [], array $options = []) |
|
66 | |||
67 | /** |
||
68 | * @param array $query |
||
69 | * @param string $separator |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function buildQuery(array $query, $separator = '&') |
||
91 | |||
92 | /** |
||
93 | * @param string $uri |
||
94 | * @param array $body |
||
95 | * @param array $options |
||
96 | * |
||
97 | * @return Response |
||
98 | */ |
||
99 | View Code Duplication | public function post($uri, array $body = [], array $options = []) |
|
106 | |||
107 | /** |
||
108 | * @param string $uri |
||
109 | * @param array $body |
||
110 | * @param array $options |
||
111 | * |
||
112 | * @return Promise |
||
113 | */ |
||
114 | View Code Duplication | public function postAsync($uri, array $body = [], array $options = []) |
|
121 | } |
||
122 |
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.