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 |
||
| 25 | class Share extends AbstractShare { |
||
| 26 | /** |
||
| 27 | * @var IServer $server |
||
| 28 | */ |
||
| 29 | private $server; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string $name |
||
| 33 | */ |
||
| 34 | private $name; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var Connection $connection |
||
| 38 | */ |
||
| 39 | public $connection; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var Parser |
||
| 43 | */ |
||
| 44 | protected $parser; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var ISystem |
||
| 48 | */ |
||
| 49 | private $system; |
||
| 50 | |||
| 51 | const MODE_MAP = [ |
||
| 52 | FileInfo::MODE_READONLY => 'r', |
||
| 53 | FileInfo::MODE_HIDDEN => 'h', |
||
| 54 | FileInfo::MODE_ARCHIVE => 'a', |
||
| 55 | FileInfo::MODE_SYSTEM => 's' |
||
| 56 | ]; |
||
| 57 | |||
| 58 | const EXEC_CMD = 'exec'; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @param IServer $server |
||
| 62 | * @param string $name |
||
| 63 | * @param ISystem $system |
||
| 64 | */ |
||
| 65 | 1028 | public function __construct(IServer $server, $name, ISystem $system) { |
|
| 66 | 1028 | parent::__construct(); |
|
| 67 | 1028 | $this->server = $server; |
|
| 68 | 1028 | $this->name = $name; |
|
| 69 | 1028 | $this->system = $system; |
|
| 70 | 1028 | $this->parser = new Parser($server->getTimeZone()); |
|
| 71 | 1028 | } |
|
| 72 | |||
| 73 | 1024 | private function getAuthFileArgument() { |
|
| 74 | 1024 | if ($this->server->getAuth()->getUsername()) { |
|
|
|
|||
| 75 | 1024 | return '--authentication-file=' . $this->system->getFD(3); |
|
| 76 | } else { |
||
| 77 | return ''; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | 1024 | protected function getConnection() { |
|
| 82 | 1024 | $command = sprintf( |
|
| 83 | 1024 | '%s %s%s -t %s %s %s %s', |
|
| 84 | 1024 | self::EXEC_CMD, |
|
| 85 | 1024 | $this->system->getStdBufPath() ? $this->system->getStdBufPath() . ' -o0 ' : '', |
|
| 86 | 1024 | $this->system->getSmbclientPath(), |
|
| 87 | 1024 | $this->server->getOptions()->getTimeout(), |
|
| 88 | 1024 | $this->getAuthFileArgument(), |
|
| 89 | 1024 | $this->server->getAuth()->getExtraCommandLineArguments(), |
|
| 90 | 1024 | escapeshellarg('//' . $this->server->getHost() . '/' . $this->name) |
|
| 91 | ); |
||
| 92 | 1024 | $connection = new Connection($command, $this->parser); |
|
| 93 | 1024 | $connection->writeAuthentication($this->server->getAuth()->getUsername(), $this->server->getAuth()->getPassword()); |
|
| 94 | 1024 | $connection->connect(); |
|
| 95 | 1024 | if (!$connection->isValid()) { |
|
| 96 | throw new ConnectionException($connection->readLine()); |
||
| 97 | } |
||
| 98 | // some versions of smbclient add a help message in first of the first prompt |
||
| 99 | 1024 | $connection->clearTillPrompt(); |
|
| 100 | 1024 | return $connection; |
|
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
| 105 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
| 106 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
| 107 | */ |
||
| 108 | 1020 | protected function connect() { |
|
| 109 | 1020 | if ($this->connection and $this->connection->isValid()) { |
|
| 110 | 1020 | return; |
|
| 111 | } |
||
| 112 | 1020 | $this->connection = $this->getConnection(); |
|
| 113 | 1020 | } |
|
| 114 | |||
| 115 | 4 | protected function reconnect() { |
|
| 116 | 4 | $this->connection->reconnect(); |
|
| 117 | 4 | if (!$this->connection->isValid()) { |
|
| 118 | throw new ConnectionException(); |
||
| 119 | } |
||
| 120 | 4 | } |
|
| 121 | |||
| 122 | /** |
||
| 123 | * Get the name of the share |
||
| 124 | * |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | 8 | public function getName() { |
|
| 130 | |||
| 131 | 1020 | protected function simpleCommand($command, $path) { |
|
| 132 | 1020 | $escapedPath = $this->escapePath($path); |
|
| 133 | 1020 | $cmd = $command . ' ' . $escapedPath; |
|
| 134 | 1020 | $output = $this->execute($cmd); |
|
| 137 | |||
| 138 | /** |
||
| 139 | * List the content of a remote folder |
||
| 140 | * |
||
| 141 | * @param $path |
||
| 142 | * @return \Icewind\SMB\IFileInfo[] |
||
| 143 | * |
||
| 144 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 145 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 146 | */ |
||
| 147 | 1004 | public function dir($path) { |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @param string $path |
||
| 161 | * @return \Icewind\SMB\IFileInfo |
||
| 162 | */ |
||
| 163 | 132 | public function stat($path) { |
|
| 164 | // some windows server setups don't seem to like the allinfo command |
||
| 165 | // use the dir command instead to get the file info where possible |
||
| 166 | 132 | if ($path !== "" && $path !== "/") { |
|
| 167 | 128 | $parent = dirname($path); |
|
| 168 | 128 | $dir = $this->dir($parent); |
|
| 169 | 128 | $file = array_values(array_filter($dir, function (IFileInfo $info) use ($path) { |
|
| 170 | 124 | return $info->getPath() === $path; |
|
| 171 | 128 | })); |
|
| 172 | 128 | if ($file) { |
|
| 173 | 88 | return $file[0]; |
|
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | 44 | $escapedPath = $this->escapePath($path); |
|
| 178 | 8 | $output = $this->execute('allinfo ' . $escapedPath); |
|
| 179 | // Windows and non Windows Fileserver may respond different |
||
| 180 | // to the allinfo command for directories. If the result is a single |
||
| 181 | // line = error line, redo it with a different allinfo parameter |
||
| 182 | 8 | if ($escapedPath == '""' && count($output) < 2) { |
|
| 183 | $output = $this->execute('allinfo ' . '"."'); |
||
| 184 | } |
||
| 185 | 8 | if (count($output) < 3) { |
|
| 186 | 4 | $this->parseOutput($output, $path); |
|
| 187 | } |
||
| 188 | 4 | $stat = $this->parser->parseStat($output); |
|
| 189 | 4 | return new FileInfo($path, basename($path), $stat['size'], $stat['mtime'], $stat['mode']); |
|
| 190 | } |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Create a folder on the share |
||
| 194 | * |
||
| 195 | * @param string $path |
||
| 196 | * @return bool |
||
| 197 | * |
||
| 198 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 199 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 200 | */ |
||
| 201 | 1008 | public function mkdir($path) { |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Remove a folder on the share |
||
| 207 | * |
||
| 208 | * @param string $path |
||
| 209 | * @return bool |
||
| 210 | * |
||
| 211 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 212 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 213 | */ |
||
| 214 | 1008 | public function rmdir($path) { |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Delete a file on the share |
||
| 220 | * |
||
| 221 | * @param string $path |
||
| 222 | * @param bool $secondTry |
||
| 223 | * @return bool |
||
| 224 | * @throws InvalidTypeException |
||
| 225 | * @throws NotFoundException |
||
| 226 | * @throws \Exception |
||
| 227 | */ |
||
| 228 | 524 | public function del($path, $secondTry = false) { |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Rename a remote file |
||
| 254 | * |
||
| 255 | * @param string $from |
||
| 256 | * @param string $to |
||
| 257 | * @return bool |
||
| 258 | * |
||
| 259 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 260 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 261 | */ |
||
| 262 | 120 | View Code Duplication | public function rename($from, $to) { |
| 268 | |||
| 269 | /** |
||
| 270 | * Upload a local file |
||
| 271 | * |
||
| 272 | * @param string $source local file |
||
| 273 | * @param string $target remove file |
||
| 274 | * @return bool |
||
| 275 | * |
||
| 276 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 277 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 278 | */ |
||
| 279 | 460 | View Code Duplication | public function put($source, $target) { |
| 285 | |||
| 286 | /** |
||
| 287 | * Download a remote file |
||
| 288 | * |
||
| 289 | * @param string $source remove file |
||
| 290 | * @param string $target local file |
||
| 291 | * @return bool |
||
| 292 | * |
||
| 293 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 294 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 295 | */ |
||
| 296 | 176 | View Code Duplication | public function get($source, $target) { |
| 302 | |||
| 303 | /** |
||
| 304 | * Open a readable stream to a remote file |
||
| 305 | * |
||
| 306 | * @param string $source |
||
| 307 | * @return resource a read only stream with the contents of the remote file |
||
| 308 | * |
||
| 309 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 310 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 311 | */ |
||
| 312 | 100 | public function read($source) { |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Open a writable stream to a remote file |
||
| 327 | * |
||
| 328 | * @param string $target |
||
| 329 | * @param bool $truncate |
||
| 330 | * @return resource a write only stream to upload a remote file |
||
| 331 | * |
||
| 332 | * @throws \Icewind\SMB\Exception\DependencyException |
||
| 333 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 334 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 335 | */ |
||
| 336 | 100 | public function write($target, $truncate = true) { |
|
| 356 | |||
| 357 | /** |
||
| 358 | * Append to stream |
||
| 359 | * Note: smbclient does not support this (Use php-libsmbclient) |
||
| 360 | * |
||
| 361 | * @param string $target |
||
| 362 | * |
||
| 363 | * @throws \Icewind\SMB\Exception\DependencyException |
||
| 364 | */ |
||
| 365 | 4 | public function append($target) { |
|
| 368 | |||
| 369 | /** |
||
| 370 | * @param string $path |
||
| 371 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
| 372 | * @return mixed |
||
| 373 | */ |
||
| 374 | 32 | public function setMode($path, $mode) { |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @param string $path |
||
| 400 | * @return INotifyHandler |
||
| 401 | * @throws ConnectionException |
||
| 402 | * @throws DependencyException |
||
| 403 | */ |
||
| 404 | 20 | public function notify($path) { |
|
| 413 | |||
| 414 | /** |
||
| 415 | * @param string $command |
||
| 416 | * @return array |
||
| 417 | */ |
||
| 418 | 1020 | protected function execute($command) { |
|
| 423 | |||
| 424 | /** |
||
| 425 | * check output for errors |
||
| 426 | * |
||
| 427 | * @param string[] $lines |
||
| 428 | * @param string $path |
||
| 429 | * |
||
| 430 | * @throws NotFoundException |
||
| 431 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 432 | * @throws \Icewind\SMB\Exception\AccessDeniedException |
||
| 433 | * @throws \Icewind\SMB\Exception\NotEmptyException |
||
| 434 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 435 | * @throws \Icewind\SMB\Exception\Exception |
||
| 436 | * @return bool |
||
| 437 | */ |
||
| 438 | 1020 | protected function parseOutput($lines, $path = '') { |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @param string $string |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | protected function escape($string) { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param string $path |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | 1024 | protected function escapePath($path) { |
|
| 469 | |||
| 470 | /** |
||
| 471 | * @param string $path |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | 532 | protected function escapeLocalPath($path) { |
|
| 478 | |||
| 479 | 1028 | public function __destruct() { |
|
| 482 | } |
||
| 483 |
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: