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 |
||
| 35 | class UserManager implements LoggerAwareInterface, CreatorInterface, UpdaterInterface, RemoverInterface, FinderInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * @var Repository |
||
| 39 | */ |
||
| 40 | private $repository; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var LoggerInterface |
||
| 44 | */ |
||
| 45 | private $logger; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var UserService |
||
| 49 | */ |
||
| 50 | private $userService; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var UserGroupManager |
||
| 54 | */ |
||
| 55 | private $userGroupManager; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param Repository $repository |
||
| 59 | * @param UserGroupManager $userGroupManager |
||
| 60 | */ |
||
| 61 | 3 | public function __construct(Repository $repository, UserGroupManager $userGroupManager) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritdoc} |
||
| 70 | */ |
||
| 71 | 19 | public function setLogger(LoggerInterface $logger) |
|
| 75 | |||
| 76 | /** |
||
| 77 | * Finds user object by username. |
||
| 78 | * |
||
| 79 | * @param ValueObject $object |
||
| 80 | * @param bool $throwException |
||
| 81 | * |
||
| 82 | * @return User|false |
||
| 83 | * |
||
| 84 | * @throws NotFoundException |
||
| 85 | */ |
||
| 86 | 7 | View Code Duplication | public function find(ValueObject $object, $throwException = false) |
|
|
|||
| 87 | { |
||
| 88 | try { |
||
| 89 | 7 | if (isset($object->data['username'])) { |
|
| 90 | 7 | $user = $this->userService->loadUserByLogin($object->data['username']); |
|
| 91 | 6 | } |
|
| 92 | 7 | } catch (NotFoundException $notFoundException) { |
|
| 93 | 2 | $exception = $notFoundException; |
|
| 94 | } |
||
| 95 | |||
| 96 | 7 | if (!isset($user)) { |
|
| 97 | 2 | if (isset($exception) && $throwException) { |
|
| 98 | 1 | throw $exception; |
|
| 99 | } |
||
| 100 | |||
| 101 | 2 | return false; |
|
| 102 | } |
||
| 103 | |||
| 104 | 6 | return $user; |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * {@inheritdoc} |
||
| 109 | */ |
||
| 110 | 7 | public function create(ObjectInterface $object) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | */ |
||
| 142 | 7 | public function update(ObjectInterface $object) |
|
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | */ |
||
| 169 | 7 | public function createOrUpdate(ObjectInterface $object) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * {@inheritdoc} |
||
| 184 | */ |
||
| 185 | 2 | View Code Duplication | public function remove(ObjectInterface $object) |
| 199 | |||
| 200 | /** |
||
| 201 | * Assigns a collection of Transfer user groups from an eZ user, and returns the once who were added. |
||
| 202 | * |
||
| 203 | * @param User $user |
||
| 204 | * @param UserGroupObject[] $userGroupObjects |
||
| 205 | * |
||
| 206 | * @return UserGroup[] |
||
| 207 | */ |
||
| 208 | 6 | protected function assignUserToUserGroups(User $user, array $userGroupObjects) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Unassigns a collection of eZ UserGroups from an eZ User. |
||
| 231 | * |
||
| 232 | * @param User $user |
||
| 233 | * @param UserGroup[] $userGroups |
||
| 234 | */ |
||
| 235 | 6 | protected function unassignUserFromUserGroups(User $user, array $userGroups) |
|
| 244 | } |
||
| 245 |
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.