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 | 771 | public function __construct(IServer $server, $name, ISystem $system) { |
|
73 | |||
74 | 768 | private function getAuthFileArgument() { |
|
81 | |||
82 | 768 | protected function getConnection() { |
|
103 | |||
104 | /** |
||
105 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
106 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
107 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
108 | */ |
||
109 | 765 | protected function connect() { |
|
115 | |||
116 | 3 | protected function reconnect() { |
|
122 | |||
123 | /** |
||
124 | * Get the name of the share |
||
125 | * |
||
126 | * @return string |
||
127 | */ |
||
128 | 6 | public function getName() { |
|
131 | |||
132 | 765 | protected function simpleCommand($command, $path) { |
|
138 | |||
139 | /** |
||
140 | * List the content of a remote folder |
||
141 | * |
||
142 | * @param $path |
||
143 | * @return \Icewind\SMB\IFileInfo[] |
||
144 | * |
||
145 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
146 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
147 | */ |
||
148 | 753 | public function dir($path) { |
|
161 | |||
162 | /** |
||
163 | * @param string $path |
||
164 | * @return \Icewind\SMB\IFileInfo |
||
165 | */ |
||
166 | 99 | public function stat($path) { |
|
196 | |||
197 | /** |
||
198 | * Create 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\AlreadyExistsException |
||
205 | */ |
||
206 | 756 | public function mkdir($path) { |
|
209 | |||
210 | /** |
||
211 | * Remove a folder on the share |
||
212 | * |
||
213 | * @param string $path |
||
214 | * @return bool |
||
215 | * |
||
216 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
217 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
218 | */ |
||
219 | 756 | public function rmdir($path) { |
|
222 | |||
223 | /** |
||
224 | * Delete a file on the share |
||
225 | * |
||
226 | * @param string $path |
||
227 | * @param bool $secondTry |
||
228 | * @return bool |
||
229 | * @throws InvalidTypeException |
||
230 | * @throws NotFoundException |
||
231 | * @throws \Exception |
||
232 | */ |
||
233 | 393 | public function del($path, $secondTry = false) { |
|
256 | |||
257 | /** |
||
258 | * Rename a remote file |
||
259 | * |
||
260 | * @param string $from |
||
261 | * @param string $to |
||
262 | * @return bool |
||
263 | * |
||
264 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
265 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
266 | */ |
||
267 | 90 | public function rename($from, $to) { |
|
273 | |||
274 | /** |
||
275 | * Upload a local file |
||
276 | * |
||
277 | * @param string $source local file |
||
278 | * @param string $target remove file |
||
279 | * @return bool |
||
280 | * |
||
281 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
282 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
283 | */ |
||
284 | 345 | public function put($source, $target) { |
|
290 | |||
291 | /** |
||
292 | * Download a remote file |
||
293 | * |
||
294 | * @param string $source remove file |
||
295 | * @param string $target local file |
||
296 | * @return bool |
||
297 | * |
||
298 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
299 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
300 | */ |
||
301 | 132 | public function get($source, $target) { |
|
307 | |||
308 | /** |
||
309 | * Open a readable stream to a remote file |
||
310 | * |
||
311 | * @param string $source |
||
312 | * @return resource a read only stream with the contents of the remote file |
||
313 | * |
||
314 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
315 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
316 | */ |
||
317 | 75 | public function read($source) { |
|
329 | |||
330 | /** |
||
331 | * Open a writable stream to a remote file |
||
332 | * |
||
333 | * @param string $target |
||
334 | * @return resource a write only stream to upload a remote file |
||
335 | * |
||
336 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
337 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
338 | */ |
||
339 | 75 | public function write($target) { |
|
355 | |||
356 | /** |
||
357 | * Append to stream |
||
358 | * Note: smbclient does not support this (Use php-libsmbclient) |
||
359 | * |
||
360 | * @param string $target |
||
361 | * |
||
362 | * @throws \Icewind\SMB\Exception\DependencyException |
||
363 | */ |
||
364 | 3 | public function append($target) { |
|
367 | |||
368 | /** |
||
369 | * @param string $path |
||
370 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
371 | * @return mixed |
||
372 | */ |
||
373 | 24 | public function setMode($path, $mode) { |
|
396 | |||
397 | /** |
||
398 | * @param string $path |
||
399 | * @return INotifyHandler |
||
400 | * @throws ConnectionException |
||
401 | * @throws DependencyException |
||
402 | */ |
||
403 | 15 | public function notify($path) { |
|
412 | |||
413 | /** |
||
414 | * @param string $command |
||
415 | * @return array |
||
416 | */ |
||
417 | 765 | protected function execute($command) { |
|
422 | |||
423 | /** |
||
424 | * check output for errors |
||
425 | * |
||
426 | * @param string[] $lines |
||
427 | * @param string $path |
||
428 | * |
||
429 | * @return bool |
||
430 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
431 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
432 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
433 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
434 | * @throws \Icewind\SMB\Exception\Exception |
||
435 | * @throws NotFoundException |
||
436 | */ |
||
437 | 765 | protected function parseOutput($lines, $path = '') { |
|
445 | |||
446 | /** |
||
447 | * @param string $string |
||
448 | * @return string |
||
449 | */ |
||
450 | protected function escape($string) { |
||
453 | |||
454 | /** |
||
455 | * @param string $path |
||
456 | * @return string |
||
457 | */ |
||
458 | 768 | protected function escapePath($path) { |
|
468 | |||
469 | /** |
||
470 | * @param string $path |
||
471 | * @return string |
||
472 | */ |
||
473 | 399 | protected function escapeLocalPath($path) { |
|
477 | |||
478 | protected function getAcls($path) { |
||
555 | |||
556 | 771 | public function __destruct() { |
|
559 | } |
||
560 |
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: