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 |
||
24 | class Share extends AbstractShare { |
||
25 | /** |
||
26 | * @var IServer $server |
||
27 | */ |
||
28 | private $server; |
||
29 | |||
30 | /** |
||
31 | * @var string $name |
||
32 | */ |
||
33 | private $name; |
||
34 | |||
35 | /** |
||
36 | * @var Connection $connection |
||
37 | */ |
||
38 | public $connection; |
||
39 | |||
40 | /** |
||
41 | * @var Parser |
||
42 | */ |
||
43 | protected $parser; |
||
44 | |||
45 | /** |
||
46 | * @var ISystem |
||
47 | */ |
||
48 | private $system; |
||
49 | |||
50 | const MODE_MAP = [ |
||
51 | FileInfo::MODE_READONLY => 'r', |
||
52 | FileInfo::MODE_HIDDEN => 'h', |
||
53 | FileInfo::MODE_ARCHIVE => 'a', |
||
54 | FileInfo::MODE_SYSTEM => 's' |
||
55 | ]; |
||
56 | |||
57 | /** |
||
58 | * @param IServer $server |
||
59 | * @param string $name |
||
60 | 512 | * @param ISystem $system |
|
61 | 512 | */ |
|
62 | 512 | public function __construct(IServer $server, $name, ISystem $system) { |
|
69 | 510 | ||
70 | 510 | private function getAuthFileArgument() { |
|
77 | 510 | ||
78 | 510 | protected function getConnection() { |
|
98 | |||
99 | /** |
||
100 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
101 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
102 | 508 | * @throws \Icewind\SMB\Exception\InvalidHostException |
|
103 | 508 | */ |
|
104 | 508 | protected function connect() { |
|
110 | 2 | ||
111 | 2 | protected function reconnect() { |
|
117 | |||
118 | /** |
||
119 | * Get the name of the share |
||
120 | * |
||
121 | 4 | * @return string |
|
122 | 4 | */ |
|
123 | public function getName() { |
||
126 | 508 | ||
127 | 508 | protected function simpleCommand($command, $path) { |
|
133 | |||
134 | /** |
||
135 | * List the content of a remote folder |
||
136 | * |
||
137 | * @param $path |
||
138 | * @return \Icewind\SMB\IFileInfo[] |
||
139 | * |
||
140 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
141 | 500 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
142 | 500 | */ |
|
143 | 500 | public function dir($path) { |
|
154 | |||
155 | /** |
||
156 | * @param string $path |
||
157 | 66 | * @return \Icewind\SMB\IFileInfo |
|
158 | */ |
||
159 | public function stat($path) { |
||
187 | |||
188 | /** |
||
189 | * Create a folder on the share |
||
190 | * |
||
191 | * @param string $path |
||
192 | * @return bool |
||
193 | * |
||
194 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
195 | 502 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
|
196 | 502 | */ |
|
197 | public function mkdir($path) { |
||
200 | |||
201 | /** |
||
202 | * Remove a folder on the share |
||
203 | * |
||
204 | * @param string $path |
||
205 | * @return bool |
||
206 | * |
||
207 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
208 | 502 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
209 | 502 | */ |
|
210 | public function rmdir($path) { |
||
213 | |||
214 | /** |
||
215 | * Delete a file on the share |
||
216 | * |
||
217 | * @param string $path |
||
218 | * @param bool $secondTry |
||
219 | * @return bool |
||
220 | * @throws InvalidTypeException |
||
221 | * @throws NotFoundException |
||
222 | 262 | * @throws \Exception |
|
223 | */ |
||
224 | public function del($path, $secondTry = false) { |
||
247 | |||
248 | /** |
||
249 | * Rename a remote file |
||
250 | * |
||
251 | * @param string $from |
||
252 | * @param string $to |
||
253 | * @return bool |
||
254 | * |
||
255 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
256 | 60 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
|
257 | 60 | */ |
|
258 | 42 | View Code Duplication | public function rename($from, $to) { |
264 | |||
265 | /** |
||
266 | * Upload a local file |
||
267 | * |
||
268 | * @param string $source local file |
||
269 | * @param string $target remove file |
||
270 | * @return bool |
||
271 | * |
||
272 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
273 | 230 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
274 | 230 | */ |
|
275 | 230 | View Code Duplication | public function put($source, $target) { |
281 | |||
282 | /** |
||
283 | * Download a remote file |
||
284 | * |
||
285 | * @param string $source remove file |
||
286 | * @param string $target local file |
||
287 | * @return bool |
||
288 | * |
||
289 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
290 | 88 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
291 | 88 | */ |
|
292 | 70 | View Code Duplication | public function get($source, $target) { |
298 | |||
299 | /** |
||
300 | * Open a readable stream to a remote file |
||
301 | * |
||
302 | * @param string $source |
||
303 | * @return resource a read only stream with the contents of the remote file |
||
304 | * |
||
305 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
306 | 50 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
307 | 50 | */ |
|
308 | public function read($source) { |
||
320 | |||
321 | /** |
||
322 | * Open a writable stream to a remote file |
||
323 | * |
||
324 | * @param string $target |
||
325 | * @return resource a write only stream to upload a remote file |
||
326 | * |
||
327 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
328 | 50 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
329 | 50 | */ |
|
330 | public function write($target) { |
||
346 | |||
347 | /** |
||
348 | * Append to stream |
||
349 | * Note: smbclient does not support this (Use php-libsmbclient) |
||
350 | 16 | * |
|
351 | 16 | * @param string $target |
|
352 | 16 | * |
|
353 | 16 | * @throws \Icewind\SMB\Exception\DependencyException |
|
354 | 16 | */ |
|
355 | 8 | public function append($target) { |
|
358 | |||
359 | /** |
||
360 | 16 | * @param string $path |
|
361 | 16 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
|
362 | 16 | * @return mixed |
|
363 | */ |
||
364 | 16 | public function setMode($path, $mode) { |
|
387 | 10 | ||
388 | /** |
||
389 | * @param string $path |
||
390 | * @return INotifyHandler |
||
391 | * @throws ConnectionException |
||
392 | * @throws DependencyException |
||
393 | */ |
||
394 | 508 | public function notify($path) { |
|
403 | |||
404 | /** |
||
405 | * @param string $command |
||
406 | * @return array |
||
407 | */ |
||
408 | protected function execute($command) { |
||
413 | |||
414 | 508 | /** |
|
415 | 508 | * check output for errors |
|
416 | 508 | * |
|
417 | * @param string[] $lines |
||
418 | 40 | * @param string $path |
|
419 | * |
||
420 | * @throws NotFoundException |
||
421 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
422 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
423 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
424 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
425 | * @throws \Icewind\SMB\Exception\Exception |
||
426 | * @return bool |
||
427 | */ |
||
428 | protected function parseOutput($lines, $path = '') { |
||
436 | 510 | ||
437 | 510 | /** |
|
438 | 2 | * @param string $string |
|
439 | 1 | * @return string |
|
440 | 510 | */ |
|
441 | 510 | protected function escape($string) { |
|
444 | |||
445 | /** |
||
446 | * @param string $path |
||
447 | * @return string |
||
448 | */ |
||
449 | protected function escapePath($path) { |
||
459 | |||
460 | /** |
||
461 | * @param string $path |
||
462 | * @return string |
||
463 | */ |
||
464 | protected function escapeLocalPath($path) { |
||
468 | |||
469 | public function __destruct() { |
||
472 | } |
||
473 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: