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 elFinderVolumeS3 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 elFinderVolumeS3, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class elFinderVolumeS3 extends elFinderVolumeDriver {
|
||
|
|
|||
| 12 | protected $driverId = 's3s'; |
||
| 13 | |||
| 14 | protected $s3; |
||
| 15 | |||
| 16 | public function __construct() {
|
||
| 27 | |||
| 28 | |||
| 29 | protected function init() {
|
||
| 44 | |||
| 45 | protected function configure() {
|
||
| 54 | |||
| 55 | /** |
||
| 56 | * Return parent directory path |
||
| 57 | * |
||
| 58 | * @param string $path file path |
||
| 59 | * @return string |
||
| 60 | * @author Dmitry (dio) Levashov |
||
| 61 | **/ |
||
| 62 | protected function _dirname($path) {
|
||
| 73 | |||
| 74 | /** |
||
| 75 | * Return file name |
||
| 76 | * |
||
| 77 | * @param string $path file path |
||
| 78 | * @return string |
||
| 79 | * @author Dmitry (dio) Levashov |
||
| 80 | **/ |
||
| 81 | protected function _basename($path) {
|
||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | /** |
||
| 88 | * Join dir name and file name and return full path. |
||
| 89 | * Some drivers (db) use int as path - so we give to concat path to driver itself |
||
| 90 | * |
||
| 91 | * @param string $dir dir path |
||
| 92 | * @param string $name file name |
||
| 93 | * @return string |
||
| 94 | * @author Dmitry (dio) Levashov |
||
| 95 | **/ |
||
| 96 | protected function _joinPath($dir, $name) {
|
||
| 99 | |||
| 100 | /** |
||
| 101 | * Return normalized path, this works the same as os.path.normpath() in Python |
||
| 102 | * |
||
| 103 | * @param string $path path |
||
| 104 | * @return string |
||
| 105 | * @author Troex Nevelin |
||
| 106 | **/ |
||
| 107 | protected function _normpath($path) {
|
||
| 113 | |||
| 114 | /** |
||
| 115 | * Return file path related to root dir |
||
| 116 | * |
||
| 117 | * @param string $path file path |
||
| 118 | * @return string |
||
| 119 | * @author Dmitry (dio) Levashov |
||
| 120 | **/ |
||
| 121 | protected function _relpath($path) {
|
||
| 137 | |||
| 138 | /** |
||
| 139 | * Convert path related to root dir into real path |
||
| 140 | * |
||
| 141 | * @param string $path file path |
||
| 142 | * @return string |
||
| 143 | * @author Dmitry (dio) Levashov |
||
| 144 | **/ |
||
| 145 | protected function _abspath($path) {
|
||
| 148 | |||
| 149 | /** |
||
| 150 | * Return fake path started from root dir |
||
| 151 | * |
||
| 152 | * @param string $path file path |
||
| 153 | * @return string |
||
| 154 | * @author Dmitry (dio) Levashov |
||
| 155 | **/ |
||
| 156 | protected function _path($path) {
|
||
| 159 | |||
| 160 | /** |
||
| 161 | * Return true if $path is children of $parent |
||
| 162 | * |
||
| 163 | * @param string $path path to check |
||
| 164 | * @param string $parent parent path |
||
| 165 | * @return bool |
||
| 166 | * @author Dmitry (dio) Levashov |
||
| 167 | **/ |
||
| 168 | protected function _inpath($path, $parent) {
|
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * Converting array of objects with name and value properties to |
||
| 175 | * array[key] = value |
||
| 176 | * @param array $metadata source array |
||
| 177 | * @return array |
||
| 178 | * @author Alexey Sukhotin |
||
| 179 | **/ |
||
| 180 | protected function metaobj2array($metadata) {
|
||
| 192 | |||
| 193 | /** |
||
| 194 | * Return stat for given path. |
||
| 195 | * Stat contains following fields: |
||
| 196 | * - (int) size file size in b. required |
||
| 197 | * - (int) ts file modification time in unix time. required |
||
| 198 | * - (string) mime mimetype. required for folders, others - optionally |
||
| 199 | * - (bool) read read permissions. required |
||
| 200 | * - (bool) write write permissions. required |
||
| 201 | * - (bool) locked is object locked. optionally |
||
| 202 | * - (bool) hidden is object hidden. optionally |
||
| 203 | * - (string) alias for symlinks - link target path relative to root path. optionally |
||
| 204 | * - (string) target for symlinks - link target path. optionally |
||
| 205 | * |
||
| 206 | * If file does not exists - returns empty array or false. |
||
| 207 | * |
||
| 208 | * @param string $path file path |
||
| 209 | * @return array|false |
||
| 210 | * @author Dmitry (dio) Levashov, |
||
| 211 | * @author Alexey Sukhotin |
||
| 212 | **/ |
||
| 213 | protected function _stat($path) {
|
||
| 284 | |||
| 285 | |||
| 286 | |||
| 287 | /***************** file stat ********************/ |
||
| 288 | |||
| 289 | |||
| 290 | /** |
||
| 291 | * Return true if path is dir and has at least one childs directory |
||
| 292 | * |
||
| 293 | * @param string $path dir path |
||
| 294 | * @return bool |
||
| 295 | * @author Alexey Sukhotin |
||
| 296 | **/ |
||
| 297 | protected function _subdirs($path) {
|
||
| 313 | |||
| 314 | /** |
||
| 315 | * Return object width and height |
||
| 316 | * Ususaly used for images, but can be realize for video etc... |
||
| 317 | * |
||
| 318 | * @param string $path file path |
||
| 319 | * @param string $mime file mime type |
||
| 320 | * @return string|false |
||
| 321 | * @author Dmitry (dio) Levashov |
||
| 322 | * @author Naoki Sawada |
||
| 323 | **/ |
||
| 324 | View Code Duplication | protected function _dimensions($path, $mime) {
|
|
| 331 | |||
| 332 | /******************** file/dir content *********************/ |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Return files list in directory |
||
| 336 | * |
||
| 337 | * @param string $path dir path |
||
| 338 | * @return array |
||
| 339 | * @author Dmitry (dio) Levashov, |
||
| 340 | * @author Alexey Sukhotin |
||
| 341 | **/ |
||
| 342 | protected function _scandir($path) {
|
||
| 366 | |||
| 367 | /** |
||
| 368 | * Open file and return file pointer |
||
| 369 | * |
||
| 370 | * @param string $path file path |
||
| 371 | * @param string $mode open mode |
||
| 372 | * @return resource|false |
||
| 373 | * @author Dmitry (dio) Levashov, |
||
| 374 | * @author Alexey Sukhotin |
||
| 375 | **/ |
||
| 376 | protected function _fopen($path, $mode="rb") {
|
||
| 404 | |||
| 405 | /** |
||
| 406 | * Close opened file |
||
| 407 | * |
||
| 408 | * @param resource $fp file pointer |
||
| 409 | * @param string $path file path |
||
| 410 | * @return bool |
||
| 411 | * @author Dmitry (dio) Levashov |
||
| 412 | **/ |
||
| 413 | protected function _fclose($fp, $path='') {
|
||
| 419 | |||
| 420 | /******************** file/dir manipulations *************************/ |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Create dir and return created dir path or false on failed |
||
| 424 | * |
||
| 425 | * @param string $path parent dir path |
||
| 426 | * @param string $name new directory name |
||
| 427 | * @return string|bool |
||
| 428 | * @author Dmitry (dio) Levashov, |
||
| 429 | * @author Alexey Sukhotin |
||
| 430 | **/ |
||
| 431 | protected function _mkdir($path, $name) {
|
||
| 449 | |||
| 450 | /** |
||
| 451 | * Create file and return it's path or false on failed |
||
| 452 | * |
||
| 453 | * @param string $path parent dir path |
||
| 454 | * @param string $name new file name |
||
| 455 | * @return string|bool |
||
| 456 | * @author Dmitry (dio) Levashov, |
||
| 457 | * @author Alexey Sukhotin |
||
| 458 | **/ |
||
| 459 | protected function _mkfile($path, $name) {
|
||
| 477 | |||
| 478 | /** |
||
| 479 | * Create symlink |
||
| 480 | * |
||
| 481 | * @param string $source file to link to |
||
| 482 | * @param string $targetDir folder to create link in |
||
| 483 | * @param string $name symlink name |
||
| 484 | * @return bool |
||
| 485 | * @author Dmitry (dio) Levashov |
||
| 486 | **/ |
||
| 487 | protected function _symlink($source, $targetDir, $name) {
|
||
| 490 | |||
| 491 | /** |
||
| 492 | * Copy file into another file (only inside one volume) |
||
| 493 | * |
||
| 494 | * @param string $source source file path |
||
| 495 | * @param string $targetDir target dir path |
||
| 496 | * @param string $name file name |
||
| 497 | * @return bool |
||
| 498 | * @author Dmitry (dio) Levashov |
||
| 499 | **/ |
||
| 500 | protected function _copy($source, $targetDir, $name) {
|
||
| 503 | |||
| 504 | /** |
||
| 505 | * Move file into another parent dir. |
||
| 506 | * Return new file path or false. |
||
| 507 | * |
||
| 508 | * @param string $source source file path |
||
| 509 | * @param string $targetDir target dir path |
||
| 510 | * @param string $name file name |
||
| 511 | * @return string|bool |
||
| 512 | * @author Dmitry (dio) Levashov |
||
| 513 | **/ |
||
| 514 | protected function _move($source, $targetDir, $name) {
|
||
| 517 | |||
| 518 | /** |
||
| 519 | * Remove file |
||
| 520 | * |
||
| 521 | * @param string $path file path |
||
| 522 | * @return bool |
||
| 523 | * @author Dmitry (dio) Levashov |
||
| 524 | **/ |
||
| 525 | protected function _unlink($path) {
|
||
| 557 | |||
| 558 | /** |
||
| 559 | * Remove dir |
||
| 560 | * |
||
| 561 | * @param string $path dir path |
||
| 562 | * @return bool |
||
| 563 | * @author Dmitry (dio) Levashov |
||
| 564 | **/ |
||
| 565 | protected function _rmdir($path) {
|
||
| 568 | |||
| 569 | /** |
||
| 570 | * Create new file and write into it from file pointer. |
||
| 571 | * Return new file path or false on error. |
||
| 572 | * |
||
| 573 | * @param resource $fp file pointer |
||
| 574 | * @param string $dir target dir path |
||
| 575 | * @param string $name file name |
||
| 576 | * @param array $stat |
||
| 577 | * @return bool|string |
||
| 578 | * @author Dmitry (dio) Levashov |
||
| 579 | */ |
||
| 580 | protected function _save($fp, $dir, $name, $stat) {
|
||
| 583 | |||
| 584 | /** |
||
| 585 | * Get file contents |
||
| 586 | * |
||
| 587 | * @param string $path file path |
||
| 588 | * @return string|false |
||
| 589 | * @author Dmitry (dio) Levashov |
||
| 590 | **/ |
||
| 591 | protected function _getContents($path) {
|
||
| 594 | |||
| 595 | /** |
||
| 596 | * Write a string to a file |
||
| 597 | * |
||
| 598 | * @param string $path file path |
||
| 599 | * @param string $content new file content |
||
| 600 | * @return bool |
||
| 601 | * @author Dmitry (dio) Levashov |
||
| 602 | **/ |
||
| 603 | protected function _filePutContents($path, $content) {
|
||
| 606 | |||
| 607 | /** |
||
| 608 | * Extract files from archive |
||
| 609 | * |
||
| 610 | * @param string $path file path |
||
| 611 | * @param array $arc archiver options |
||
| 612 | * @return bool |
||
| 613 | * @author Dmitry (dio) Levashov, |
||
| 614 | * @author Alexey Sukhotin |
||
| 615 | **/ |
||
| 616 | protected function _extract($path, $arc) {
|
||
| 619 | |||
| 620 | /** |
||
| 621 | * Create archive and return its path |
||
| 622 | * |
||
| 623 | * @param string $dir target dir |
||
| 624 | * @param array $files files names list |
||
| 625 | * @param string $name archive name |
||
| 626 | * @param array $arc archiver options |
||
| 627 | * @return string|bool |
||
| 628 | * @author Dmitry (dio) Levashov, |
||
| 629 | * @author Alexey Sukhotin |
||
| 630 | **/ |
||
| 631 | protected function _archive($dir, $files, $name, $arc) {
|
||
| 634 | |||
| 635 | /** |
||
| 636 | * Detect available archivers |
||
| 637 | * |
||
| 638 | * @return void |
||
| 639 | * @author Dmitry (dio) Levashov, |
||
| 640 | * @author Alexey Sukhotin |
||
| 641 | **/ |
||
| 642 | protected function _checkArchivers() {
|
||
| 645 | |||
| 646 | /** |
||
| 647 | * chmod implementation |
||
| 648 | * |
||
| 649 | * @param string $path |
||
| 650 | * @param string $mode |
||
| 651 | * @return bool |
||
| 652 | */ |
||
| 653 | protected function _chmod($path, $mode) {
|
||
| 656 | |||
| 657 | } |
||
| 658 | |||
| 742 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.