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 UserManager 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 UserManager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class UserManager extends RepositoryExecutor |
||
| 12 | { |
||
| 13 | protected $supportedStepTypes = array('user'); |
||
| 14 | |||
| 15 | protected $userMatcher; |
||
| 16 | |||
| 17 | 27 | public function __construct(UserMatcher $userMatcher) |
|
| 18 | { |
||
| 19 | 27 | $this->userMatcher = $userMatcher; |
|
| 20 | 27 | } |
|
| 21 | |||
| 22 | /** |
||
| 23 | * Creates a user based on the DSL instructions. |
||
| 24 | * |
||
| 25 | * @todo allow setting extra profile attributes! |
||
| 26 | */ |
||
| 27 | 2 | protected function create() |
|
| 28 | { |
||
| 29 | 2 | if (!isset($this->dsl['groups'])) { |
|
| 30 | throw new \Exception('No user groups set to create user in.'); |
||
| 31 | } |
||
| 32 | |||
| 33 | 2 | View Code Duplication | if (!is_array($this->dsl['groups'])) { |
|
|
|||
| 34 | $this->dsl['groups'] = array($this->dsl['groups']); |
||
| 35 | } |
||
| 36 | |||
| 37 | 2 | $userService = $this->repository->getUserService(); |
|
| 38 | 2 | $contentTypeService = $this->repository->getContentTypeService(); |
|
| 39 | |||
| 40 | 2 | $userGroups = array(); |
|
| 41 | 2 | foreach ($this->dsl['groups'] as $groupId) { |
|
| 42 | 2 | $groupId = $this->referenceResolver->resolveReference($groupId); |
|
| 43 | 2 | $userGroup = $userService->loadUserGroup($groupId); |
|
| 44 | |||
| 45 | // q: in which case can we have no group? And should we throw an exception? |
||
| 46 | //if ($userGroup) { |
||
| 47 | 2 | $userGroups[] = $userGroup; |
|
| 48 | //} |
||
| 49 | 2 | } |
|
| 50 | |||
| 51 | // FIXME: Hard coding content type to user for now |
||
| 52 | 2 | $userContentType = $contentTypeService->loadContentTypeByIdentifier(self::USER_CONTENT_TYPE); |
|
| 53 | |||
| 54 | 2 | $userCreateStruct = $userService->newUserCreateStruct( |
|
| 55 | 2 | $this->dsl['username'], |
|
| 56 | 2 | $this->dsl['email'], |
|
| 57 | 2 | $this->dsl['password'], |
|
| 58 | 2 | $this->getLanguageCode(), |
|
| 59 | $userContentType |
||
| 60 | 2 | ); |
|
| 61 | 2 | $userCreateStruct->setField('first_name', $this->dsl['first_name']); |
|
| 62 | 2 | $userCreateStruct->setField('last_name', $this->dsl['last_name']); |
|
| 63 | |||
| 64 | // Create the user |
||
| 65 | 2 | $user = $userService->createUser($userCreateStruct, $userGroups); |
|
| 66 | |||
| 67 | 2 | $this->setReferences($user); |
|
| 68 | |||
| 69 | 2 | return $user; |
|
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Method to handle the update operation of the migration instructions |
||
| 74 | * |
||
| 75 | * @todo allow setting extra profile attributes! |
||
| 76 | */ |
||
| 77 | 2 | protected function update() |
|
| 145 | |||
| 146 | /** |
||
| 147 | * Method to handle the delete operation of the migration instructions |
||
| 148 | */ |
||
| 149 | 2 | protected function delete() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $action |
||
| 164 | * @return RoleCollection |
||
| 165 | * @throws \Exception |
||
| 166 | */ |
||
| 167 | 2 | protected function matchUsers($action) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * Sets references to object attributes. |
||
| 209 | * |
||
| 210 | * The User manager currently only supports setting references to user_id. |
||
| 211 | * |
||
| 212 | * @param \eZ\Publish\API\Repository\Values\User\User|UserCollection $user |
||
| 213 | * @throws \InvalidArgumentException when trying to set references to unsupported attributes |
||
| 214 | * @return boolean |
||
| 215 | */ |
||
| 216 | 2 | protected function setReferences($user) |
|
| 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.