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 |
||
22 | class Share extends AbstractShare { |
||
23 | /** |
||
24 | * @var IServer $server |
||
25 | */ |
||
26 | private $server; |
||
27 | |||
28 | /** |
||
29 | * @var string $name |
||
30 | */ |
||
31 | private $name; |
||
32 | |||
33 | /** |
||
34 | * @var Connection $connection |
||
35 | */ |
||
36 | public $connection; |
||
37 | |||
38 | /** |
||
39 | * @var Parser |
||
40 | */ |
||
41 | protected $parser; |
||
42 | |||
43 | /** |
||
44 | * @var System |
||
45 | */ |
||
46 | private $system; |
||
47 | |||
48 | const MODE_MAP = [ |
||
49 | FileInfo::MODE_READONLY => 'r', |
||
50 | FileInfo::MODE_HIDDEN => 'h', |
||
51 | FileInfo::MODE_ARCHIVE => 'a', |
||
52 | FileInfo::MODE_SYSTEM => 's' |
||
53 | ]; |
||
54 | |||
55 | /** |
||
56 | * @param IServer $server |
||
57 | * @param string $name |
||
58 | * @param System $system |
||
59 | */ |
||
60 | 1024 | public function __construct(IServer $server, $name, System $system = null) { |
|
61 | 1024 | parent::__construct(); |
|
62 | 1024 | $this->server = $server; |
|
63 | 1024 | $this->name = $name; |
|
64 | 1024 | $this->system = (!is_null($system)) ? $system : new System(); |
|
65 | 1024 | $this->parser = new Parser(new TimeZoneProvider($this->server->getHost(), $this->system)); |
|
66 | 1024 | } |
|
67 | |||
68 | 1020 | private function getAuthFileArgument() { |
|
75 | |||
76 | 1020 | protected function getConnection() { |
|
77 | 1020 | $command = sprintf('%s%s %s %s %s', |
|
78 | 1020 | $this->system->hasStdBuf() ? 'stdbuf -o0 ' : '', |
|
79 | 1020 | $this->system->getSmbclientPath(), |
|
80 | 1020 | $this->getAuthFileArgument(), |
|
81 | 1020 | $this->server->getAuth()->getExtraCommandLineArguments(), |
|
82 | 1020 | escapeshellarg('//' . $this->server->getHost() . '/' . $this->name) |
|
83 | 255 | ); |
|
84 | 1020 | $connection = new Connection($command, $this->parser); |
|
85 | 1020 | $connection->writeAuthentication($this->server->getAuth()->getUsername(), $this->server->getAuth()->getPassword()); |
|
86 | 1020 | $connection->connect(); |
|
87 | 1020 | if (!$connection->isValid()) { |
|
88 | throw new ConnectionException($connection->readLine()); |
||
89 | } |
||
90 | // some versions of smbclient add a help message in first of the first prompt |
||
91 | 1020 | $connection->clearTillPrompt(); |
|
92 | 1020 | return $connection; |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
97 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
98 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
99 | */ |
||
100 | 1016 | protected function connect() { |
|
106 | |||
107 | 4 | protected function reconnect() { |
|
113 | |||
114 | /** |
||
115 | * Get the name of the share |
||
116 | * |
||
117 | * @return string |
||
118 | */ |
||
119 | 8 | public function getName() { |
|
122 | |||
123 | 1016 | protected function simpleCommand($command, $path) { |
|
129 | |||
130 | /** |
||
131 | * List the content of a remote folder |
||
132 | * |
||
133 | * @param $path |
||
134 | * @return \Icewind\SMB\IFileInfo[] |
||
135 | * |
||
136 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
137 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
138 | */ |
||
139 | 1000 | public function dir($path) { |
|
150 | |||
151 | /** |
||
152 | * @param string $path |
||
153 | * @return \Icewind\SMB\IFileInfo |
||
154 | */ |
||
155 | 132 | public function stat($path) { |
|
183 | |||
184 | /** |
||
185 | * Create a folder on the share |
||
186 | * |
||
187 | * @param string $path |
||
188 | * @return bool |
||
189 | * |
||
190 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
191 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
192 | */ |
||
193 | 1004 | public function mkdir($path) { |
|
196 | |||
197 | /** |
||
198 | * Remove a folder on the share |
||
199 | * |
||
200 | * @param string $path |
||
201 | * @return bool |
||
202 | * |
||
203 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
204 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
205 | */ |
||
206 | 1004 | public function rmdir($path) { |
|
209 | |||
210 | /** |
||
211 | * Delete a file on the share |
||
212 | * |
||
213 | * @param string $path |
||
214 | * @param bool $secondTry |
||
215 | * @return bool |
||
216 | * @throws InvalidTypeException |
||
217 | * @throws NotFoundException |
||
218 | * @throws \Exception |
||
219 | */ |
||
220 | 524 | public function del($path, $secondTry = false) { |
|
243 | |||
244 | /** |
||
245 | * Rename a remote file |
||
246 | * |
||
247 | * @param string $from |
||
248 | * @param string $to |
||
249 | * @return bool |
||
250 | * |
||
251 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
252 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
253 | */ |
||
254 | 120 | View Code Duplication | public function rename($from, $to) { |
260 | |||
261 | /** |
||
262 | * Upload a local file |
||
263 | * |
||
264 | * @param string $source local file |
||
265 | * @param string $target remove file |
||
266 | * @return bool |
||
267 | * |
||
268 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
269 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
270 | */ |
||
271 | 460 | View Code Duplication | public function put($source, $target) { |
277 | |||
278 | /** |
||
279 | * Download a remote file |
||
280 | * |
||
281 | * @param string $source remove file |
||
282 | * @param string $target local file |
||
283 | * @return bool |
||
284 | * |
||
285 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
286 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
287 | */ |
||
288 | 176 | View Code Duplication | public function get($source, $target) { |
294 | |||
295 | /** |
||
296 | * Open a readable stream to a remote file |
||
297 | * |
||
298 | * @param string $source |
||
299 | * @return resource a read only stream with the contents of the remote file |
||
300 | * |
||
301 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
302 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
303 | */ |
||
304 | 100 | public function read($source) { |
|
316 | |||
317 | /** |
||
318 | * Open a writable stream to a remote file |
||
319 | * |
||
320 | * @param string $target |
||
321 | * @return resource a write only stream to upload a remote file |
||
322 | * |
||
323 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
324 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
325 | */ |
||
326 | 100 | public function write($target) { |
|
342 | |||
343 | /** |
||
344 | * @param string $path |
||
345 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
346 | * @return mixed |
||
347 | */ |
||
348 | 32 | public function setMode($path, $mode) { |
|
371 | |||
372 | /** |
||
373 | * @param string $path |
||
374 | * @return INotifyHandler |
||
375 | * @throws ConnectionException |
||
376 | * @throws DependencyException |
||
377 | */ |
||
378 | 20 | public function notify($path) { |
|
387 | |||
388 | /** |
||
389 | * @param string $command |
||
390 | * @return array |
||
391 | */ |
||
392 | 1016 | protected function execute($command) { |
|
397 | |||
398 | /** |
||
399 | * check output for errors |
||
400 | * |
||
401 | * @param string[] $lines |
||
402 | * @param string $path |
||
403 | * |
||
404 | * @throws NotFoundException |
||
405 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
406 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
407 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
408 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
409 | * @throws \Icewind\SMB\Exception\Exception |
||
410 | * @return bool |
||
411 | */ |
||
412 | 1016 | protected function parseOutput($lines, $path = '') { |
|
420 | |||
421 | /** |
||
422 | * @param string $string |
||
423 | * @return string |
||
424 | */ |
||
425 | protected function escape($string) { |
||
428 | |||
429 | /** |
||
430 | * @param string $path |
||
431 | * @return string |
||
432 | */ |
||
433 | 1020 | protected function escapePath($path) { |
|
443 | |||
444 | /** |
||
445 | * @param string $path |
||
446 | * @return string |
||
447 | */ |
||
448 | 532 | protected function escapeLocalPath($path) { |
|
452 | |||
453 | 1024 | public function __destruct() { |
|
456 | } |
||
457 |
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: