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  | 
            ||
| 61 | class ExternalFilesService { | 
            ||
| 62 | |||
| 63 | |||
| 64 | use TArrayTools;  | 
            ||
| 65 | |||
| 66 | |||
| 67 | /** @var IRootFolder */  | 
            ||
| 68 | private $rootFolder;  | 
            ||
| 69 | |||
| 70 | /** @var AppManager */  | 
            ||
| 71 | private $appManager;  | 
            ||
| 72 | |||
| 73 | /** @var IUserManager */  | 
            ||
| 74 | private $userManager;  | 
            ||
| 75 | |||
| 76 | /** @var IManager */  | 
            ||
| 77 | private $shareManager;  | 
            ||
| 78 | |||
| 79 | /** @var UserGlobalStoragesService */  | 
            ||
| 80 | private $userGlobalStoragesService;  | 
            ||
| 81 | |||
| 82 | /** @var GlobalStoragesService */  | 
            ||
| 83 | private $globalStoragesService;  | 
            ||
| 84 | |||
| 85 | /** @var IGroupManager */  | 
            ||
| 86 | private $groupManager;  | 
            ||
| 87 | |||
| 88 | /** @var LocalFilesService */  | 
            ||
| 89 | private $localFilesService;  | 
            ||
| 90 | |||
| 91 | /** @var ConfigService */  | 
            ||
| 92 | private $configService;  | 
            ||
| 93 | |||
| 94 | /** @var MiscService */  | 
            ||
| 95 | private $miscService;  | 
            ||
| 96 | |||
| 97 | |||
| 98 | /**  | 
            ||
| 99 | * ExternalFilesService constructor.  | 
            ||
| 100 | *  | 
            ||
| 101 | * @param IRootFolder $rootFolder  | 
            ||
| 102 | * @param IAppManager $appManager  | 
            ||
| 103 | * @param IUserManager $userManager  | 
            ||
| 104 | * @param IGroupManager $groupManager  | 
            ||
| 105 | * @param IManager $shareManager  | 
            ||
| 106 | * @param LocalFilesService $localFilesService  | 
            ||
| 107 | * @param ConfigService $configService  | 
            ||
| 108 | * @param MiscService $miscService  | 
            ||
| 109 | */  | 
            ||
| 110 | View Code Duplication | public function __construct(  | 
            |
| 
                                                                                                    
                        
                         | 
                |||
| 111 | IRootFolder $rootFolder, IAppManager $appManager, IUserManager $userManager,  | 
            ||
| 112 | IGroupManager $groupManager, IManager $shareManager, LocalFilesService $localFilesService,  | 
            ||
| 113 | ConfigService $configService, MiscService $miscService  | 
            ||
| 114 | 	) { | 
            ||
| 115 | $this->rootFolder = $rootFolder;  | 
            ||
| 116 | $this->appManager = $appManager;  | 
            ||
| 117 | $this->userManager = $userManager;  | 
            ||
| 118 | $this->groupManager = $groupManager;  | 
            ||
| 119 | $this->shareManager = $shareManager;  | 
            ||
| 120 | |||
| 121 | $this->localFilesService = $localFilesService;  | 
            ||
| 122 | |||
| 123 | $this->configService = $configService;  | 
            ||
| 124 | $this->miscService = $miscService;  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | |||
| 128 | /**  | 
            ||
| 129 | * @param string $userId  | 
            ||
| 130 | */  | 
            ||
| 131 | 	public function initExternalFilesForUser(string $userId) { | 
            ||
| 132 | 		if (!$this->appManager->isInstalled('files_external')) { | 
            ||
| 133 | return;  | 
            ||
| 134 | }  | 
            ||
| 135 | |||
| 136 | $this->userGlobalStoragesService = OC::$server->getUserGlobalStoragesService();  | 
            ||
| 137 | $this->globalStoragesService = OC::$server->getGlobalStoragesService();  | 
            ||
| 138 | }  | 
            ||
| 139 | |||
| 140 | |||
| 141 | /**  | 
            ||
| 142 | * @param Node $file  | 
            ||
| 143 | *  | 
            ||
| 144 | * @param string $source  | 
            ||
| 145 | *  | 
            ||
| 146 | * @throws FileIsNotIndexableException  | 
            ||
| 147 | * @throws KnownFileSourceException  | 
            ||
| 148 | */  | 
            ||
| 149 | View Code Duplication | 	public function getFileSource(Node $file, string &$source) { | 
            |
| 161 | |||
| 162 | |||
| 163 | /**  | 
            ||
| 164 | * @param FilesDocument $document  | 
            ||
| 165 | * @param array $users  | 
            ||
| 166 | */  | 
            ||
| 167 | 	public function getShareUsers(FilesDocument $document, array &$users) { | 
            ||
| 174 | |||
| 175 | |||
| 176 | /**  | 
            ||
| 177 | * @param FilesDocument $document  | 
            ||
| 178 | * @param Node $file  | 
            ||
| 179 | *  | 
            ||
| 180 | * @throws FileIsNotIndexableException  | 
            ||
| 181 | */  | 
            ||
| 182 | 	public function updateDocumentAccess(FilesDocument &$document, Node $file) { | 
            ||
| 209 | |||
| 210 | |||
| 211 | /**  | 
            ||
| 212 | * @param MountPoint $mount  | 
            ||
| 213 | *  | 
            ||
| 214 | * @return bool  | 
            ||
| 215 | */  | 
            ||
| 216 | 	private function isMountFullGlobal(MountPoint $mount): bool { | 
            ||
| 231 | |||
| 232 | |||
| 233 | /**  | 
            ||
| 234 | * @param Node $file  | 
            ||
| 235 | *  | 
            ||
| 236 | * @return MountPoint  | 
            ||
| 237 | * @throws FileIsNotIndexableException  | 
            ||
| 238 | */  | 
            ||
| 239 | 	private function getMountPoint(Node $file): MountPoint { | 
            ||
| 250 | |||
| 251 | |||
| 252 | /**  | 
            ||
| 253 | * @param int $externalMountId  | 
            ||
| 254 | *  | 
            ||
| 255 | * @return MountPoint  | 
            ||
| 256 | * @throws ExternalMountNotFoundException  | 
            ||
| 257 | */  | 
            ||
| 258 | 	private function getExternalMountById(int $externalMountId): MountPoint { | 
            ||
| 278 | |||
| 279 | |||
| 280 | /**  | 
            ||
| 281 | * @param IIndex $index  | 
            ||
| 282 | */  | 
            ||
| 283 | 	public function impersonateOwner(IIndex $index) { | 
            ||
| 300 | |||
| 301 | |||
| 302 | /**  | 
            ||
| 303 | * @param MountPoint $mount  | 
            ||
| 304 | *  | 
            ||
| 305 | * @return string  | 
            ||
| 306 | * @throws ExternalMountWithNoViewerException  | 
            ||
| 307 | */  | 
            ||
| 308 | 	private function getRandomUserFromMountPoint(MountPoint $mount): string { | 
            ||
| 333 | |||
| 334 | }  | 
            ||
| 335 | |||
| 336 | 
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.