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 |
||
25 | class Share extends AbstractShare { |
||
26 | /** |
||
27 | * @var IServer $server |
||
28 | */ |
||
29 | private $server; |
||
30 | |||
31 | /** |
||
32 | * @var string $name |
||
33 | */ |
||
34 | private $name; |
||
35 | |||
36 | /** |
||
37 | * @var Connection $connection |
||
38 | */ |
||
39 | public $connection; |
||
40 | |||
41 | /** |
||
42 | * @var Parser |
||
43 | */ |
||
44 | protected $parser; |
||
45 | |||
46 | /** |
||
47 | * @var ISystem |
||
48 | */ |
||
49 | private $system; |
||
50 | |||
51 | const MODE_MAP = [ |
||
52 | FileInfo::MODE_READONLY => 'r', |
||
53 | FileInfo::MODE_HIDDEN => 'h', |
||
54 | FileInfo::MODE_ARCHIVE => 'a', |
||
55 | FileInfo::MODE_SYSTEM => 's' |
||
56 | ]; |
||
57 | |||
58 | const EXEC_CMD = 'exec'; |
||
59 | |||
60 | /** |
||
61 | * @param IServer $server |
||
62 | * @param string $name |
||
63 | * @param ISystem $system |
||
64 | */ |
||
65 | 1028 | public function __construct(IServer $server, $name, ISystem $system) { |
|
66 | 1028 | parent::__construct(); |
|
67 | 1028 | $this->server = $server; |
|
68 | 1028 | $this->name = $name; |
|
69 | 1028 | $this->system = $system; |
|
70 | 1028 | $this->parser = new Parser($server->getTimeZone()); |
|
71 | 1028 | } |
|
72 | |||
73 | 1024 | private function getAuthFileArgument() { |
|
74 | 1024 | if ($this->server->getAuth()->getUsername()) { |
|
|
|||
75 | 1024 | return '--authentication-file=' . $this->system->getFD(3); |
|
76 | } else { |
||
77 | return ''; |
||
78 | } |
||
79 | } |
||
80 | |||
81 | 1024 | protected function getConnection() { |
|
82 | 1024 | $command = sprintf( |
|
83 | 1024 | '%s %s%s -t %s %s %s %s', |
|
84 | 1024 | self::EXEC_CMD, |
|
85 | 1024 | $this->system->getStdBufPath() ? $this->system->getStdBufPath() . ' -o0 ' : '', |
|
86 | 1024 | $this->system->getSmbclientPath(), |
|
87 | 1024 | $this->server->getOptions()->getTimeout(), |
|
88 | 1024 | $this->getAuthFileArgument(), |
|
89 | 1024 | $this->server->getAuth()->getExtraCommandLineArguments(), |
|
90 | 1024 | escapeshellarg('//' . $this->server->getHost() . '/' . $this->name) |
|
91 | 256 | ); |
|
92 | 1024 | $connection = new Connection($command, $this->parser); |
|
93 | 1024 | $connection->writeAuthentication($this->server->getAuth()->getUsername(), $this->server->getAuth()->getPassword()); |
|
94 | 1024 | $connection->connect(); |
|
95 | 1024 | if (!$connection->isValid()) { |
|
96 | throw new ConnectionException($connection->readLine()); |
||
97 | } |
||
98 | // some versions of smbclient add a help message in first of the first prompt |
||
99 | 1024 | $connection->clearTillPrompt(); |
|
100 | 1024 | return $connection; |
|
101 | } |
||
102 | |||
103 | /** |
||
104 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
105 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
106 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
107 | */ |
||
108 | 1020 | protected function connect() { |
|
109 | 1020 | if ($this->connection and $this->connection->isValid()) { |
|
110 | 1020 | return; |
|
111 | } |
||
112 | 1020 | $this->connection = $this->getConnection(); |
|
113 | 1020 | } |
|
114 | |||
115 | 4 | protected function reconnect() { |
|
116 | 4 | $this->connection->reconnect(); |
|
117 | 4 | if (!$this->connection->isValid()) { |
|
118 | throw new ConnectionException(); |
||
119 | } |
||
120 | 4 | } |
|
121 | |||
122 | /** |
||
123 | * Get the name of the share |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | 8 | public function getName() { |
|
130 | |||
131 | 1020 | protected function simpleCommand($command, $path) { |
|
132 | 1020 | $escapedPath = $this->escapePath($path); |
|
133 | 1020 | $cmd = $command . ' ' . $escapedPath; |
|
134 | 1020 | $output = $this->execute($cmd); |
|
135 | 1020 | return $this->parseOutput($output, $path); |
|
137 | |||
138 | /** |
||
139 | * List the content of a remote folder |
||
140 | * |
||
141 | * @param $path |
||
142 | * @return \Icewind\SMB\IFileInfo[] |
||
143 | * |
||
144 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
145 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
146 | */ |
||
147 | 1004 | public function dir($path) { |
|
158 | |||
159 | /** |
||
160 | * @param string $path |
||
161 | * @return \Icewind\SMB\IFileInfo |
||
162 | */ |
||
163 | 132 | public function stat($path) { |
|
191 | |||
192 | /** |
||
193 | * Create a folder on the share |
||
194 | * |
||
195 | * @param string $path |
||
196 | * @return bool |
||
197 | * |
||
198 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
199 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
200 | */ |
||
201 | 1008 | public function mkdir($path) { |
|
204 | |||
205 | /** |
||
206 | * Remove a folder on the share |
||
207 | * |
||
208 | * @param string $path |
||
209 | * @return bool |
||
210 | * |
||
211 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
212 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
213 | */ |
||
214 | 1008 | public function rmdir($path) { |
|
217 | |||
218 | /** |
||
219 | * Delete a file on the share |
||
220 | * |
||
221 | * @param string $path |
||
222 | * @param bool $secondTry |
||
223 | * @return bool |
||
224 | * @throws InvalidTypeException |
||
225 | * @throws NotFoundException |
||
226 | * @throws \Exception |
||
227 | */ |
||
228 | 524 | public function del($path, $secondTry = false) { |
|
251 | |||
252 | /** |
||
253 | * Rename a remote file |
||
254 | * |
||
255 | * @param string $from |
||
256 | * @param string $to |
||
257 | * @return bool |
||
258 | * |
||
259 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
260 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
261 | */ |
||
262 | 120 | View Code Duplication | public function rename($from, $to) { |
268 | |||
269 | /** |
||
270 | * Upload a local file |
||
271 | * |
||
272 | * @param string $source local file |
||
273 | * @param string $target remove file |
||
274 | * @return bool |
||
275 | * |
||
276 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
277 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
278 | */ |
||
279 | 460 | View Code Duplication | public function put($source, $target) { |
285 | |||
286 | /** |
||
287 | * Download a remote file |
||
288 | * |
||
289 | * @param string $source remove file |
||
290 | * @param string $target local file |
||
291 | * @return bool |
||
292 | * |
||
293 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
294 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
295 | */ |
||
296 | 176 | View Code Duplication | public function get($source, $target) { |
302 | |||
303 | /** |
||
304 | * Open a readable stream to a remote file |
||
305 | * |
||
306 | * @param string $source |
||
307 | * @return resource a read only stream with the contents of the remote file |
||
308 | * |
||
309 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
310 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
311 | */ |
||
312 | 100 | public function read($source) { |
|
324 | |||
325 | /** |
||
326 | * Open a writable stream to a remote file |
||
327 | * |
||
328 | * @param string $target |
||
329 | * @return resource a write only stream to upload a remote file |
||
330 | * |
||
331 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
332 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
333 | */ |
||
334 | 100 | public function write($target) { |
|
350 | |||
351 | /** |
||
352 | * Append to stream |
||
353 | * Note: smbclient does not support this (Use php-libsmbclient) |
||
354 | * |
||
355 | * @param string $target |
||
356 | * |
||
357 | * @throws \Icewind\SMB\Exception\DependencyException |
||
358 | */ |
||
359 | 4 | public function append($target) { |
|
362 | |||
363 | /** |
||
364 | * @param string $path |
||
365 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
366 | * @return mixed |
||
367 | */ |
||
368 | 32 | public function setMode($path, $mode) { |
|
391 | |||
392 | /** |
||
393 | * @param string $path |
||
394 | * @return INotifyHandler |
||
395 | * @throws ConnectionException |
||
396 | * @throws DependencyException |
||
397 | */ |
||
398 | 20 | public function notify($path) { |
|
407 | |||
408 | /** |
||
409 | * @param string $command |
||
410 | * @return array |
||
411 | */ |
||
412 | 1020 | protected function execute($command) { |
|
417 | |||
418 | /** |
||
419 | * check output for errors |
||
420 | * |
||
421 | * @param string[] $lines |
||
422 | * @param string $path |
||
423 | * |
||
424 | * @throws NotFoundException |
||
425 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
426 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
427 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
428 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
429 | * @throws \Icewind\SMB\Exception\Exception |
||
430 | * @return bool |
||
431 | */ |
||
432 | 1020 | protected function parseOutput($lines, $path = '') { |
|
440 | |||
441 | /** |
||
442 | * @param string $string |
||
443 | * @return string |
||
444 | */ |
||
445 | protected function escape($string) { |
||
448 | |||
449 | /** |
||
450 | * @param string $path |
||
451 | * @return string |
||
452 | */ |
||
453 | 1024 | protected function escapePath($path) { |
|
463 | |||
464 | /** |
||
465 | * @param string $path |
||
466 | * @return string |
||
467 | */ |
||
468 | 532 | protected function escapeLocalPath($path) { |
|
472 | |||
473 | 1028 | public function __destruct() { |
|
476 | } |
||
477 |
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: