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 SyncService 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 SyncService, and based on these observations, apply Extract Interface, too.
| 1 | <?php  | 
            ||
| 43 | class SyncService { | 
            ||
| 44 | |||
| 45 | /** @var IConfig */  | 
            ||
| 46 | private $config;  | 
            ||
| 47 | /** @var ILogger */  | 
            ||
| 48 | private $logger;  | 
            ||
| 49 | /** @var AccountMapper */  | 
            ||
| 50 | private $mapper;  | 
            ||
| 51 | |||
| 52 | /**  | 
            ||
| 53 | * SyncService constructor.  | 
            ||
| 54 | *  | 
            ||
| 55 | * @param IConfig $config  | 
            ||
| 56 | * @param ILogger $logger  | 
            ||
| 57 | * @param AccountMapper $mapper  | 
            ||
| 58 | */  | 
            ||
| 59 | public function __construct(IConfig $config,  | 
            ||
| 60 | ILogger $logger,  | 
            ||
| 61 | 								AccountMapper $mapper) { | 
            ||
| 62 | $this->config = $config;  | 
            ||
| 63 | $this->logger = $logger;  | 
            ||
| 64 | $this->mapper = $mapper;  | 
            ||
| 65 | }  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * For unit tests  | 
            ||
| 69 | * @param AccountMapper $mapper  | 
            ||
| 70 | */  | 
            ||
| 71 | 	public function setAccountMapper(AccountMapper $mapper) { | 
            ||
| 72 | $this->mapper = $mapper;  | 
            ||
| 73 | }  | 
            ||
| 74 | |||
| 75 | /**  | 
            ||
| 76 | * @param UserInterface $backend the backend to check  | 
            ||
| 77 | * @param \Closure $callback is called for every user to allow progress display  | 
            ||
| 78 | * @return array  | 
            ||
| 79 | */  | 
            ||
| 80 | 	public function getNoLongerExistingUsers(UserInterface $backend, \Closure $callback) { | 
            ||
| 81 | // detect no longer existing users  | 
            ||
| 82 | $toBeDeleted = [];  | 
            ||
| 83 | $backendClass = get_class($backend);  | 
            ||
| 84 | 		$this->mapper->callForAllUsers(function (Account $a) use (&$toBeDeleted, $backend, $backendClass, $callback) { | 
            ||
| 85 | 			if ($a->getBackend() === $backendClass) { | 
            ||
| 86 | 				if (!$backend->userExists($a->getUserId())) { | 
            ||
| 87 | $toBeDeleted[] = $a->getUserId();  | 
            ||
| 88 | }  | 
            ||
| 89 | }  | 
            ||
| 90 | $callback($a);  | 
            ||
| 91 | }, '', false);  | 
            ||
| 92 | |||
| 93 | return $toBeDeleted;  | 
            ||
| 94 | }  | 
            ||
| 95 | |||
| 96 | /**  | 
            ||
| 97 | * @param UserInterface $backend to sync  | 
            ||
| 98 | * @param \Closure $callback is called for every user to progress display  | 
            ||
| 99 | */  | 
            ||
| 100 | 	public function run(UserInterface $backend, \Closure $callback) { | 
            ||
| 101 | $limit = 500;  | 
            ||
| 102 | $offset = 0;  | 
            ||
| 103 | $backendClass = get_class($backend);  | 
            ||
| 104 | 		do { | 
            ||
| 105 | 			$users = $backend->getUsers('', $limit, $offset); | 
            ||
| 106 | |||
| 107 | // update existing and insert new users  | 
            ||
| 108 | 			foreach ($users as $uid) { | 
            ||
| 109 | 				try { | 
            ||
| 110 | $account = $this->createOrSyncAccount($uid, $backend);  | 
            ||
| 111 | $uid = $account->getUserId(); // get correct case  | 
            ||
| 112 | // clean the user's preferences  | 
            ||
| 113 | $this->cleanPreferences($uid);  | 
            ||
| 114 | 				} catch (\Exception $e) { | 
            ||
| 115 | // Error syncing this user  | 
            ||
| 116 | 					$this->logger->error("Error syncing user with uid: $uid and backend: {get_class($backend)}"); | 
            ||
| 117 | $this->logger->logException($e);  | 
            ||
| 118 | }  | 
            ||
| 119 | |||
| 120 | // call the callback  | 
            ||
| 121 | $callback($uid);  | 
            ||
| 122 | }  | 
            ||
| 123 | $offset += $limit;  | 
            ||
| 124 | } while(count($users) >= $limit);  | 
            ||
| 125 | }  | 
            ||
| 126 | |||
| 127 | /**  | 
            ||
| 128 | * @param Account $a  | 
            ||
| 129 | */  | 
            ||
| 130 | 	private function syncState(Account $a) { | 
            ||
| 131 | $uid = $a->getUserId();  | 
            ||
| 132 | list($hasKey, $value) = $this->readUserConfig($uid, 'core', 'enabled');  | 
            ||
| 133 | 		if ($hasKey) { | 
            ||
| 134 | 			if ($value === 'true') { | 
            ||
| 135 | $a->setState(Account::STATE_ENABLED);  | 
            ||
| 136 | 			} else { | 
            ||
| 137 | $a->setState(Account::STATE_DISABLED);  | 
            ||
| 138 | }  | 
            ||
| 139 | 			if (array_key_exists('state', $a->getUpdatedFields())) { | 
            ||
| 140 | 				if ($value === 'true') { | 
            ||
| 141 | $this->logger->debug(  | 
            ||
| 142 | "Enabling <$uid>", ['app' => self::class]  | 
            ||
| 143 | );  | 
            ||
| 144 | 				} else { | 
            ||
| 145 | $this->logger->debug(  | 
            ||
| 146 | "Disabling <$uid>", ['app' => self::class]  | 
            ||
| 147 | );  | 
            ||
| 148 | }  | 
            ||
| 149 | }  | 
            ||
| 150 | }  | 
            ||
| 151 | }  | 
            ||
| 152 | |||
| 153 | /**  | 
            ||
| 154 | * @param Account $a  | 
            ||
| 155 | */  | 
            ||
| 156 | 	private function syncLastLogin(Account $a) { | 
            ||
| 168 | |||
| 169 | /**  | 
            ||
| 170 | * @param Account $a  | 
            ||
| 171 | * @param UserInterface $backend  | 
            ||
| 172 | */  | 
            ||
| 173 | View Code Duplication | 	private function syncEmail(Account $a, UserInterface $backend) { | 
            |
| 191 | |||
| 192 | /**  | 
            ||
| 193 | * @param Account $a  | 
            ||
| 194 | * @param UserInterface $backend  | 
            ||
| 195 | */  | 
            ||
| 196 | View Code Duplication | 	private function syncQuota(Account $a, UserInterface $backend) { | 
            |
| 216 | |||
| 217 | /**  | 
            ||
| 218 | * @param Account $a  | 
            ||
| 219 | * @param UserInterface $backend  | 
            ||
| 220 | */  | 
            ||
| 221 | 	private function syncHome(Account $a, UserInterface $backend) { | 
            ||
| 258 | |||
| 259 | /**  | 
            ||
| 260 | * @param Account $a  | 
            ||
| 261 | * @param UserInterface $backend  | 
            ||
| 262 | */  | 
            ||
| 263 | View Code Duplication | 	private function syncDisplayName(Account $a, UserInterface $backend) { | 
            |
| 275 | |||
| 276 | /**  | 
            ||
| 277 | * @param Account $a  | 
            ||
| 278 | * @param UserInterface $backend  | 
            ||
| 279 | */  | 
            ||
| 280 | View Code Duplication | 	private function syncSearchTerms(Account $a, UserInterface $backend) { | 
            |
| 293 | |||
| 294 | /**  | 
            ||
| 295 | * @param Account $a  | 
            ||
| 296 | * @param UserInterface $backend of the user  | 
            ||
| 297 | * @return Account  | 
            ||
| 298 | */  | 
            ||
| 299 | 	public function syncAccount(Account $a, UserInterface $backend) { | 
            ||
| 309 | |||
| 310 | /**  | 
            ||
| 311 | * @param $uid  | 
            ||
| 312 | * @param UserInterface $backend  | 
            ||
| 313 | * @return Account  | 
            ||
| 314 | * @throws \Exception  | 
            ||
| 315 | * @throws \InvalidArgumentException if you try to sync with a backend  | 
            ||
| 316 | * that doesnt match an existing account  | 
            ||
| 317 | */  | 
            ||
| 318 | 	public function createOrSyncAccount($uid, UserInterface $backend) { | 
            ||
| 349 | |||
| 350 | /**  | 
            ||
| 351 | * @param string $backend of the user  | 
            ||
| 352 | * @param string $uid of the user  | 
            ||
| 353 | * @return Account  | 
            ||
| 354 | */  | 
            ||
| 355 | 	public function createNewAccount($backend, $uid) { | 
            ||
| 363 | |||
| 364 | /**  | 
            ||
| 365 | * @param string $uid  | 
            ||
| 366 | * @param string $app  | 
            ||
| 367 | * @param string $key  | 
            ||
| 368 | * @return array  | 
            ||
| 369 | */  | 
            ||
| 370 | 	private function readUserConfig($uid, $app, $key) { | 
            ||
| 378 | |||
| 379 | /**  | 
            ||
| 380 | * @param string $uid  | 
            ||
| 381 | */  | 
            ||
| 382 | 	private function cleanPreferences($uid) { | 
            ||
| 388 | |||
| 389 | }  | 
            ||
| 390 |