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:
Complex classes like Reactions often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Reactions, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Reactions extends AbstractApi |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | * |
||
23 | * @param string $name |
||
24 | * @param string $file |
||
25 | * @param string $file_comment |
||
26 | * @param string $channel |
||
27 | * @param float $timestamp |
||
28 | * @return Reactions |
||
29 | */ |
||
30 | View Code Duplication | public function add($name, $file = NULL, $file_comment = NULL, $channel = NULL, $timestamp = NULL) { |
|
85 | |||
86 | |||
87 | |||
88 | |||
89 | /** |
||
90 | * {@inheritdoc} |
||
91 | * |
||
92 | * @param string $file |
||
93 | * @param string $file_comment |
||
94 | * @param string $channel |
||
95 | * @param float $timestamp |
||
96 | * @param boolean $full |
||
97 | * @return Reactions |
||
98 | */ |
||
99 | public function get($file = NULL, $file_comment = NULL, $channel = NULL, $timestamp = NULL, $full = NULL) { |
||
155 | |||
156 | |||
157 | |||
158 | |||
159 | /** |
||
160 | * {@inheritdoc} |
||
161 | * |
||
162 | * @param string $user |
||
163 | * @param boolean $full |
||
164 | * @param string $count |
||
165 | * @param string $page |
||
166 | * @return Reactions |
||
167 | */ |
||
168 | public function list_reactions($user = NULL, $full = NULL, $count = 100, $page = 1) { |
||
169 | |||
170 | // Check if the type of the variables is valid. |
||
171 | if (($user != NULL) && !is_string($user)) { |
||
172 | throw new InvalidArgumentException("The type of the user variable is not valid."); |
||
173 | } |
||
174 | if (($full != NULL) && !is_bool($full)) { |
||
175 | throw new InvalidArgumentException("The type of the full variable is not valid."); |
||
176 | } |
||
177 | if (!is_integer($count)) { |
||
178 | throw new InvalidArgumentException("The type of the count variable is not valid."); |
||
179 | } |
||
180 | if (!is_integer($page)) { |
||
181 | throw new InvalidArgumentException("The type of the page variable is not valid."); |
||
182 | } |
||
183 | |||
184 | // Set the arguments of the request |
||
185 | $arguments = array( |
||
186 | "count" => $count, |
||
187 | "page" => $page |
||
188 | ); |
||
189 | |||
190 | if($user != NULL) { |
||
191 | $arguments["user"] = $user; |
||
192 | } |
||
193 | if($full != NULL) { |
||
194 | $arguments["full"] = $full; |
||
195 | } |
||
196 | |||
197 | $this->setUrl("reactions.list"); |
||
198 | |||
199 | // Send the request |
||
200 | try { |
||
201 | $client = new \GuzzleHttp\Client(); |
||
202 | $json_response = $client->request('GET', $this->getUrl(), []); |
||
203 | $response = json_decode( $json_response->getBody() ); |
||
204 | } |
||
205 | catch (RequestException $e) { |
||
206 | throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
||
207 | } |
||
208 | |||
209 | if($response->{'ok'} === FALSE) { |
||
210 | throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
||
211 | } |
||
212 | |||
213 | return $json_response->getBody(); |
||
214 | } |
||
215 | |||
216 | |||
217 | |||
218 | |||
219 | /** |
||
220 | * {@inheritdoc} |
||
221 | * |
||
222 | * @param string $name |
||
223 | * @param string $file |
||
224 | * @param string $file_comment |
||
225 | * @param string $channel |
||
226 | * @param float $timestamp |
||
227 | * @return Reactions |
||
228 | */ |
||
229 | View Code Duplication | public function remove($name, $file = NULL, $file_comment = NULL, $channel = NULL, $timestamp = NULL) { |
|
284 | } |
||
285 |
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.