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 |
||
18 | class Pin extends AbstractApi |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | * |
||
23 | * @param string $channel |
||
24 | * @param string $file |
||
25 | * @param string $file_comment |
||
26 | * @param float $timestamp |
||
27 | * @return Pin |
||
28 | */ |
||
29 | View Code Duplication | public function add($channel, $file = NULL, $file_comment = NULL, $timestamp = NULL) { |
|
78 | |||
79 | |||
80 | |||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | * |
||
85 | * @param string $channel |
||
86 | * @return string |
||
87 | */ |
||
88 | public function list_pin($channel) { |
||
89 | |||
90 | // Check if the type of the variables is valid. |
||
91 | if (!is_string($channel)) { |
||
92 | throw new InvalidArgumentException("The type of the channel variable is not valid."); |
||
93 | } |
||
94 | |||
95 | // Set the arguments of the request |
||
96 | $arguments = array( |
||
97 | "channel" => $channel |
||
98 | ); |
||
99 | |||
100 | $this->setUrl("pin.list"); |
||
101 | |||
102 | // Send the request |
||
103 | try { |
||
104 | $client = new \GuzzleHttp\Client(); |
||
105 | $json_response = $client->request('GET', $this->getUrl(), []); |
||
106 | $response = json_decode( $json_response->getBody() ); |
||
107 | } |
||
108 | catch (RequestException $e) { |
||
109 | throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
||
110 | } |
||
111 | |||
112 | if($response->{'ok'} === FALSE) { |
||
113 | throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
||
114 | } |
||
115 | |||
116 | return $json_response->getBody(); |
||
117 | } |
||
118 | |||
119 | |||
120 | |||
121 | |||
122 | /** |
||
123 | * {@inheritdoc} |
||
124 | * |
||
125 | * @param string $channel |
||
126 | * @param string $file |
||
127 | * @param string $file_comment |
||
128 | * @param float $timestamp |
||
129 | * @return Pin |
||
130 | */ |
||
131 | View Code Duplication | public function remove($channel, $file = NULL, $file_comment = NULL, $timestamp = NULL) { |
|
180 | } |
||
181 |
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.