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 Share 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 Share, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Share extends AbstractShare { |
||
17 | /** |
||
18 | * @var Server $server |
||
19 | */ |
||
20 | private $server; |
||
21 | |||
22 | /** |
||
23 | * @var string $name |
||
24 | */ |
||
25 | private $name; |
||
26 | |||
27 | /** |
||
28 | * @var Connection $connection |
||
29 | */ |
||
30 | public $connection; |
||
31 | |||
32 | /** |
||
33 | * @var \Icewind\SMB\Parser |
||
34 | */ |
||
35 | protected $parser; |
||
36 | |||
37 | /** |
||
38 | * @var \Icewind\SMB\System |
||
39 | */ |
||
40 | private $system; |
||
41 | |||
42 | /** |
||
43 | * @param Server $server |
||
44 | * @param string $name |
||
45 | */ |
||
46 | public function __construct($server, $name) { |
||
53 | |||
54 | protected function getConnection() { |
||
69 | |||
70 | /** |
||
71 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
72 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
73 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
74 | */ |
||
75 | protected function connect() { |
||
81 | |||
82 | protected function reconnect() { |
||
89 | |||
90 | /** |
||
91 | * Get the name of the share |
||
92 | * |
||
93 | * @return string |
||
94 | */ |
||
95 | public function getName() { |
||
98 | |||
99 | protected function simpleCommand($command, $path) { |
||
105 | |||
106 | /** |
||
107 | * List the content of a remote folder |
||
108 | * |
||
109 | * @param $path |
||
110 | * @return \Icewind\SMB\IFileInfo[] |
||
111 | * |
||
112 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
113 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
114 | */ |
||
115 | public function dir($path) { |
||
125 | |||
126 | /** |
||
127 | * @param string $path |
||
128 | * @return \Icewind\SMB\IFileInfo[] |
||
129 | */ |
||
130 | public function stat($path) { |
||
145 | |||
146 | /** |
||
147 | * Create a folder on the share |
||
148 | * |
||
149 | * @param string $path |
||
150 | * @return bool |
||
151 | * |
||
152 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
153 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
154 | */ |
||
155 | public function mkdir($path) { |
||
158 | |||
159 | /** |
||
160 | * Remove a folder on the share |
||
161 | * |
||
162 | * @param string $path |
||
163 | * @return bool |
||
164 | * |
||
165 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
166 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
167 | */ |
||
168 | public function rmdir($path) { |
||
171 | |||
172 | /** |
||
173 | * Delete a file on the share |
||
174 | * |
||
175 | * @param string $path |
||
176 | * @param bool $secondTry |
||
177 | * @return bool |
||
178 | * @throws InvalidTypeException |
||
179 | * @throws NotFoundException |
||
180 | * @throws \Exception |
||
181 | */ |
||
182 | public function del($path, $secondTry = false) { |
||
205 | |||
206 | /** |
||
207 | * Rename a remote file |
||
208 | * |
||
209 | * @param string $from |
||
210 | * @param string $to |
||
211 | * @return bool |
||
212 | * |
||
213 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
214 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
215 | */ |
||
216 | View Code Duplication | public function rename($from, $to) { |
|
223 | |||
224 | /** |
||
225 | * Upload a local file |
||
226 | * |
||
227 | * @param string $source local file |
||
228 | * @param string $target remove file |
||
229 | * @return bool |
||
230 | * |
||
231 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
232 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
233 | */ |
||
234 | View Code Duplication | public function put($source, $target) { |
|
240 | |||
241 | /** |
||
242 | * Download a remote file |
||
243 | * |
||
244 | * @param string $source remove file |
||
245 | * @param string $target local file |
||
246 | * @return bool |
||
247 | * |
||
248 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
249 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
250 | */ |
||
251 | View Code Duplication | public function get($source, $target) { |
|
257 | |||
258 | /** |
||
259 | * Open a readable stream to a remote file |
||
260 | * |
||
261 | * @param string $source |
||
262 | * @return resource a read only stream with the contents of the remote file |
||
263 | * |
||
264 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
265 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
266 | */ |
||
267 | public function read($source) { |
||
286 | |||
287 | /** |
||
288 | * Open a writable stream to a remote file |
||
289 | * |
||
290 | * @param string $target |
||
291 | * @return resource a write only stream to upload a remote file |
||
292 | * |
||
293 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
294 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
295 | */ |
||
296 | public function write($target) { |
||
320 | |||
321 | /** |
||
322 | * @param string $path |
||
323 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
324 | * @return mixed |
||
325 | */ |
||
326 | public function setMode($path, $mode) { |
||
351 | |||
352 | /** |
||
353 | * @param string $path |
||
354 | * @param callable $callback callable which will be called for each received change |
||
355 | * @return mixed |
||
356 | */ |
||
357 | public function notify($path, callable $callback) { |
||
371 | |||
372 | /** |
||
373 | * @param string $command |
||
374 | * @return array |
||
375 | */ |
||
376 | protected function execute($command) { |
||
382 | |||
383 | /** |
||
384 | * check output for errors |
||
385 | * |
||
386 | * @param string[] $lines |
||
387 | * @param string $path |
||
388 | * |
||
389 | * @throws NotFoundException |
||
390 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
391 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
392 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
393 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
394 | * @throws \Icewind\SMB\Exception\Exception |
||
395 | * @return bool |
||
396 | */ |
||
397 | protected function parseOutput($lines, $path = '') { |
||
400 | |||
401 | /** |
||
402 | * @param string $string |
||
403 | * @return string |
||
404 | */ |
||
405 | protected function escape($string) { |
||
408 | |||
409 | /** |
||
410 | * @param string $path |
||
411 | * @return string |
||
412 | */ |
||
413 | protected function escapePath($path) { |
||
422 | |||
423 | /** |
||
424 | * @param string $path |
||
425 | * @return string |
||
426 | */ |
||
427 | protected function escapeLocalPath($path) { |
||
431 | |||
432 | public function __destruct() { |
||
435 | } |
||
436 |
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.