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 PermissionService 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 PermissionService, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class PermissionService { |
||
45 | |||
46 | /** @var BoardMapper */ |
||
47 | private $boardMapper; |
||
48 | /** @var AclMapper */ |
||
49 | private $aclMapper; |
||
50 | /** @var ILogger */ |
||
51 | private $logger; |
||
52 | /** @var IUserManager */ |
||
53 | private $userManager; |
||
54 | /** @var IGroupManager */ |
||
55 | private $groupManager; |
||
56 | /** @var IConfig */ |
||
57 | private $config; |
||
58 | /** @var IManager */ |
||
59 | private $shareManager; |
||
60 | /** @var string */ |
||
61 | private $userId; |
||
62 | /** @var array */ |
||
63 | private $users = []; |
||
64 | |||
65 | private $circlesEnabled = false; |
||
66 | |||
67 | public function __construct( |
||
89 | |||
90 | /** |
||
91 | * Get current user permissions for a board by id |
||
92 | * |
||
93 | * @param $boardId |
||
94 | * @return bool|array |
||
95 | */ |
||
96 | View Code Duplication | public function getPermissions($boardId) { |
|
107 | |||
108 | /** |
||
109 | * Get current user permissions for a board |
||
110 | * |
||
111 | * @param Board|Entity $board |
||
112 | * @return array|bool |
||
113 | * @internal param $boardId |
||
114 | */ |
||
115 | View Code Duplication | public function matchPermissions(Board $board) { |
|
126 | |||
127 | /** |
||
128 | * check permissions for replacing dark magic middleware |
||
129 | * |
||
130 | * @param $mapper IPermissionMapper|null null if $id is a boardId |
||
131 | * @param $id int unique identifier of the Entity |
||
132 | * @param $permission int |
||
133 | * @return bool |
||
134 | * @throws NoPermissionException |
||
135 | */ |
||
136 | public function checkPermission($mapper, $id, $permission, $userId = null) { |
||
163 | |||
164 | /** |
||
165 | * @param $boardId |
||
166 | * @return bool |
||
167 | */ |
||
168 | public function userIsBoardOwner($boardId, $userId = null) { |
||
180 | |||
181 | /** |
||
182 | * Check if permission matches the acl rules for current user and groups |
||
183 | * |
||
184 | * @param Acl[] $acls |
||
185 | * @param $permission |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function userCan(array $acls, $permission, $userId = null) { |
||
216 | |||
217 | /** |
||
218 | * Find a list of all users (including the ones from groups) |
||
219 | * Required to allow assigning them to cards |
||
220 | * |
||
221 | * @param $boardId |
||
222 | * @return array |
||
223 | */ |
||
224 | public function findUsers($boardId, $refresh = false) { |
||
225 | // cache users of a board so we don't query them for every cards |
||
226 | if (array_key_exists((string) $boardId, $this->users) && !$refresh) { |
||
227 | return $this->users[(string) $boardId]; |
||
228 | } |
||
229 | try { |
||
230 | $board = $this->boardMapper->find($boardId); |
||
231 | } catch (DoesNotExistException $e) { |
||
232 | return []; |
||
233 | } catch (MultipleObjectsReturnedException $e) { |
||
234 | return []; |
||
235 | } |
||
236 | |||
237 | $owner = $this->userManager->get($board->getOwner()); |
||
238 | if ($owner === null) { |
||
239 | $this->logger->info('No owner found for board ' . $board->getId()); |
||
240 | } else { |
||
241 | $users = []; |
||
242 | $users[$owner->getUID()] = new User($owner); |
||
243 | } |
||
244 | $acls = $this->aclMapper->findAll($boardId); |
||
245 | /** @var Acl $acl */ |
||
246 | foreach ($acls as $acl) { |
||
247 | View Code Duplication | if ($acl->getType() === Acl::PERMISSION_TYPE_USER) { |
|
248 | $user = $this->userManager->get($acl->getParticipant()); |
||
249 | if ($user === null) { |
||
250 | $this->logger->info('No user found for acl rule ' . $acl->getId()); |
||
251 | continue; |
||
252 | } |
||
253 | $users[$user->getUID()] = new User($user); |
||
254 | } |
||
255 | View Code Duplication | if ($acl->getType() === Acl::PERMISSION_TYPE_GROUP) { |
|
256 | $group = $this->groupManager->get($acl->getParticipant()); |
||
257 | if ($group === null) { |
||
258 | $this->logger->info('No group found for acl rule ' . $acl->getId()); |
||
259 | continue; |
||
260 | } |
||
261 | foreach ($group->getUsers() as $user) { |
||
262 | $users[$user->getUID()] = new User($user); |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | $this->users[(string) $boardId] = $users; |
||
267 | return $this->users[(string) $boardId]; |
||
268 | } |
||
269 | |||
270 | public function canCreate() { |
||
282 | |||
283 | View Code Duplication | private function getGroupLimitList() { |
|
291 | } |
||
292 |
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.