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 Server 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 Server, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class Server extends SimpleContainer implements IServerContainer { |
||
| 33 | /** @var string */ |
||
| 34 | private $webRoot; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @param string $webRoot |
||
| 38 | */ |
||
| 39 | function __construct($webRoot) { |
||
| 304 | |||
| 305 | /** |
||
| 306 | * @return \OCP\Contacts\IManager |
||
| 307 | */ |
||
| 308 | function getContactsManager() { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * The current request object holding all information about the request |
||
| 314 | * currently being processed is returned from this method. |
||
| 315 | * In case the current execution was not initiated by a web request null is returned |
||
| 316 | * |
||
| 317 | * @return \OCP\IRequest|null |
||
| 318 | */ |
||
| 319 | function getRequest() { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Returns the preview manager which can create preview images for a given file |
||
| 325 | * |
||
| 326 | * @return \OCP\IPreview |
||
| 327 | */ |
||
| 328 | function getPreviewManager() { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Returns the tag manager which can get and set tags for different object types |
||
| 334 | * |
||
| 335 | * @see \OCP\ITagManager::load() |
||
| 336 | * @return \OCP\ITagManager |
||
| 337 | */ |
||
| 338 | function getTagManager() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Returns the avatar manager, used for avatar functionality |
||
| 344 | * |
||
| 345 | * @return \OCP\IAvatarManager |
||
| 346 | */ |
||
| 347 | function getAvatarManager() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Returns the root folder of ownCloud's data directory |
||
| 353 | * |
||
| 354 | * @return \OCP\Files\Folder |
||
| 355 | */ |
||
| 356 | function getRootFolder() { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Returns a view to ownCloud's files folder |
||
| 362 | * |
||
| 363 | * @param string $userId user ID |
||
| 364 | * @return \OCP\Files\Folder |
||
| 365 | */ |
||
| 366 | function getUserFolder($userId = null) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Returns an app-specific view in ownClouds data directory |
||
| 412 | * |
||
| 413 | * @return \OCP\Files\Folder |
||
| 414 | */ |
||
| 415 | function getAppFolder() { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return \OC\User\Manager |
||
| 429 | */ |
||
| 430 | function getUserManager() { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @return \OC\Group\Manager |
||
| 436 | */ |
||
| 437 | function getGroupManager() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * @return \OC\User\Session |
||
| 443 | */ |
||
| 444 | function getUserSession() { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * @return \OCP\ISession |
||
| 450 | */ |
||
| 451 | function getSession() { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @param \OCP\ISession $session |
||
| 457 | */ |
||
| 458 | function setSession(\OCP\ISession $session) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @return \OC\NavigationManager |
||
| 464 | */ |
||
| 465 | function getNavigationManager() { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return \OCP\IConfig |
||
| 471 | */ |
||
| 472 | function getConfig() { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * For internal use only |
||
| 478 | * |
||
| 479 | * @return \OC\SystemConfig |
||
| 480 | */ |
||
| 481 | function getSystemConfig() { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Returns the app config manager |
||
| 487 | * |
||
| 488 | * @return \OCP\IAppConfig |
||
| 489 | */ |
||
| 490 | function getAppConfig() { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * get an L10N instance |
||
| 496 | * |
||
| 497 | * @param string $app appid |
||
| 498 | * @param string $lang |
||
| 499 | * @return \OC_L10N |
||
| 500 | */ |
||
| 501 | function getL10N($app, $lang = null) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return \OCP\IURLGenerator |
||
| 507 | */ |
||
| 508 | function getURLGenerator() { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * @return \OCP\IHelper |
||
| 514 | */ |
||
| 515 | function getHelper() { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Returns an ICache instance |
||
| 521 | * |
||
| 522 | * @return \OCP\ICache |
||
| 523 | */ |
||
| 524 | function getCache() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Returns an \OCP\CacheFactory instance |
||
| 530 | * |
||
| 531 | * @return \OCP\ICacheFactory |
||
| 532 | */ |
||
| 533 | function getMemCacheFactory() { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Returns the current session |
||
| 539 | * |
||
| 540 | * @return \OCP\IDBConnection |
||
| 541 | */ |
||
| 542 | function getDatabaseConnection() { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns the activity manager |
||
| 548 | * |
||
| 549 | * @return \OCP\Activity\IManager |
||
| 550 | */ |
||
| 551 | function getActivityManager() { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Returns an job list for controlling background jobs |
||
| 557 | * |
||
| 558 | * @return \OCP\BackgroundJob\IJobList |
||
| 559 | */ |
||
| 560 | function getJobList() { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Returns a logger instance |
||
| 566 | * |
||
| 567 | * @return \OCP\ILogger |
||
| 568 | */ |
||
| 569 | function getLogger() { |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Returns a router for generating and matching urls |
||
| 575 | * |
||
| 576 | * @return \OCP\Route\IRouter |
||
| 577 | */ |
||
| 578 | function getRouter() { |
||
| 581 | |||
| 582 | /** |
||
| 583 | * Returns a search instance |
||
| 584 | * |
||
| 585 | * @return \OCP\ISearch |
||
| 586 | */ |
||
| 587 | function getSearch() { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Returns a SecureRandom instance |
||
| 593 | * |
||
| 594 | * @return \OCP\Security\ISecureRandom |
||
| 595 | */ |
||
| 596 | function getSecureRandom() { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Returns a Crypto instance |
||
| 602 | * |
||
| 603 | * @return \OCP\Security\ICrypto |
||
| 604 | */ |
||
| 605 | function getCrypto() { |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Returns a Hasher instance |
||
| 611 | * |
||
| 612 | * @return \OCP\Security\IHasher |
||
| 613 | */ |
||
| 614 | function getHasher() { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Returns an instance of the db facade |
||
| 620 | * |
||
| 621 | * @return \OCP\IDb |
||
| 622 | */ |
||
| 623 | function getDb() { |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Returns an instance of the HTTP helper class |
||
| 629 | * |
||
| 630 | * @return \OC\HTTPHelper |
||
| 631 | */ |
||
| 632 | function getHTTPHelper() { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Get the certificate manager for the user |
||
| 638 | * |
||
| 639 | * @param string $uid (optional) if not specified the current loggedin user is used |
||
| 640 | * @return \OCP\ICertificateManager | null if $uid is null and no user is logged in |
||
| 641 | */ |
||
| 642 | function getCertificateManager($uid = null) { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Create a new event source |
||
| 656 | * |
||
| 657 | * @return \OCP\IEventSource |
||
| 658 | */ |
||
| 659 | function createEventSource() { |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Get the active event logger |
||
| 665 | * |
||
| 666 | * The returned logger only logs data when debug mode is enabled |
||
| 667 | * |
||
| 668 | * @return \OCP\Diagnostics\IEventLogger |
||
| 669 | */ |
||
| 670 | function getEventLogger() { |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Get the active query logger |
||
| 676 | * |
||
| 677 | * The returned logger only logs data when debug mode is enabled |
||
| 678 | * |
||
| 679 | * @return \OCP\Diagnostics\IQueryLogger |
||
| 680 | */ |
||
| 681 | function getQueryLogger() { |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Get the manager for temporary files and folders |
||
| 687 | * |
||
| 688 | * @return \OCP\ITempManager |
||
| 689 | */ |
||
| 690 | function getTempManager() { |
||
| 693 | |||
| 694 | /** |
||
| 695 | * Get the app manager |
||
| 696 | * |
||
| 697 | * @return \OCP\App\IAppManager |
||
| 698 | */ |
||
| 699 | function getAppManager() { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Get the webroot |
||
| 705 | * |
||
| 706 | * @return string |
||
| 707 | */ |
||
| 708 | function getWebRoot() { |
||
| 711 | |||
| 712 | /** |
||
| 713 | * @return \OCP\IDateTimeZone |
||
| 714 | */ |
||
| 715 | public function getDateTimeZone() { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * @return \OCP\IDateTimeFormatter |
||
| 721 | */ |
||
| 722 | public function getDateTimeFormatter() { |
||
| 725 | |||
| 726 | /** |
||
| 727 | * @return \OCP\Files\Config\IMountProviderCollection |
||
| 728 | */ |
||
| 729 | function getMountProviderCollection(){ |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Get the IniWrapper |
||
| 735 | * |
||
| 736 | * @return IniGetWrapper |
||
| 737 | */ |
||
| 738 | public function getIniWrapper() { |
||
| 741 | } |
||
| 742 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.