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 | /** |
||
| 49 | * @param IServer $server |
||
| 50 | * @param string $name |
||
| 51 | * @param System $system |
||
| 52 | */ |
||
| 53 | 768 | public function __construct(IServer $server, $name, System $system = null) { |
|
| 60 | |||
| 61 | 765 | private function getAuthFileArgument() { |
|
| 68 | |||
| 69 | 765 | protected function getConnection() { |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @throws \Icewind\SMB\Exception\ConnectionException |
||
| 90 | * @throws \Icewind\SMB\Exception\AuthenticationException |
||
| 91 | * @throws \Icewind\SMB\Exception\InvalidHostException |
||
| 92 | */ |
||
| 93 | 762 | protected function connect() { |
|
| 99 | |||
| 100 | 3 | protected function reconnect() { |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Get the name of the share |
||
| 109 | * |
||
| 110 | * @return string |
||
| 111 | */ |
||
| 112 | 6 | public function getName() { |
|
| 113 | 6 | return $this->name; |
|
| 114 | } |
||
| 115 | |||
| 116 | 762 | protected function simpleCommand($command, $path) { |
|
| 122 | |||
| 123 | /** |
||
| 124 | * List the content of a remote folder |
||
| 125 | * |
||
| 126 | * @param $path |
||
| 127 | * @return \Icewind\SMB\IFileInfo[] |
||
| 128 | * |
||
| 129 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 130 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 131 | */ |
||
| 132 | 750 | public function dir($path) { |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @param string $path |
||
| 146 | * @return \Icewind\SMB\IFileInfo |
||
| 147 | */ |
||
| 148 | 99 | public function stat($path) { |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Create a folder on the share |
||
| 166 | * |
||
| 167 | * @param string $path |
||
| 168 | * @return bool |
||
| 169 | * |
||
| 170 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 171 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 172 | */ |
||
| 173 | 753 | public function mkdir($path) { |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Remove a folder on the share |
||
| 179 | * |
||
| 180 | * @param string $path |
||
| 181 | * @return bool |
||
| 182 | * |
||
| 183 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 184 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 185 | */ |
||
| 186 | 753 | public function rmdir($path) { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Delete a file on the share |
||
| 192 | * |
||
| 193 | * @param string $path |
||
| 194 | * @param bool $secondTry |
||
| 195 | * @return bool |
||
| 196 | * @throws InvalidTypeException |
||
| 197 | * @throws NotFoundException |
||
| 198 | * @throws \Exception |
||
| 199 | */ |
||
| 200 | 393 | public function del($path, $secondTry = false) { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Rename a remote file |
||
| 226 | * |
||
| 227 | * @param string $from |
||
| 228 | * @param string $to |
||
| 229 | * @return bool |
||
| 230 | * |
||
| 231 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 232 | * @throws \Icewind\SMB\Exception\AlreadyExistsException |
||
| 233 | */ |
||
| 234 | 90 | View Code Duplication | public function rename($from, $to) { |
| 240 | |||
| 241 | /** |
||
| 242 | * Upload a local file |
||
| 243 | * |
||
| 244 | * @param string $source local file |
||
| 245 | * @param string $target remove file |
||
| 246 | * @return bool |
||
| 247 | * |
||
| 248 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 249 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 250 | */ |
||
| 251 | 345 | View Code Duplication | public function put($source, $target) { |
| 257 | |||
| 258 | /** |
||
| 259 | * Download a remote file |
||
| 260 | * |
||
| 261 | * @param string $source remove file |
||
| 262 | * @param string $target local file |
||
| 263 | * @return bool |
||
| 264 | * |
||
| 265 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 266 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 267 | */ |
||
| 268 | 132 | View Code Duplication | public function get($source, $target) { |
| 274 | |||
| 275 | /** |
||
| 276 | * Open a readable stream to a remote file |
||
| 277 | * |
||
| 278 | * @param string $source |
||
| 279 | * @return resource a read only stream with the contents of the remote file |
||
| 280 | * |
||
| 281 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 282 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 283 | */ |
||
| 284 | 75 | public function read($source) { |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Open a writable stream to a remote file |
||
| 299 | * |
||
| 300 | * @param string $target |
||
| 301 | * @return resource a write only stream to upload a remote file |
||
| 302 | * |
||
| 303 | * @throws \Icewind\SMB\Exception\NotFoundException |
||
| 304 | * @throws \Icewind\SMB\Exception\InvalidTypeException |
||
| 305 | */ |
||
| 306 | 75 | public function write($target) { |
|
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $path |
||
| 325 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL |
||
| 326 | * @return mixed |
||
| 327 | */ |
||
| 328 | 24 | public function setMode($path, $mode) { |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @param string $path |
||
| 360 | * @return INotifyHandler |
||
| 361 | * @throws ConnectionException |
||
| 362 | * @throws DependencyException |
||
| 363 | */ |
||
| 364 | 15 | public function notify($path) { |
|
| 373 | |||
| 374 | /** |
||
| 375 | * @param string $command |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | 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: