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 | 768 | * @param ISystem $system |
|
61 | 768 | */ |
|
62 | 768 | public function __construct(IServer $server, $name, ISystem $system) { |
|
69 | 765 | ||
70 | 765 | private function getAuthFileArgument() { |
|
77 | 765 | ||
78 | 765 | protected function getConnection() { |
|
97 | |||
98 | /** |
||
99 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
100 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
101 | 762 | * @throws \Icewind\SMB\Exception\InvalidHostException |
|
102 | 762 | */ |
|
103 | 762 | protected function connect() { |
|
109 | 3 | ||
110 | 3 | protected function reconnect() { |
|
116 | |||
117 | /** |
||
118 | * Get the name of the share |
||
119 | * |
||
120 | 6 | * @return string |
|
121 | 6 | */ |
|
122 | public function getName() { |
||
125 | 762 | ||
126 | 762 | protected function simpleCommand($command, $path) { |
|
132 | |||
133 | /** |
||
134 | * List the content of a remote folder |
||
135 | * |
||
136 | * @param $path |
||
137 | * @return \Icewind\SMB\IFileInfo[] |
||
138 | * |
||
139 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
140 | 750 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
141 | 750 | */ |
|
142 | 750 | public function dir($path) { |
|
153 | |||
154 | /** |
||
155 | * @param string $path |
||
156 | 99 | * @return \Icewind\SMB\IFileInfo |
|
157 | 99 | */ |
|
158 | 72 | public function stat($path) { |
|
186 | |||
187 | /** |
||
188 | * Create a folder on the share |
||
189 | * |
||
190 | * @param string $path |
||
191 | * @return bool |
||
192 | * |
||
193 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
194 | 753 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
|
195 | 753 | */ |
|
196 | public function mkdir($path) { |
||
199 | |||
200 | /** |
||
201 | * Remove a folder on the share |
||
202 | * |
||
203 | * @param string $path |
||
204 | * @return bool |
||
205 | * |
||
206 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
207 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
208 | 393 | */ |
|
209 | public function rmdir($path) { |
||
212 | 393 | ||
213 | 36 | /** |
|
214 | * Delete a file on the share |
||
215 | * |
||
216 | 6 | * @param string $path |
|
217 | 6 | * @param bool $secondTry |
|
218 | 3 | * @return bool |
|
219 | 3 | * @throws InvalidTypeException |
|
220 | 3 | * @throws NotFoundException |
|
221 | * @throws \Exception |
||
222 | */ |
||
223 | 30 | public function del($path, $secondTry = false) { |
|
246 | 63 | ||
247 | /** |
||
248 | * Rename a remote file |
||
249 | * |
||
250 | * @param string $from |
||
251 | * @param string $to |
||
252 | * @return bool |
||
253 | * |
||
254 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
255 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
256 | */ |
||
257 | View Code Duplication | public function rename($from, $to) { |
|
263 | 318 | ||
264 | /** |
||
265 | * Upload a local file |
||
266 | * |
||
267 | * @param string $source local file |
||
268 | * @param string $target remove file |
||
269 | * @return bool |
||
270 | * |
||
271 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
272 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
273 | */ |
||
274 | View Code Duplication | public function put($source, $target) { |
|
280 | 105 | ||
281 | /** |
||
282 | * Download a remote file |
||
283 | * |
||
284 | * @param string $source remove file |
||
285 | * @param string $target local file |
||
286 | * @return bool |
||
287 | * |
||
288 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
289 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
290 | */ |
||
291 | View Code Duplication | public function get($source, $target) { |
|
297 | |||
298 | 48 | /** |
|
299 | 48 | * Open a readable stream to a remote file |
|
300 | 48 | * |
|
301 | 48 | * @param string $source |
|
302 | 48 | * @return resource a read only stream with the contents of the remote file |
|
303 | * |
||
304 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
305 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
306 | */ |
||
307 | public function read($source) { |
||
319 | |||
320 | 48 | /** |
|
321 | 48 | * Open a writable stream to a remote file |
|
322 | 48 | * |
|
323 | * @param string $target |
||
324 | * @return resource a write only stream to upload a remote file |
||
325 | * |
||
326 | 48 | * @throws \Icewind\SMB\Exception\NotFoundException |
|
327 | 48 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
328 | 48 | */ |
|
329 | public function write($target) { |
||
345 | |||
346 | 24 | /** |
|
347 | 24 | * Append to stream |
|
348 | 24 | * Note: smbclient does not support it so we use php-libsmbclient |
|
349 | * |
||
350 | 24 | * @param string $target |
|
351 | * |
||
352 | 24 | * @throws \Icewind\SMB\Exception\DependencyException |
|
353 | 24 | * @throws \Icewind\SMB\Exception\NotFoundException |
|
354 | 24 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
355 | */ |
||
356 | 24 | public function append($target) { |
|
364 | |||
365 | /** |
||
366 | 15 | * @param string $path |
|
367 | 15 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
|
368 | * @return mixed |
||
369 | */ |
||
370 | 15 | public function setMode($path, $mode) { |
|
393 | |||
394 | /** |
||
395 | * @param string $path |
||
396 | * @return INotifyHandler |
||
397 | * @throws ConnectionException |
||
398 | * @throws DependencyException |
||
399 | */ |
||
400 | 762 | public function notify($path) { |
|
409 | |||
410 | /** |
||
411 | * @param string $command |
||
412 | * @return array |
||
413 | */ |
||
414 | protected function execute($command) { |
||
419 | |||
420 | /** |
||
421 | 765 | * check output for errors |
|
422 | 765 | * |
|
423 | 765 | * @param string[] $lines |
|
424 | 3 | * @param string $path |
|
425 | * |
||
426 | 765 | * @throws NotFoundException |
|
427 | 765 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
|
428 | 765 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
|
429 | 765 | * @throws \Icewind\SMB\Exception\NotEmptyException |
|
430 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
431 | * @throws \Icewind\SMB\Exception\Exception |
||
432 | * @return bool |
||
433 | */ |
||
434 | protected function parseOutput($lines, $path = '') { |
||
442 | 768 | ||
443 | 768 | /** |
|
444 | * @param string $string |
||
445 | * @return string |
||
446 | */ |
||
447 | protected function escape($string) { |
||
450 | |||
451 | /** |
||
452 | * @param string $path |
||
453 | * @return string |
||
454 | */ |
||
455 | protected function escapePath($path) { |
||
465 | |||
466 | /** |
||
467 | * @param string $path |
||
468 | * @return string |
||
469 | */ |
||
470 | protected function escapeLocalPath($path) { |
||
474 | |||
475 | public function __destruct() { |
||
478 | } |
||
479 |
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: