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