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 |
||
17 | class Request { |
||
18 | |||
19 | /** |
||
20 | * Host to make the calls to |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | private $host = "https://api.pinterest.com/v1/"; |
||
25 | |||
26 | /** |
||
27 | * Access token |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | protected $access_token = null; |
||
32 | |||
33 | /** |
||
34 | * Instance of the CurlBuilder class |
||
35 | * |
||
36 | * @var CurlBuilder |
||
37 | */ |
||
38 | private $curlbuilder; |
||
39 | |||
40 | /** |
||
41 | * Array with the headers from the last request |
||
42 | * |
||
43 | * @var array |
||
44 | */ |
||
45 | private $headers; |
||
46 | |||
47 | /** |
||
48 | * Constructor |
||
49 | * |
||
50 | * @param CurlBuilder $curlbuilder |
||
51 | */ |
||
52 | 43 | public function __construct(CurlBuilder $curlbuilder) |
|
56 | |||
57 | /** |
||
58 | * Set the access token |
||
59 | * |
||
60 | * @access public |
||
61 | * @param string $token |
||
62 | * @return void |
||
63 | */ |
||
64 | 43 | public function setAccessToken($token) |
|
68 | |||
69 | /** |
||
70 | * Make a get request to the given endpoint |
||
71 | * |
||
72 | * @access public |
||
73 | * @param string $endpoint |
||
74 | * @param array $parameters |
||
75 | * @return Response |
||
76 | */ |
||
77 | 27 | View Code Duplication | public function get($endpoint, array $parameters = array()) |
87 | |||
88 | /** |
||
89 | * Make a post request to the given endpoint |
||
90 | * |
||
91 | * @access public |
||
92 | * @param string $endpoint |
||
93 | * @param array $parameters |
||
94 | * @return Response |
||
95 | */ |
||
96 | 3 | public function post($endpoint, array $parameters = array()) |
|
100 | |||
101 | /** |
||
102 | * Make a put request to the given endpoint |
||
103 | * |
||
104 | * @access public |
||
105 | * @param string $endpoint |
||
106 | * @param array $parameters |
||
107 | * @return Response |
||
108 | */ |
||
109 | 1 | public function put($endpoint, array $parameters = array()) |
|
113 | |||
114 | /** |
||
115 | * Make a delete request to the given endpoint |
||
116 | * |
||
117 | * @access public |
||
118 | * @param string $endpoint |
||
119 | * @param array $parameters |
||
120 | * @return Response |
||
121 | */ |
||
122 | 6 | public function delete($endpoint, array $parameters = array()) |
|
126 | |||
127 | /** |
||
128 | * Make an update request to the given endpoint |
||
129 | * |
||
130 | * @access public |
||
131 | * @param string $endpoint |
||
132 | * @param array $parameters |
||
133 | * @param array $queryparameters |
||
134 | * @return Response |
||
135 | */ |
||
136 | 2 | View Code Duplication | public function update($endpoint, array $parameters = array(), array $queryparameters = array()) |
146 | |||
147 | /** |
||
148 | * Return the headers from the last request |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | 2 | public function getHeaders() |
|
156 | |||
157 | /** |
||
158 | * Execute the http request |
||
159 | * |
||
160 | * @access public |
||
161 | * @param string $method |
||
162 | * @param string $apiCall |
||
163 | * @param array $parameters |
||
164 | * @param array $headers |
||
165 | * @return Response |
||
166 | * @throws CurlException |
||
167 | * @throws PinterestException |
||
168 | */ |
||
169 | 39 | public function execute($method, $apiCall, array $parameters = array(), $headers = array()) |
|
258 | |||
259 | } |
||
260 |
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.