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 ISystem |
||
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 ISystem $system |
||
59 | */ |
||
60 | 1024 | public function __construct(IServer $server, $name, ISystem $system) { |
|
67 | |||
68 | 1020 | private function getAuthFileArgument() { |
|
75 | |||
76 | 1020 | protected function getConnection() { |
|
96 | |||
97 | /** |
||
98 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
99 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
100 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
101 | */ |
||
102 | 1016 | protected function connect() { |
|
108 | |||
109 | 4 | protected function reconnect() { |
|
115 | |||
116 | /** |
||
117 | * Get the name of the share |
||
118 | * |
||
119 | * @return string |
||
120 | */ |
||
121 | 8 | public function getName() { |
|
124 | |||
125 | 1016 | protected function simpleCommand($command, $path) { |
|
131 | |||
132 | /** |
||
133 | * List the content of a remote folder |
||
134 | * |
||
135 | * @param $path |
||
136 | * @return \Icewind\SMB\IFileInfo[] |
||
137 | * |
||
138 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
139 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
140 | */ |
||
141 | 1000 | public function dir($path) { |
|
152 | |||
153 | /** |
||
154 | * @param string $path |
||
155 | * @return \Icewind\SMB\IFileInfo |
||
156 | */ |
||
157 | 132 | public function stat($path) { |
|
185 | |||
186 | /** |
||
187 | * Create a folder on the share |
||
188 | * |
||
189 | * @param string $path |
||
190 | * @return bool |
||
191 | * |
||
192 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
193 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
194 | */ |
||
195 | 1004 | public function mkdir($path) { |
|
198 | |||
199 | /** |
||
200 | * Remove a folder on the share |
||
201 | * |
||
202 | * @param string $path |
||
203 | * @return bool |
||
204 | * |
||
205 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
206 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
207 | */ |
||
208 | 1004 | public function rmdir($path) { |
|
211 | |||
212 | /** |
||
213 | * Delete a file on the share |
||
214 | * |
||
215 | * @param string $path |
||
216 | * @param bool $secondTry |
||
217 | * @return bool |
||
218 | * @throws InvalidTypeException |
||
219 | * @throws NotFoundException |
||
220 | * @throws \Exception |
||
221 | */ |
||
222 | 524 | public function del($path, $secondTry = false) { |
|
245 | |||
246 | /** |
||
247 | * Rename a remote file |
||
248 | * |
||
249 | * @param string $from |
||
250 | * @param string $to |
||
251 | * @return bool |
||
252 | * |
||
253 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
254 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
255 | */ |
||
256 | 120 | View Code Duplication | public function rename($from, $to) { |
262 | |||
263 | /** |
||
264 | * Upload a local file |
||
265 | * |
||
266 | * @param string $source local file |
||
267 | * @param string $target remove file |
||
268 | * @return bool |
||
269 | * |
||
270 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
271 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
272 | */ |
||
273 | 460 | View Code Duplication | public function put($source, $target) { |
279 | |||
280 | /** |
||
281 | * Download a remote file |
||
282 | * |
||
283 | * @param string $source remove file |
||
284 | * @param string $target local file |
||
285 | * @return bool |
||
286 | * |
||
287 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
288 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
289 | */ |
||
290 | 176 | View Code Duplication | public function get($source, $target) { |
296 | |||
297 | /** |
||
298 | * Open a readable stream to a remote file |
||
299 | * |
||
300 | * @param string $source |
||
301 | * @return resource a read only stream with the contents of the remote file |
||
302 | * |
||
303 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
304 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
305 | */ |
||
306 | 100 | public function read($source) { |
|
318 | |||
319 | /** |
||
320 | * Open a writable stream to a remote file |
||
321 | * |
||
322 | * @param string $target |
||
323 | * @return resource a write only stream to upload a remote file |
||
324 | * |
||
325 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
326 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
327 | */ |
||
328 | 100 | public function write($target) { |
|
344 | |||
345 | /** |
||
346 | * @param string $path |
||
347 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
348 | * @return mixed |
||
349 | */ |
||
350 | 32 | public function setMode($path, $mode) { |
|
373 | |||
374 | /** |
||
375 | * @param string $path |
||
376 | * @return INotifyHandler |
||
377 | * @throws ConnectionException |
||
378 | * @throws DependencyException |
||
379 | */ |
||
380 | 20 | public function notify($path) { |
|
389 | |||
390 | /** |
||
391 | * @param string $command |
||
392 | * @return array |
||
393 | */ |
||
394 | 1016 | protected function execute($command) { |
|
399 | |||
400 | /** |
||
401 | * check output for errors |
||
402 | * |
||
403 | * @param string[] $lines |
||
404 | * @param string $path |
||
405 | * |
||
406 | * @throws NotFoundException |
||
407 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
408 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
409 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
410 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
411 | * @throws \Icewind\SMB\Exception\Exception |
||
412 | * @return bool |
||
413 | */ |
||
414 | 1016 | protected function parseOutput($lines, $path = '') { |
|
422 | |||
423 | /** |
||
424 | * @param string $string |
||
425 | * @return string |
||
426 | */ |
||
427 | protected function escape($string) { |
||
430 | |||
431 | /** |
||
432 | * @param string $path |
||
433 | * @return string |
||
434 | */ |
||
435 | 1020 | protected function escapePath($path) { |
|
445 | |||
446 | /** |
||
447 | * @param string $path |
||
448 | * @return string |
||
449 | */ |
||
450 | 532 | protected function escapeLocalPath($path) { |
|
454 | |||
455 | 1024 | public function __destruct() { |
|
458 | } |
||
459 |
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: