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:
| 1 | <?php |
||
| 44 | class GroupFoldersService { |
||
| 45 | |||
| 46 | |||
| 47 | const DOCUMENT_SOURCE = 'group_folders'; |
||
| 48 | |||
| 49 | /** @var IRootFolder */ |
||
| 50 | private $rootFolder; |
||
| 51 | |||
| 52 | /** @var IUserManager */ |
||
| 53 | private $userManager; |
||
| 54 | |||
| 55 | /** @var IManager */ |
||
| 56 | private $shareManager; |
||
| 57 | |||
| 58 | /** @var FolderManager */ |
||
| 59 | private $folderManager; |
||
| 60 | |||
| 61 | /** @var LocalFilesService */ |
||
| 62 | private $localFilesService; |
||
| 63 | |||
| 64 | /** @var ConfigService */ |
||
| 65 | private $configService; |
||
| 66 | |||
| 67 | /** @var MiscService */ |
||
| 68 | private $miscService; |
||
| 69 | |||
| 70 | |||
| 71 | /** @var MountPoint[] */ |
||
| 72 | private $groupFolders = []; |
||
| 73 | |||
| 74 | |||
| 75 | /** |
||
| 76 | * ExternalFilesService constructor. |
||
| 77 | * |
||
| 78 | * @param IRootFolder $rootFolder |
||
| 79 | * @param IUserManager $userManager |
||
| 80 | * @param IManager $shareManager |
||
| 81 | * @param FolderManager $folderManager |
||
| 82 | * @param LocalFilesService $localFilesService |
||
| 83 | * @param ConfigService $configService |
||
| 84 | * @param MiscService $miscService |
||
| 85 | */ |
||
| 86 | View Code Duplication | public function __construct( |
|
| 102 | |||
| 103 | |||
| 104 | /** |
||
| 105 | * @param string $userId |
||
| 106 | */ |
||
| 107 | public function initGroupSharesForUser($userId) { |
||
| 116 | |||
| 117 | |||
| 118 | /** |
||
| 119 | * @param Node $file |
||
| 120 | * |
||
| 121 | * @param string $source |
||
| 122 | * |
||
| 123 | * @throws KnownFileSourceException |
||
| 124 | */ |
||
| 125 | public function getFileSource(Node $file, &$source) { |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * @param FilesDocument $document |
||
| 140 | * @param Node $file |
||
| 141 | */ |
||
| 142 | public function updateDocumentAccess(FilesDocument &$document, Node $file) { |
||
| 171 | |||
| 172 | |||
| 173 | /** |
||
| 174 | * @param FilesDocument $document |
||
| 175 | * @param array $users |
||
| 176 | */ |
||
| 177 | public function getShareUsers(FilesDocument $document, &$users) { |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * @param Node $file |
||
| 189 | * |
||
| 190 | * @return MountPoint |
||
| 191 | * @throws FileIsNotIndexableException |
||
| 192 | */ |
||
| 193 | private function getMountPoint(Node $file) { |
||
| 205 | |||
| 206 | |||
| 207 | /** |
||
| 208 | * @param string $userId |
||
| 209 | * |
||
| 210 | * @return MountPoint[] |
||
| 211 | */ |
||
| 212 | private function getMountPoints($userId) { |
||
| 226 | |||
| 227 | |||
| 228 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.