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 |
||
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 | 1028 | public function __construct($server, $name, System $system = null) { |
|
55 | |||
56 | 1024 | protected function getConnection() { |
|
79 | |||
80 | /** |
||
81 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
82 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
83 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
84 | */ |
||
85 | 1020 | protected function connect() { |
|
91 | |||
92 | 4 | protected function reconnect() { |
|
98 | |||
99 | /** |
||
100 | * Get the name of the share |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | 8 | public function getName() { |
|
107 | |||
108 | 1020 | 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 | 1004 | public function dir($path) { |
|
135 | |||
136 | /** |
||
137 | * @param string $path |
||
138 | * @return \Icewind\SMB\IFileInfo |
||
139 | */ |
||
140 | 132 | public function stat($path) { |
|
168 | |||
169 | /** |
||
170 | * Create a folder on the share |
||
171 | * |
||
172 | * @param string $path |
||
173 | * @return bool |
||
174 | * |
||
175 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
176 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
177 | */ |
||
178 | 1008 | public function mkdir($path) { |
|
181 | |||
182 | /** |
||
183 | * Remove a folder on the share |
||
184 | * |
||
185 | * @param string $path |
||
186 | * @return bool |
||
187 | * |
||
188 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
189 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
190 | */ |
||
191 | 1008 | public function rmdir($path) { |
|
194 | |||
195 | /** |
||
196 | * Delete a file on the share |
||
197 | * |
||
198 | * @param string $path |
||
199 | * @param bool $secondTry |
||
200 | * @return bool |
||
201 | * @throws InvalidTypeException |
||
202 | * @throws NotFoundException |
||
203 | * @throws \Exception |
||
204 | */ |
||
205 | 524 | public function del($path, $secondTry = false) { |
|
228 | |||
229 | /** |
||
230 | * Rename a remote file |
||
231 | * |
||
232 | * @param string $from |
||
233 | * @param string $to |
||
234 | * @return bool |
||
235 | * |
||
236 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
237 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
238 | */ |
||
239 | 120 | View Code Duplication | public function rename($from, $to) { |
245 | |||
246 | /** |
||
247 | * Upload a local file |
||
248 | * |
||
249 | * @param string $source local file |
||
250 | * @param string $target remove file |
||
251 | * @return bool |
||
252 | * |
||
253 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
254 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
255 | */ |
||
256 | 460 | View Code Duplication | public function put($source, $target) { |
262 | |||
263 | /** |
||
264 | * Download a remote file |
||
265 | * |
||
266 | * @param string $source remove file |
||
267 | * @param string $target local file |
||
268 | * @return bool |
||
269 | * |
||
270 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
271 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
272 | */ |
||
273 | 176 | View Code Duplication | public function get($source, $target) { |
279 | |||
280 | /** |
||
281 | * Open a readable stream to a remote file |
||
282 | * |
||
283 | * @param string $source |
||
284 | * @return resource a read only stream with the contents of the remote file |
||
285 | * |
||
286 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
287 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
288 | */ |
||
289 | 100 | public function read($source) { |
|
301 | |||
302 | /** |
||
303 | * Open a writable stream to a remote file |
||
304 | * |
||
305 | * @param string $target |
||
306 | * @return resource a write only stream to upload a remote file |
||
307 | * |
||
308 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
309 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
310 | */ |
||
311 | 100 | public function write($target) { |
|
327 | |||
328 | /** |
||
329 | * @param string $path |
||
330 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
331 | * @return mixed |
||
332 | */ |
||
333 | 32 | public function setMode($path, $mode) { |
|
362 | |||
363 | /** |
||
364 | * @param string $path |
||
365 | * @return INotifyHandler |
||
366 | * @throws ConnectionException |
||
367 | * @throws DependencyException |
||
368 | */ |
||
369 | 20 | public function notify($path) { |
|
378 | |||
379 | /** |
||
380 | * @param string $command |
||
381 | * @return array |
||
382 | */ |
||
383 | 1020 | protected function execute($command) { |
|
388 | |||
389 | /** |
||
390 | * check output for errors |
||
391 | * |
||
392 | * @param string[] $lines |
||
393 | * @param string $path |
||
394 | * |
||
395 | * @throws NotFoundException |
||
396 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
397 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
398 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
399 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
400 | * @throws \Icewind\SMB\Exception\Exception |
||
401 | * @return bool |
||
402 | */ |
||
403 | 1020 | protected function parseOutput($lines, $path = '') { |
|
411 | |||
412 | /** |
||
413 | * @param string $string |
||
414 | * @return string |
||
415 | */ |
||
416 | protected function escape($string) { |
||
419 | |||
420 | /** |
||
421 | * @param string $path |
||
422 | * @return string |
||
423 | */ |
||
424 | 1024 | protected function escapePath($path) { |
|
434 | |||
435 | /** |
||
436 | * @param string $path |
||
437 | * @return string |
||
438 | */ |
||
439 | 532 | protected function escapeLocalPath($path) { |
|
443 | |||
444 | 1028 | public function __destruct() { |
|
447 | } |
||
448 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.