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 System |
||
| 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 System $system |
||
| 59 | */ |
||
| 60 | 768 | public function __construct(IServer $server, $name, System $system = null) { |
|
| 61 | 768 | parent::__construct(); |
|
| 62 | 768 | $this->server = $server; |
|
| 63 | 768 | $this->name = $name; |
|
| 64 | 768 | $this->system = (!is_null($system)) ? $system : new System(); |
|
| 65 | 768 | $this->parser = new Parser(new TimeZoneProvider($this->server->getHost(), $this->system)); |
|
| 66 | 768 | } |
|
| 67 | |||
| 68 | 765 | private function getAuthFileArgument() { |
|
| 75 | |||
| 76 | 765 | protected function getConnection() { |
|
| 94 | |||
| 95 | /** |
||
| 96 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
| 97 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
| 98 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
| 99 | */ |
||
| 100 | 762 | protected function connect() { |
|
| 106 | |||
| 107 | 3 | protected function reconnect() { |
|
| 113 | |||
| 114 | /** |
||
| 115 | * Get the name of the share |
||
| 116 | * |
||
| 117 | * @return string |
||
| 118 | */ |
||
| 119 | 6 | public function getName() { |
|
| 122 | |||
| 123 | 762 | protected function simpleCommand($command, $path) { |
|
| 129 | |||
| 130 | /** |
||
| 131 | * List the content of a remote folder |
||
| 132 | * |
||
| 133 | * @param $path |
||
| 134 | * @return \Icewind\SMB\IFileInfo[] |
||
| 135 | * |
||
| 136 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 137 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 138 | */ |
||
| 139 | 750 | public function dir($path) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * @param string $path |
||
| 153 | * @return \Icewind\SMB\IFileInfo |
||
| 154 | */ |
||
| 155 | 99 | public function stat($path) { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Create a folder on the share |
||
| 173 | * |
||
| 174 | * @param string $path |
||
| 175 | * @return bool |
||
| 176 | * |
||
| 177 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 178 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 179 | */ |
||
| 180 | 753 | public function mkdir($path) { |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Remove a folder on the share |
||
| 186 | * |
||
| 187 | * @param string $path |
||
| 188 | * @return bool |
||
| 189 | * |
||
| 190 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 191 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 192 | */ |
||
| 193 | 753 | public function rmdir($path) { |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Delete a file on the share |
||
| 199 | * |
||
| 200 | * @param string $path |
||
| 201 | * @param bool $secondTry |
||
| 202 | * @return bool |
||
| 203 | * @throws InvalidTypeException |
||
| 204 | * @throws NotFoundException |
||
| 205 | * @throws \Exception |
||
| 206 | */ |
||
| 207 | 393 | public function del($path, $secondTry = false) { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Rename a remote file |
||
| 233 | * |
||
| 234 | * @param string $from |
||
| 235 | * @param string $to |
||
| 236 | * @return bool |
||
| 237 | * |
||
| 238 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 239 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 240 | */ |
||
| 241 | 90 | View Code Duplication | public function rename($from, $to) { |
| 247 | |||
| 248 | /** |
||
| 249 | * Upload a local file |
||
| 250 | * |
||
| 251 | * @param string $source local file |
||
| 252 | * @param string $target remove file |
||
| 253 | * @return bool |
||
| 254 | * |
||
| 255 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 256 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 257 | */ |
||
| 258 | 345 | View Code Duplication | public function put($source, $target) { |
| 264 | |||
| 265 | /** |
||
| 266 | * Download a remote file |
||
| 267 | * |
||
| 268 | * @param string $source remove file |
||
| 269 | * @param string $target local file |
||
| 270 | * @return bool |
||
| 271 | * |
||
| 272 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 273 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 274 | */ |
||
| 275 | 132 | View Code Duplication | public function get($source, $target) { |
| 281 | |||
| 282 | /** |
||
| 283 | * Open a readable stream to a remote file |
||
| 284 | * |
||
| 285 | * @param string $source |
||
| 286 | * @return resource a read only stream with the contents of the remote file |
||
| 287 | * |
||
| 288 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 289 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 290 | */ |
||
| 291 | 75 | public function read($source) { |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Open a writable stream to a remote file |
||
| 306 | * |
||
| 307 | * @param string $target |
||
| 308 | * @return resource a write only stream to upload a remote file |
||
| 309 | * |
||
| 310 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 311 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 312 | */ |
||
| 313 | 75 | public function write($target) { |
|
| 329 | |||
| 330 | /** |
||
| 331 | * @param string $path |
||
| 332 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
| 333 | * @return mixed |
||
| 334 | */ |
||
| 335 | 24 | public function setMode($path, $mode) { |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $path |
||
| 361 | * @return INotifyHandler |
||
| 362 | * @throws ConnectionException |
||
| 363 | * @throws DependencyException |
||
| 364 | */ |
||
| 365 | 15 | public function notify($path) { |
|
| 374 | |||
| 375 | /** |
||
| 376 | * @param string $command |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | 762 | protected function execute($command) { |
|
| 384 | |||
| 385 | /** |
||
| 386 | * check output for errors |
||
| 387 | * |
||
| 388 | * @param string[] $lines |
||
| 389 | * @param string $path |
||
| 390 | * |
||
| 391 | * @throws NotFoundException |
||
| 392 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 393 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
| 394 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
| 395 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 396 | * @throws \Icewind\SMB\Exception\Exception |
||
| 397 | * @return bool |
||
| 398 | */ |
||
| 399 | 762 | protected function parseOutput($lines, $path = '') { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * @param string $string |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | protected function escape($string) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $path |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | 765 | protected function escapePath($path) { |
|
| 430 | |||
| 431 | /** |
||
| 432 | * @param string $path |
||
| 433 | * @return string |
||
| 434 | */ |
||
| 435 | 399 | protected function escapeLocalPath($path) { |
|
| 439 | |||
| 440 | 768 | public function __destruct() { |
|
| 443 | } |
||
| 444 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: