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 |
||
17 | class Share extends AbstractShare { |
||
18 | /** |
||
19 | * @var Server $server |
||
20 | */ |
||
21 | private $server; |
||
22 | |||
23 | /** |
||
24 | * @var string $name |
||
25 | */ |
||
26 | private $name; |
||
27 | |||
28 | /** |
||
29 | * @var Connection $connection |
||
30 | */ |
||
31 | public $connection; |
||
32 | |||
33 | /** |
||
34 | * @var \Icewind\SMB\Parser |
||
35 | */ |
||
36 | protected $parser; |
||
37 | |||
38 | /** |
||
39 | * @var \Icewind\SMB\System |
||
40 | */ |
||
41 | private $system; |
||
42 | |||
43 | /** |
||
44 | * @param Server $server |
||
45 | * @param string $name |
||
46 | * @param System $system |
||
47 | */ |
||
48 | 512 | public function __construct($server, $name, System $system = null) { |
|
55 | |||
56 | 510 | protected function getConnection() { |
|
78 | |||
79 | /** |
||
80 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
81 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
82 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
83 | */ |
||
84 | 508 | protected function connect() { |
|
90 | |||
91 | 2 | protected function reconnect() { |
|
98 | |||
99 | /** |
||
100 | * Get the name of the share |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | 4 | public function getName() { |
|
107 | |||
108 | 508 | protected function simpleCommand($command, $path) { |
|
114 | |||
115 | /** |
||
116 | * List the content of a remote folder |
||
117 | * |
||
118 | * @param $path |
||
119 | * @return \Icewind\SMB\IFileInfo[] |
||
120 | * |
||
121 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
122 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
123 | */ |
||
124 | 500 | public function dir($path) { |
|
134 | |||
135 | /** |
||
136 | * @param string $path |
||
137 | * @return \Icewind\SMB\IFileInfo |
||
138 | */ |
||
139 | 66 | public function stat($path) { |
|
154 | |||
155 | /** |
||
156 | * Create a folder on the share |
||
157 | * |
||
158 | * @param string $path |
||
159 | * @return bool |
||
160 | * |
||
161 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
162 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
163 | */ |
||
164 | 502 | public function mkdir($path) { |
|
167 | |||
168 | /** |
||
169 | * Remove a folder on the share |
||
170 | * |
||
171 | * @param string $path |
||
172 | * @return bool |
||
173 | * |
||
174 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
175 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
176 | */ |
||
177 | 502 | public function rmdir($path) { |
|
180 | |||
181 | /** |
||
182 | * Delete a file on the share |
||
183 | * |
||
184 | * @param string $path |
||
185 | * @param bool $secondTry |
||
186 | * @return bool |
||
187 | * @throws InvalidTypeException |
||
188 | * @throws NotFoundException |
||
189 | * @throws \Exception |
||
190 | */ |
||
191 | 262 | public function del($path, $secondTry = false) { |
|
214 | |||
215 | /** |
||
216 | * Rename a remote file |
||
217 | * |
||
218 | * @param string $from |
||
219 | * @param string $to |
||
220 | * @return bool |
||
221 | * |
||
222 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
223 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
224 | */ |
||
225 | 58 | public function rename($from, $to) { |
|
231 | |||
232 | /** |
||
233 | * Upload a local file |
||
234 | * |
||
235 | * @param string $source local file |
||
236 | * @param string $target remove file |
||
237 | * @return bool |
||
238 | * |
||
239 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
240 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
241 | */ |
||
242 | 230 | public function put($source, $target) { |
|
248 | |||
249 | /** |
||
250 | * Download a remote file |
||
251 | * |
||
252 | * @param string $source remove file |
||
253 | * @param string $target local file |
||
254 | * @return bool |
||
255 | * |
||
256 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
257 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
258 | */ |
||
259 | 88 | public function get($source, $target) { |
|
265 | |||
266 | /** |
||
267 | * Open a readable stream to a remote file |
||
268 | * |
||
269 | * @param string $source |
||
270 | * @return resource a read only stream with the contents of the remote file |
||
271 | * |
||
272 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
273 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
274 | */ |
||
275 | 50 | public function read($source) { |
|
287 | |||
288 | /** |
||
289 | * Open a writable stream to a remote file |
||
290 | * |
||
291 | * @param string $target |
||
292 | * @return resource a write only stream to upload a remote file |
||
293 | * |
||
294 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
295 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
296 | */ |
||
297 | 50 | public function write($target) { |
|
313 | |||
314 | /** |
||
315 | * @param string $path |
||
316 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
317 | * @return mixed |
||
318 | */ |
||
319 | 16 | public function setMode($path, $mode) { |
|
348 | |||
349 | /** |
||
350 | * @param string $path |
||
351 | * @return INotifyHandler |
||
352 | * @throws ConnectionException |
||
353 | * @throws DependencyException |
||
354 | */ |
||
355 | 10 | public function notify($path) { |
|
364 | |||
365 | /** |
||
366 | * @param string $command |
||
367 | * @return array |
||
368 | */ |
||
369 | 508 | protected function execute($command) { |
|
375 | |||
376 | /** |
||
377 | * check output for errors |
||
378 | * |
||
379 | * @param string[] $lines |
||
380 | * @param string $path |
||
381 | * |
||
382 | * @throws NotFoundException |
||
383 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
384 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
385 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
386 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
387 | * @throws \Icewind\SMB\Exception\Exception |
||
388 | * @return bool |
||
389 | */ |
||
390 | 508 | protected function parseOutput($lines, $path = '') { |
|
398 | |||
399 | /** |
||
400 | * @param string $string |
||
401 | * @return string |
||
402 | */ |
||
403 | protected function escape($string) { |
||
406 | |||
407 | /** |
||
408 | * @param string $path |
||
409 | * @return string |
||
410 | */ |
||
411 | 510 | protected function escapePath($path) { |
|
420 | |||
421 | /** |
||
422 | * @param string $path |
||
423 | * @return string |
||
424 | */ |
||
425 | 266 | protected function escapeLocalPath($path) { |
|
429 | |||
430 | 512 | public function __destruct() { |
|
433 | } |
||
434 |