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 Files 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 Files, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Files extends AbstractApi |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | * |
||
23 | * @param string $file |
||
24 | * @return Files |
||
25 | */ |
||
26 | public function delete($file) { |
||
56 | |||
57 | |||
58 | |||
59 | /** |
||
60 | * {@inheritdoc} |
||
61 | * |
||
62 | * @param string $file |
||
63 | * @param integer $count |
||
64 | * @param integer $page |
||
65 | * @return Files |
||
66 | */ |
||
67 | View Code Duplication | public function info($file, $count = 100, $page = 1) { |
|
105 | |||
106 | |||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | * |
||
111 | * @param string $user |
||
112 | * @param string $channel |
||
113 | * @param integer $ts_from |
||
114 | * @param integer $ts_to |
||
115 | * @param string $types |
||
116 | * @param integer $count |
||
117 | * @param integer $page |
||
118 | * @return Files |
||
119 | */ |
||
120 | public function list_files($user = NULL, $channel = NULL, $ts_from = "now", $ts_to = "all", $types = "all", $count = 100, $page = 1) { |
||
121 | |||
122 | // Check if the type of the variables is valid. |
||
123 | if (($user != NULL) && !is_string($user)) { |
||
124 | throw new InvalidArgumentException("The type of the user variable is not valid."); |
||
125 | } |
||
126 | if (($channel != NULL) && !is_string($channel)) { |
||
127 | throw new InvalidArgumentException("The type of the channel variable is not valid."); |
||
128 | } |
||
129 | if (!is_int($ts_from)) { |
||
130 | throw new InvalidArgumentException("The type of the ts_from variable is not valid."); |
||
131 | } |
||
132 | if (!is_int($ts_to)) { |
||
133 | throw new InvalidArgumentException("The type of the ts_to variable is not valid."); |
||
134 | } |
||
135 | if (!is_string($types)) { |
||
136 | throw new InvalidArgumentException("The type of the types variable is not valid."); |
||
137 | } |
||
138 | if (!is_integer($count)) { |
||
139 | throw new InvalidArgumentException("The type of the count variable is not valid."); |
||
140 | } |
||
141 | if (!is_integer($page)) { |
||
142 | throw new InvalidArgumentException("The type of the page variable is not valid."); |
||
143 | } |
||
144 | |||
145 | // Set the arguments of the request |
||
146 | $arguments = array( |
||
147 | "ts_from" => (string)$ts_from, |
||
148 | "ts_to" => (string)$ts_to, |
||
149 | "types" => $types, |
||
150 | "count" => $count, |
||
151 | "page" => $page |
||
152 | ); |
||
153 | |||
154 | if ($user != NULL) { |
||
155 | $arguments["user"] = $user; |
||
156 | } |
||
157 | if ($channel != NULL) { |
||
158 | $arguments["channel"] = $channel; |
||
159 | } |
||
160 | |||
161 | $this->setUrl("files.list", $arguments); |
||
162 | |||
163 | // Send the request |
||
164 | try { |
||
165 | $client = new \GuzzleHttp\Client(); |
||
166 | $json_response = $client->request('GET', $this->getUrl(), []); |
||
167 | $response = json_decode( $json_response->getBody() ); |
||
168 | } |
||
169 | catch (RequestException $e) { |
||
170 | throw new RuntimeException('The request to the API failed: '.$e->getMessage(), $e->getCode(), $e); |
||
171 | } |
||
172 | |||
173 | if($response->{'ok'} === FALSE) { |
||
174 | throw new RuntimeException('The request to the API failed: '.$response->{'error'}."."); |
||
175 | } |
||
176 | |||
177 | return $json_response->getBody(); |
||
178 | } |
||
179 | |||
180 | |||
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | * |
||
185 | * @param string $file |
||
186 | * @return Files |
||
187 | */ |
||
188 | public function revokePublicURL($file) { |
||
218 | |||
219 | |||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | * |
||
224 | * @param string $file |
||
225 | * @return Files |
||
226 | */ |
||
227 | public function sharedPublicURL($file) { |
||
257 | |||
258 | |||
259 | |||
260 | /** |
||
261 | * {@inheritdoc} |
||
262 | * |
||
263 | * @param string $filename |
||
264 | * @param string $path_to_file |
||
265 | * @param string $filetype |
||
266 | * @param string $title |
||
267 | * @param string $initial_comment |
||
268 | * @param string $channels |
||
269 | * @return Files |
||
270 | */ |
||
271 | public function upload($filename, $path_to_file = NULL, $filetype = NULL, $title = NULL, |
||
337 | } |
||
338 |
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.