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 |
||
| 22 | class UserHandler extends AbstractHandler implements UserHandlerInterface |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * {@inheritdoc} |
||
| 26 | */ |
||
| 27 | public function create(User $user) |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritdoc} |
||
| 40 | */ |
||
| 41 | public function load($userId) |
||
| 47 | |||
| 48 | /** |
||
| 49 | * {@inheritdoc} |
||
| 50 | */ |
||
| 51 | public function loadByLogin($login) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * {@inheritdoc} |
||
| 60 | */ |
||
| 61 | public function loadByEmail($email) |
||
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | public function update(User $user) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function delete($userId) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * {@inheritdoc} |
||
| 100 | */ |
||
| 101 | public function createRole(RoleCreateStruct $createStruct) |
||
| 107 | |||
| 108 | /** |
||
| 109 | * {@inheritdoc} |
||
| 110 | */ |
||
| 111 | public function createRoleDraft($roleId) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * {@inheritdoc} |
||
| 120 | */ |
||
| 121 | public function loadRole($roleId, $status = Role::STATUS_DEFINED) |
||
| 122 | { |
||
| 123 | if ($status === Role::STATUS_DEFINED) { |
||
| 124 | $cache = $this->cache->getItem('user', 'role', $roleId); |
||
| 125 | $role = $cache->get(); |
||
| 126 | if ($cache->isMiss()) { |
||
| 127 | $this->logger->logCall(__METHOD__, array('role' => $roleId)); |
||
| 128 | $role = $this->persistenceHandler->userHandler()->loadRole($roleId, $status); |
||
| 129 | $cache->set($role)->save(); |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | $role = $this->persistenceHandler->userHandler()->loadRole($roleId, $status); |
||
| 133 | } |
||
| 134 | |||
| 135 | return $role; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * {@inheritdoc} |
||
| 140 | */ |
||
| 141 | public function loadRoleByIdentifier($identifier, $status = Role::STATUS_DEFINED) |
||
| 147 | |||
| 148 | /** |
||
| 149 | * {@inheritdoc} |
||
| 150 | */ |
||
| 151 | public function loadRoleDraftByRoleId($roleId) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * {@inheritdoc} |
||
| 160 | */ |
||
| 161 | public function loadRoles() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * {@inheritdoc} |
||
| 170 | */ |
||
| 171 | public function loadRoleAssignment($roleAssignmentId) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function loadRoleAssignmentsByRoleId($roleId) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * {@inheritdoc} |
||
| 190 | */ |
||
| 191 | public function loadRoleAssignmentsByGroupId($groupId, $inherit = false) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * {@inheritdoc} |
||
| 213 | */ |
||
| 214 | View Code Duplication | public function updateRole(RoleUpdateStruct $struct) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * {@inheritdoc} |
||
| 224 | */ |
||
| 225 | public function deleteRole($roleId, $status = Role::STATUS_DEFINED) |
||
| 226 | { |
||
| 227 | $this->logger->logCall(__METHOD__, array('role' => $roleId)); |
||
| 228 | $return = $this->persistenceHandler->userHandler()->deleteRole($roleId, $status); |
||
| 229 | |||
| 230 | if ($status === Role::STATUS_DEFINED) { |
||
| 231 | $this->cache->clear('user', 'role', $roleId); |
||
| 232 | $this->cache->clear('user', 'role', 'assignments'); |
||
| 233 | } |
||
| 234 | |||
| 235 | return $return; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * {@inheritdoc} |
||
| 240 | */ |
||
| 241 | public function publishRoleDraft($roleDraftId) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * {@inheritdoc} |
||
| 267 | */ |
||
| 268 | public function addPolicyByRoleDraft($roleId, Policy $policy) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * {@inheritdoc} |
||
| 277 | */ |
||
| 278 | public function addPolicy($roleId, Policy $policy) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * {@inheritdoc} |
||
| 290 | */ |
||
| 291 | public function updatePolicy(Policy $policy) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * {@inheritdoc} |
||
| 303 | */ |
||
| 304 | View Code Duplication | public function deletePolicy($policyId) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * {@inheritdoc} |
||
| 314 | */ |
||
| 315 | public function loadPoliciesByUserId($userId) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * {@inheritdoc} |
||
| 324 | */ |
||
| 325 | View Code Duplication | public function assignRole($contentId, $roleId, array $limitation = null) |
|
| 336 | |||
| 337 | /** |
||
| 338 | * {@inheritdoc} |
||
| 339 | */ |
||
| 340 | View Code Duplication | public function unassignRole($contentId, $roleId) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * {@inheritdoc} |
||
| 354 | */ |
||
| 355 | View Code Duplication | public function removeRoleAssignment($roleAssignmentId) |
|
| 365 | } |
||
| 366 |
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.