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 |
||
23 | class UserHandler extends AbstractInMemoryHandler implements UserHandlerInterface |
||
24 | { |
||
25 | /** @var callable */ |
||
26 | private $getUserTags; |
||
27 | |||
28 | /** @var callable */ |
||
29 | private $getUserKeys; |
||
30 | |||
31 | /** @var callable */ |
||
32 | private $getRoleTags; |
||
33 | |||
34 | /** @var callable */ |
||
35 | private $getRoleKeys; |
||
36 | |||
37 | /** @var callable */ |
||
38 | private $getRoleAssignmentTags; |
||
39 | |||
40 | /** @var callable */ |
||
41 | private $getRoleAssignmentKeys; |
||
42 | |||
43 | /** |
||
44 | * Set callback functions for use in cache retrival. |
||
45 | */ |
||
46 | public function init(): void |
||
80 | |||
81 | /** |
||
82 | * {@inheritdoc} |
||
83 | */ |
||
84 | public function create(User $user) |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function load($userId) |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | View Code Duplication | public function loadByLogin($login) |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | View Code Duplication | public function loadByEmail($email) |
|
148 | |||
149 | /** |
||
150 | * {@inheritdoc} |
||
151 | */ |
||
152 | public function loadUserByToken($hash) |
||
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | public function update(User $user) |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | View Code Duplication | public function updateUserToken(UserTokenUpdateStruct $userTokenUpdateStruct) |
|
198 | { |
||
199 | $this->logger->logCall(__METHOD__, array('struct' => $userTokenUpdateStruct)); |
||
200 | $return = $this->persistenceHandler->userHandler()->updateUserToken($userTokenUpdateStruct); |
||
201 | |||
202 | // As we 1. don't know original hash, and 2. hash is not guaranteed to be unique, we do it like this for now |
||
203 | $this->cache->invalidateTags(['user-' . $userTokenUpdateStruct->userId . '-account-key']); |
||
204 | $this->cache->deleteItems(['ez-user-' . $userTokenUpdateStruct->hashKey . '-by-account-key']); |
||
205 | |||
206 | return $return; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | View Code Duplication | public function expireUserToken($hash) |
|
213 | { |
||
214 | $this->logger->logCall(__METHOD__, array('hash' => $hash)); |
||
215 | $return = $this->persistenceHandler->userHandler()->expireUserToken($hash); |
||
216 | $this->cache->deleteItems(['ez-user-' . $hash . '-by-account-key']); |
||
217 | |||
218 | return $return; |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | View Code Duplication | public function delete($userId) |
|
225 | { |
||
226 | $this->logger->logCall(__METHOD__, array('user' => $userId)); |
||
227 | $return = $this->persistenceHandler->userHandler()->delete($userId); |
||
228 | |||
229 | // user id == content id == group id |
||
230 | $this->cache->invalidateTags(['content-fields-' . $userId, 'user-' . $userId]); |
||
231 | |||
232 | return $return; |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * {@inheritdoc} |
||
237 | */ |
||
238 | public function createRole(RoleCreateStruct $createStruct) |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function createRoleDraft($roleId) |
||
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | View Code Duplication | public function loadRole($roleId, $status = Role::STATUS_DEFINED) |
|
276 | |||
277 | /** |
||
278 | * {@inheritdoc} |
||
279 | */ |
||
280 | View Code Duplication | public function loadRoleByIdentifier($identifier, $status = Role::STATUS_DEFINED) |
|
299 | |||
300 | /** |
||
301 | * {@inheritdoc} |
||
302 | */ |
||
303 | public function loadRoleDraftByRoleId($roleId) |
||
309 | |||
310 | /** |
||
311 | * {@inheritdoc} |
||
312 | */ |
||
313 | public function loadRoles() |
||
319 | |||
320 | /** |
||
321 | * {@inheritdoc} |
||
322 | */ |
||
323 | public function loadRoleAssignment($roleAssignmentId) |
||
335 | |||
336 | /** |
||
337 | * {@inheritdoc} |
||
338 | */ |
||
339 | public function loadRoleAssignmentsByRoleId($roleId) |
||
355 | |||
356 | /** |
||
357 | * {@inheritdoc} |
||
358 | */ |
||
359 | public function loadRoleAssignmentsByGroupId($groupId, $inherit = false) |
||
391 | |||
392 | /** |
||
393 | * {@inheritdoc} |
||
394 | */ |
||
395 | View Code Duplication | public function updateRole(RoleUpdateStruct $struct) |
|
396 | { |
||
397 | $this->logger->logCall(__METHOD__, array('struct' => $struct)); |
||
398 | $this->persistenceHandler->userHandler()->updateRole($struct); |
||
399 | |||
400 | $this->cache->invalidateTags(['role-' . $struct->id]); |
||
401 | } |
||
402 | |||
403 | /** |
||
404 | * {@inheritdoc} |
||
405 | */ |
||
406 | View Code Duplication | public function deleteRole($roleId, $status = Role::STATUS_DEFINED) |
|
417 | |||
418 | /** |
||
419 | * {@inheritdoc} |
||
420 | */ |
||
421 | public function publishRoleDraft($roleDraftId) |
||
435 | |||
436 | /** |
||
437 | * {@inheritdoc} |
||
438 | */ |
||
439 | public function addPolicyByRoleDraft($roleId, Policy $policy) |
||
445 | |||
446 | /** |
||
447 | * {@inheritdoc} |
||
448 | */ |
||
449 | View Code Duplication | public function addPolicy($roleId, Policy $policy) |
|
450 | { |
||
451 | $this->logger->logCall(__METHOD__, array('role' => $roleId, 'struct' => $policy)); |
||
452 | $return = $this->persistenceHandler->userHandler()->addPolicy($roleId, $policy); |
||
453 | |||
454 | $this->cache->invalidateTags(['role-' . $roleId]); |
||
455 | |||
456 | return $return; |
||
457 | } |
||
458 | |||
459 | /** |
||
460 | * {@inheritdoc} |
||
461 | */ |
||
462 | public function updatePolicy(Policy $policy) |
||
471 | |||
472 | /** |
||
473 | * {@inheritdoc} |
||
474 | */ |
||
475 | View Code Duplication | public function deletePolicy($policyId, $roleId) |
|
476 | { |
||
477 | $this->logger->logCall(__METHOD__, array('policy' => $policyId)); |
||
478 | $this->persistenceHandler->userHandler()->deletePolicy($policyId, $roleId); |
||
479 | |||
480 | $this->cache->invalidateTags(['policy-' . $policyId, 'role-' . $roleId]); |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * {@inheritdoc} |
||
485 | */ |
||
486 | public function loadPoliciesByUserId($userId) |
||
492 | |||
493 | /** |
||
494 | * {@inheritdoc} |
||
495 | */ |
||
496 | public function assignRole($contentId, $roleId, array $limitation = null) |
||
511 | |||
512 | /** |
||
513 | * {@inheritdoc} |
||
514 | */ |
||
515 | public function unassignRole($contentId, $roleId) |
||
524 | |||
525 | /** |
||
526 | * {@inheritdoc} |
||
527 | */ |
||
528 | View Code Duplication | public function removeRoleAssignment($roleAssignmentId) |
|
529 | { |
||
530 | $this->logger->logCall(__METHOD__, array('assignment' => $roleAssignmentId)); |
||
537 | } |
||
538 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.