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 |
||
26 | class Share extends AbstractShare { |
||
27 | /** |
||
28 | * @var IServer $server |
||
29 | */ |
||
30 | private $server; |
||
31 | |||
32 | /** |
||
33 | * @var string $name |
||
34 | */ |
||
35 | private $name; |
||
36 | |||
37 | /** |
||
38 | * @var Connection $connection |
||
39 | */ |
||
40 | public $connection; |
||
41 | |||
42 | /** |
||
43 | * @var Parser |
||
44 | */ |
||
45 | protected $parser; |
||
46 | |||
47 | /** |
||
48 | * @var ISystem |
||
49 | */ |
||
50 | private $system; |
||
51 | |||
52 | const MODE_MAP = [ |
||
53 | FileInfo::MODE_READONLY => 'r', |
||
54 | FileInfo::MODE_HIDDEN => 'h', |
||
55 | FileInfo::MODE_ARCHIVE => 'a', |
||
56 | FileInfo::MODE_SYSTEM => 's' |
||
57 | ]; |
||
58 | |||
59 | const EXEC_CMD = 'exec'; |
||
60 | |||
61 | /** |
||
62 | * @param IServer $server |
||
63 | * @param string $name |
||
64 | * @param ISystem $system |
||
65 | */ |
||
66 | 488 | public function __construct(IServer $server, $name, ISystem $system) { |
|
73 | |||
74 | 486 | private function getAuthFileArgument() { |
|
81 | |||
82 | 486 | protected function getConnection() { |
|
107 | |||
108 | /** |
||
109 | 484 | * @throws \Icewind\SMB\Exception\ConnectionException |
|
110 | 484 | * @throws \Icewind\SMB\Exception\AuthenticationException |
|
111 | 484 | * @throws \Icewind\SMB\Exception\InvalidHostException |
|
112 | */ |
||
113 | 484 | protected function connect() { |
|
119 | |||
120 | protected function reconnect() { |
||
126 | |||
127 | /** |
||
128 | 4 | * Get the name of the share |
|
129 | 4 | * |
|
130 | * @return string |
||
131 | */ |
||
132 | 484 | public function getName() { |
|
135 | 484 | ||
136 | 484 | protected function simpleCommand($command, $path) { |
|
142 | |||
143 | /** |
||
144 | * List the content of a remote folder |
||
145 | * |
||
146 | * @param string $path |
||
147 | * @return \Icewind\SMB\IFileInfo[] |
||
148 | 484 | * |
|
149 | 484 | * @throws \Icewind\SMB\Exception\NotFoundException |
|
150 | 484 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
151 | */ |
||
152 | 484 | public function dir($path) { |
|
165 | |||
166 | 50 | /** |
|
167 | * @param string $path |
||
168 | * @return \Icewind\SMB\IFileInfo |
||
169 | 50 | */ |
|
170 | 48 | public function stat($path) { |
|
200 | |||
201 | /** |
||
202 | * Create a folder on the share |
||
203 | * |
||
204 | * @param string $path |
||
205 | * @return bool |
||
206 | 484 | * |
|
207 | 484 | * @throws \Icewind\SMB\Exception\NotFoundException |
|
208 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
209 | */ |
||
210 | public function mkdir($path) { |
||
213 | |||
214 | /** |
||
215 | * Remove a folder on the share |
||
216 | * |
||
217 | * @param string $path |
||
218 | * @return bool |
||
219 | 482 | * |
|
220 | 482 | * @throws \Icewind\SMB\Exception\NotFoundException |
|
221 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
222 | */ |
||
223 | public function rmdir($path) { |
||
226 | |||
227 | /** |
||
228 | * Delete a file on the share |
||
229 | * |
||
230 | * @param string $path |
||
231 | * @param bool $secondTry |
||
232 | * @return bool |
||
233 | 236 | * @throws InvalidTypeException |
|
234 | * @throws NotFoundException |
||
235 | * @throws \Exception |
||
236 | */ |
||
237 | 236 | public function del($path, $secondTry = false) { |
|
260 | |||
261 | /** |
||
262 | * Rename a remote file |
||
263 | * |
||
264 | * @param string $from |
||
265 | * @param string $to |
||
266 | * @return bool |
||
267 | 56 | * |
|
268 | 56 | * @throws \Icewind\SMB\Exception\NotFoundException |
|
269 | 38 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
|
270 | 38 | */ |
|
271 | 38 | View Code Duplication | public function rename($from, $to) { |
272 | $path1 = $this->escapePath($from); |
||
273 | $path2 = $this->escapePath($to); |
||
274 | $output = $this->execute('rename ' . $path1 . ' ' . $path2); |
||
275 | return $this->parseOutput($output, $to); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Upload a local file |
||
280 | * |
||
281 | * @param string $source local file |
||
282 | * @param string $target remove file |
||
283 | * @return bool |
||
284 | 206 | * |
|
285 | 206 | * @throws \Icewind\SMB\Exception\NotFoundException |
|
286 | 206 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
287 | 188 | */ |
|
288 | 188 | View Code Duplication | public function put($source, $target) { |
289 | $path1 = $this->escapeLocalPath($source); //first path is local, needs different escaping |
||
290 | $path2 = $this->escapePath($target); |
||
291 | $output = $this->execute('put ' . $path1 . ' ' . $path2); |
||
292 | return $this->parseOutput($output, $target); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Download a remote file |
||
297 | * |
||
298 | * @param string $source remove file |
||
299 | * @param string $target local file |
||
300 | * @return bool |
||
301 | 88 | * |
|
302 | 88 | * @throws \Icewind\SMB\Exception\NotFoundException |
|
303 | 70 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
304 | 70 | */ |
|
305 | 70 | View Code Duplication | public function get($source, $target) { |
306 | $path1 = $this->escapePath($source); |
||
307 | $path2 = $this->escapeLocalPath($target); //second path is local, needs different escaping |
||
308 | $output = $this->execute('get ' . $path1 . ' ' . $path2); |
||
309 | return $this->parseOutput($output, $source); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * Open a readable stream to a remote file |
||
314 | * |
||
315 | * @param string $source |
||
316 | * @return resource a read only stream with the contents of the remote file |
||
317 | 50 | * |
|
318 | 50 | * @throws \Icewind\SMB\Exception\NotFoundException |
|
319 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
320 | */ |
||
321 | 32 | public function read($source) { |
|
333 | |||
334 | /** |
||
335 | * Open a writable stream to a remote file |
||
336 | * |
||
337 | * @param string $target |
||
338 | * @return resource a write only stream to upload a remote file |
||
339 | 50 | * |
|
340 | 50 | * @throws \Icewind\SMB\Exception\NotFoundException |
|
341 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
342 | */ |
||
343 | 32 | public function write($target) { |
|
359 | |||
360 | /** |
||
361 | * Append to stream |
||
362 | * Note: smbclient does not support this (Use php-libsmbclient) |
||
363 | * |
||
364 | 2 | * @param string $target |
|
365 | 2 | * |
|
366 | * @throws \Icewind\SMB\Exception\DependencyException |
||
367 | */ |
||
368 | public function append($target) { |
||
371 | |||
372 | /** |
||
373 | * @param string $path |
||
374 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
375 | * @return mixed |
||
376 | */ |
||
377 | public function setMode($path, $mode) { |
||
400 | |||
401 | /** |
||
402 | * @param string $path |
||
403 | 2 | * @return INotifyHandler |
|
404 | 2 | * @throws ConnectionException |
|
405 | * @throws DependencyException |
||
406 | */ |
||
407 | 2 | public function notify($path) { |
|
416 | |||
417 | 484 | /** |
|
418 | 484 | * @param string $command |
|
419 | 484 | * @return array |
|
420 | 484 | */ |
|
421 | protected function execute($command) { |
||
426 | |||
427 | /** |
||
428 | * check output for errors |
||
429 | * |
||
430 | * @param string[] $lines |
||
431 | * @param string $path |
||
432 | * |
||
433 | * @return bool |
||
434 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
435 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
436 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
437 | 484 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
|
438 | 484 | * @throws \Icewind\SMB\Exception\Exception |
|
439 | 484 | * @throws NotFoundException |
|
440 | */ |
||
441 | 38 | protected function parseOutput($lines, $path = '') { |
|
449 | |||
450 | /** |
||
451 | * @param string $string |
||
452 | * @return string |
||
453 | */ |
||
454 | protected function escape($string) { |
||
457 | |||
458 | 486 | /** |
|
459 | 486 | * @param string $path |
|
460 | 486 | * @return string |
|
461 | 2 | */ |
|
462 | protected function escapePath($path) { |
||
472 | |||
473 | 242 | /** |
|
474 | 242 | * @param string $path |
|
475 | 242 | * @return string |
|
476 | */ |
||
477 | protected function escapeLocalPath($path) { |
||
481 | |||
482 | protected function getAcls($path) { |
||
483 | $commandPath = $this->system->getSmbcAclsPath(); |
||
484 | if (!$commandPath) { |
||
507 | |||
508 | public function getServer(): IServer { |
||
511 | |||
512 | public function __destruct() { |
||
515 | } |
||
516 |
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: