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 | ||
| 31 | class Share extends AbstractShare { | ||
| 32 | /** | ||
| 33 | * @var IServer $server | ||
| 34 | */ | ||
| 35 | private $server; | ||
| 36 | |||
| 37 | /** | ||
| 38 | * @var string $name | ||
| 39 | */ | ||
| 40 | private $name; | ||
| 41 | |||
| 42 | /** | ||
| 43 | * @var Connection|null $connection | ||
| 44 | */ | ||
| 45 | public $connection = null; | ||
| 46 | |||
| 47 | /** | ||
| 48 | * @var Parser | ||
| 49 | */ | ||
| 50 | protected $parser; | ||
| 51 | |||
| 52 | /** | ||
| 53 | * @var ISystem | ||
| 54 | */ | ||
| 55 | private $system; | ||
| 56 | |||
| 57 | const MODE_MAP = [ | ||
| 58 | FileInfo::MODE_READONLY => 'r', | ||
| 59 | FileInfo::MODE_HIDDEN => 'h', | ||
| 60 | FileInfo::MODE_ARCHIVE => 'a', | ||
| 61 | FileInfo::MODE_SYSTEM => 's' | ||
| 62 | ]; | ||
| 63 | |||
| 64 | const EXEC_CMD = 'exec'; | ||
| 65 | |||
| 66 | 488 | /** | |
| 67 | 488 | * @param IServer $server | |
| 68 | 488 | * @param string $name | |
| 69 | 488 | * @param ISystem $system | |
| 70 | 488 | */ | |
| 71 | 488 | 	public function __construct(IServer $server, string $name, ISystem $system) { | |
| 78 | |||
| 79 | 	private function getAuthFileArgument(): string { | ||
| 86 | 486 | ||
| 87 | 486 | 	protected function getConnection(): Connection { | |
| 117 | 2 | ||
| 118 | 2 | /** | |
| 119 | * @throws ConnectionException | ||
| 120 | * @throws AuthenticationException | ||
| 121 | 2 | * @throws InvalidHostException | |
| 122 | * @psalm-assert Connection $this->connection | ||
| 123 | */ | ||
| 124 | 	protected function connect(): Connection { | ||
| 131 | |||
| 132 | 484 | /** | |
| 133 | 484 | * @throws ConnectionException | |
| 134 | 484 | * @throws AuthenticationException | |
| 135 | 484 | * @throws InvalidHostException | |
| 136 | 484 | * @psalm-assert Connection $this->connection | |
| 137 | */ | ||
| 138 | 	protected function reconnect(): void { | ||
| 148 | 484 | ||
| 149 | 484 | /** | |
| 150 | 484 | * Get the name of the share | |
| 151 | * | ||
| 152 | 484 | * @return string | |
| 153 | 484 | */ | |
| 154 | 	public function getName(): string { | ||
| 157 | 484 | ||
| 158 | 	protected function simpleCommand(string $command, string $path): bool { | ||
| 164 | |||
| 165 | /** | ||
| 166 | 50 | * List the content of a remote folder | |
| 167 | * | ||
| 168 | * @param string $path | ||
| 169 | 50 | * @return IFileInfo[] | |
| 170 | 48 | * | |
| 171 | 48 | * @throws NotFoundException | |
| 172 | 48 | * @throws InvalidTypeException | |
| 173 | 46 | */ | |
| 174 | 48 | 	public function dir(string $path): array { | |
| 187 | |||
| 188 | 4 | /** | |
| 189 | 2 | * @param string $path | |
| 190 | * @return IFileInfo | ||
| 191 | 2 | */ | |
| 192 | 2 | 	public function stat(string $path): IFileInfo { | |
| 222 | |||
| 223 | /** | ||
| 224 | * Create a folder on the share | ||
| 225 | * | ||
| 226 | * @param string $path | ||
| 227 | * @return bool | ||
| 228 | * | ||
| 229 | * @throws NotFoundException | ||
| 230 | * @throws AlreadyExistsException | ||
| 231 | */ | ||
| 232 | 	public function mkdir(string $path): bool { | ||
| 235 | |||
| 236 | /** | ||
| 237 | 236 | * Remove a folder on the share | |
| 238 | 22 | * | |
| 239 | * @param string $path | ||
| 240 | * @return bool | ||
| 241 | 2 | * | |
| 242 | 2 | * @throws NotFoundException | |
| 243 | 2 | * @throws InvalidTypeException | |
| 244 | */ | ||
| 245 | 	public function rmdir(string $path): bool { | ||
| 248 | 20 | ||
| 249 | 2 | /** | |
| 250 | * Delete a file on the share | ||
| 251 | * | ||
| 252 | 2 | * @param string $path | |
| 253 | 2 | * @param bool $secondTry | |
| 254 | * @return bool | ||
| 255 | * @throws InvalidTypeException | ||
| 256 | * @throws NotFoundException | ||
| 257 | * @throws \Exception | ||
| 258 | */ | ||
| 259 | 	public function del(string $path, bool $secondTry = false): bool { | ||
| 282 | |||
| 283 | /** | ||
| 284 | 206 | * Rename a remote file | |
| 285 | 206 | * | |
| 286 | 206 | * @param string $from | |
| 287 | 188 | * @param string $to | |
| 288 | 188 | * @return bool | |
| 289 | * | ||
| 290 | * @throws NotFoundException | ||
| 291 | * @throws AlreadyExistsException | ||
| 292 | */ | ||
| 293 | View Code Duplication | 	public function rename(string $from, string $to): bool { | |
| 299 | |||
| 300 | /** | ||
| 301 | 88 | * Upload a local file | |
| 302 | 88 | * | |
| 303 | 70 | * @param string $source local file | |
| 304 | 70 | * @param string $target remove file | |
| 305 | 70 | * @return bool | |
| 306 | * | ||
| 307 | * @throws NotFoundException | ||
| 308 | * @throws InvalidTypeException | ||
| 309 | */ | ||
| 310 | View Code Duplication | 	public function put(string $source, string $target): bool { | |
| 316 | |||
| 317 | 50 | /** | |
| 318 | 50 | * Download a remote file | |
| 319 | * | ||
| 320 | * @param string $source remove file | ||
| 321 | 32 | * @param string $target local file | |
| 322 | * @return bool | ||
| 323 | 32 | * | |
| 324 | 32 | * @throws NotFoundException | |
| 325 | 32 | * @throws InvalidTypeException | |
| 326 | 32 | */ | |
| 327 | 32 | View Code Duplication | 	public function get(string $source, string $target): bool { | 
| 333 | |||
| 334 | /** | ||
| 335 | * Open a readable stream to a remote file | ||
| 336 | * | ||
| 337 | * @param string $source | ||
| 338 | * @return resource a read only stream with the contents of the remote file | ||
| 339 | 50 | * | |
| 340 | 50 | * @throws NotFoundException | |
| 341 | * @throws InvalidTypeException | ||
| 342 | */ | ||
| 343 | 32 | 	public function read(string $source) { | |
| 355 | |||
| 356 | /** | ||
| 357 | * Open a writable stream to a remote file | ||
| 358 | * | ||
| 359 | * @param string $target | ||
| 360 | * @return resource a write only stream to upload a remote file | ||
| 361 | * | ||
| 362 | * @throws NotFoundException | ||
| 363 | * @throws InvalidTypeException | ||
| 364 | 2 | */ | |
| 365 | 2 | 	public function write(string $target) { | |
| 386 | |||
| 387 | /** | ||
| 388 | * Append to stream | ||
| 389 | * Note: smbclient does not support this (Use php-libsmbclient) | ||
| 390 | * | ||
| 391 | * @param string $target | ||
| 392 | * | ||
| 393 | * @throws DependencyException | ||
| 394 | */ | ||
| 395 | 	public function append(string $target) { | ||
| 398 | |||
| 399 | /** | ||
| 400 | * @param string $path | ||
| 401 | * @param int $mode a combination of FileInfo::MODE_READONLY, FileInfo::MODE_ARCHIVE, FileInfo::MODE_SYSTEM and FileInfo::MODE_HIDDEN, FileInfo::NORMAL | ||
| 402 | * @return mixed | ||
| 403 | 2 | */ | |
| 404 | 2 | 	public function setMode(string $path, int $mode) { | |
| 427 | |||
| 428 | /** | ||
| 429 | * @param string $path | ||
| 430 | * @return INotifyHandler | ||
| 431 | * @throws ConnectionException | ||
| 432 | * @throws DependencyException | ||
| 433 | */ | ||
| 434 | 	public function notify(string $path): INotifyHandler { | ||
| 443 | |||
| 444 | /** | ||
| 445 | * @param string $command | ||
| 446 | * @return string[] | ||
| 447 | */ | ||
| 448 | 	protected function execute(string $command): array { | ||
| 452 | |||
| 453 | /** | ||
| 454 | * check output for errors | ||
| 455 | * | ||
| 456 | * @param string[] $lines | ||
| 457 | * @param string $path | ||
| 458 | 486 | * | |
| 459 | 486 | * @return bool | |
| 460 | 486 | * @throws AlreadyExistsException | |
| 461 | 2 | * @throws \Icewind\SMB\Exception\AccessDeniedException | |
| 462 | * @throws \Icewind\SMB\Exception\NotEmptyException | ||
| 463 | 486 | * @throws InvalidTypeException | |
| 464 | 486 | * @throws \Icewind\SMB\Exception\Exception | |
| 465 | 486 | * @throws NotFoundException | |
| 466 | 486 | */ | |
| 467 | 	protected function parseOutput(array $lines, string $path = ''): bool { | ||
| 474 | 242 | ||
| 475 | 242 | /** | |
| 476 | * @param string $string | ||
| 477 | * @return string | ||
| 478 | */ | ||
| 479 | 	protected function escape(string $string): string { | ||
| 482 | |||
| 483 | /** | ||
| 484 | * @param string $path | ||
| 485 | * @return string | ||
| 486 | */ | ||
| 487 | 	protected function escapePath(string $path): string { | ||
| 497 | |||
| 498 | /** | ||
| 499 | * @param string $path | ||
| 500 | * @return string | ||
| 501 | */ | ||
| 502 | 	protected function escapeLocalPath(string $path): string { | ||
| 506 | |||
| 507 | /** | ||
| 508 | * @param string $path | ||
| 509 | * @return ACL[] | ||
| 510 | * @throws ConnectionException | ||
| 511 | * @throws ConnectException | ||
| 512 | */ | ||
| 513 | 	protected function getAcls(string $path): array { | ||
| 538 | |||
| 539 | 	public function getServer(): IServer { | ||
| 542 | |||
| 543 | 	public function __destruct() { | ||
| 546 | } | ||
| 547 | 
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.