Total Complexity | 72 |
Total Lines | 357 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Group 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.
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 Group, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
45 | class Group implements IGroup { |
||
46 | /** @var null|string */ |
||
47 | protected $displayName; |
||
48 | |||
49 | /** @var string */ |
||
50 | private $gid; |
||
51 | |||
52 | /** @var \OC\User\User[] */ |
||
53 | private $users = array(); |
||
54 | |||
55 | /** @var bool */ |
||
56 | private $usersLoaded; |
||
57 | |||
58 | /** @var Backend[] */ |
||
59 | private $backends; |
||
60 | /** @var EventDispatcherInterface */ |
||
61 | private $dispatcher; |
||
62 | /** @var \OC\User\Manager|IUserManager */ |
||
63 | private $userManager; |
||
64 | /** @var PublicEmitter */ |
||
65 | private $emitter; |
||
66 | |||
67 | |||
68 | /** |
||
69 | * @param string $gid |
||
70 | * @param Backend[] $backends |
||
71 | * @param EventDispatcherInterface $dispatcher |
||
72 | * @param IUserManager $userManager |
||
73 | * @param PublicEmitter $emitter |
||
74 | * @param string $displayName |
||
75 | */ |
||
76 | public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) { |
||
77 | $this->gid = $gid; |
||
78 | $this->backends = $backends; |
||
79 | $this->dispatcher = $dispatcher; |
||
80 | $this->userManager = $userManager; |
||
81 | $this->emitter = $emitter; |
||
82 | $this->displayName = $displayName; |
||
83 | } |
||
84 | |||
85 | public function getGID() { |
||
86 | return $this->gid; |
||
87 | } |
||
88 | |||
89 | public function getDisplayName() { |
||
90 | if (is_null($this->displayName)) { |
||
91 | foreach ($this->backends as $backend) { |
||
92 | if ($backend instanceof IGetDisplayNameBackend) { |
||
93 | $displayName = $backend->getDisplayName($this->gid); |
||
94 | if (trim($displayName) !== '') { |
||
95 | $this->displayName = $displayName; |
||
96 | return $this->displayName; |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | return $this->gid; |
||
101 | } |
||
102 | return $this->displayName; |
||
103 | } |
||
104 | |||
105 | public function setDisplayName(string $displayName): bool { |
||
106 | $displayName = trim($displayName); |
||
107 | if ($displayName !== '') { |
||
108 | foreach ($this->backends as $backend) { |
||
109 | if (($backend instanceof ISetDisplayNameBackend) |
||
110 | && $backend->setDisplayName($this->gid, $displayName)) { |
||
111 | $this->displayName = $displayName; |
||
112 | return true; |
||
113 | } |
||
114 | } |
||
115 | } |
||
116 | return false; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * get all users in the group |
||
121 | * |
||
122 | * @return \OC\User\User[] |
||
123 | */ |
||
124 | public function getUsers() { |
||
125 | if ($this->usersLoaded) { |
||
126 | return $this->users; |
||
127 | } |
||
128 | |||
129 | $userIds = array(); |
||
130 | foreach ($this->backends as $backend) { |
||
131 | $diff = array_diff( |
||
132 | $backend->usersInGroup($this->gid), |
||
133 | $userIds |
||
134 | ); |
||
135 | if ($diff) { |
||
|
|||
136 | $userIds = array_merge($userIds, $diff); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | $this->users = $this->getVerifiedUsers($userIds); |
||
141 | $this->usersLoaded = true; |
||
142 | return $this->users; |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * check if a user is in the group |
||
147 | * |
||
148 | * @param IUser $user |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function inGroup(IUser $user) { |
||
152 | if (isset($this->users[$user->getUID()])) { |
||
153 | return true; |
||
154 | } |
||
155 | foreach ($this->backends as $backend) { |
||
156 | if ($backend->inGroup($user->getUID(), $this->gid)) { |
||
157 | $this->users[$user->getUID()] = $user; |
||
158 | return true; |
||
159 | } |
||
160 | } |
||
161 | return false; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * add a user to the group |
||
166 | * |
||
167 | * @param IUser $user |
||
168 | */ |
||
169 | public function addUser(IUser $user) { |
||
170 | if ($this->inGroup($user)) { |
||
171 | return; |
||
172 | } |
||
173 | |||
174 | $this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [ |
||
175 | 'user' => $user, |
||
176 | ])); |
||
177 | |||
178 | if ($this->emitter) { |
||
179 | $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
||
180 | } |
||
181 | foreach ($this->backends as $backend) { |
||
182 | if ($backend->implementsActions(\OC\Group\Backend::ADD_TO_GROUP)) { |
||
183 | $backend->addToGroup($user->getUID(), $this->gid); |
||
184 | if ($this->users) { |
||
185 | $this->users[$user->getUID()] = $user; |
||
186 | } |
||
187 | |||
188 | $this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [ |
||
189 | 'user' => $user, |
||
190 | ])); |
||
191 | |||
192 | if ($this->emitter) { |
||
193 | $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
||
194 | } |
||
195 | return; |
||
196 | } |
||
197 | } |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * remove a user from the group |
||
202 | * |
||
203 | * @param \OC\User\User $user |
||
204 | */ |
||
205 | public function removeUser($user) { |
||
206 | $result = false; |
||
207 | $this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [ |
||
208 | 'user' => $user, |
||
209 | ])); |
||
210 | if ($this->emitter) { |
||
211 | $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
||
212 | } |
||
213 | foreach ($this->backends as $backend) { |
||
214 | if ($backend->implementsActions(\OC\Group\Backend::REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
||
215 | $backend->removeFromGroup($user->getUID(), $this->gid); |
||
216 | $result = true; |
||
217 | } |
||
218 | } |
||
219 | if ($result) { |
||
220 | $this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [ |
||
221 | 'user' => $user, |
||
222 | ])); |
||
223 | if ($this->emitter) { |
||
224 | $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
||
225 | } |
||
226 | if ($this->users) { |
||
227 | foreach ($this->users as $index => $groupUser) { |
||
228 | if ($groupUser->getUID() === $user->getUID()) { |
||
229 | unset($this->users[$index]); |
||
230 | return; |
||
231 | } |
||
232 | } |
||
233 | } |
||
234 | } |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * search for users in the group by userid |
||
239 | * |
||
240 | * @param string $search |
||
241 | * @param int $limit |
||
242 | * @param int $offset |
||
243 | * @return \OC\User\User[] |
||
244 | */ |
||
245 | public function searchUsers($search, $limit = null, $offset = null) { |
||
246 | $users = array(); |
||
247 | foreach ($this->backends as $backend) { |
||
248 | $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
||
249 | $users += $this->getVerifiedUsers($userIds); |
||
250 | if (!is_null($limit) and $limit <= 0) { |
||
251 | return $users; |
||
252 | } |
||
253 | } |
||
254 | return $users; |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * returns the number of users matching the search string |
||
259 | * |
||
260 | * @param string $search |
||
261 | * @return int|bool |
||
262 | */ |
||
263 | public function count($search = '') { |
||
264 | $users = false; |
||
265 | foreach ($this->backends as $backend) { |
||
266 | if($backend->implementsActions(\OC\Group\Backend::COUNT_USERS)) { |
||
267 | if($users === false) { |
||
268 | //we could directly add to a bool variable, but this would |
||
269 | //be ugly |
||
270 | $users = 0; |
||
271 | } |
||
272 | $users += $backend->countUsersInGroup($this->gid, $search); |
||
273 | } |
||
274 | } |
||
275 | return $users; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * returns the number of disabled users |
||
280 | * |
||
281 | * @return int|bool |
||
282 | */ |
||
283 | public function countDisabled() { |
||
284 | $users = false; |
||
285 | foreach ($this->backends as $backend) { |
||
286 | if($backend instanceOf ICountDisabledInGroup) { |
||
287 | if($users === false) { |
||
288 | //we could directly add to a bool variable, but this would |
||
289 | //be ugly |
||
290 | $users = 0; |
||
291 | } |
||
292 | $users += $backend->countDisabledInGroup($this->gid); |
||
293 | } |
||
294 | } |
||
295 | return $users; |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * search for users in the group by displayname |
||
300 | * |
||
301 | * @param string $search |
||
302 | * @param int $limit |
||
303 | * @param int $offset |
||
304 | * @return \OC\User\User[] |
||
305 | */ |
||
306 | public function searchDisplayName($search, $limit = null, $offset = null) { |
||
307 | $users = array(); |
||
308 | foreach ($this->backends as $backend) { |
||
309 | $userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
||
310 | $users = $this->getVerifiedUsers($userIds); |
||
311 | if (!is_null($limit) and $limit <= 0) { |
||
312 | return array_values($users); |
||
313 | } |
||
314 | } |
||
315 | return array_values($users); |
||
316 | } |
||
317 | |||
318 | /** |
||
319 | * delete the group |
||
320 | * |
||
321 | * @return bool |
||
322 | */ |
||
323 | public function delete() { |
||
347 | } |
||
348 | |||
349 | /** |
||
350 | * returns all the Users from an array that really exists |
||
351 | * @param string[] $userIds an array containing user IDs |
||
352 | * @return \OC\User\User[] an Array with the userId as Key and \OC\User\User as value |
||
353 | */ |
||
354 | private function getVerifiedUsers($userIds) { |
||
355 | if (!is_array($userIds)) { |
||
356 | return array(); |
||
357 | } |
||
358 | $users = array(); |
||
359 | foreach ($userIds as $userId) { |
||
360 | $user = $this->userManager->get($userId); |
||
361 | if (!is_null($user)) { |
||
362 | $users[$userId] = $user; |
||
363 | } |
||
364 | } |
||
365 | return $users; |
||
366 | } |
||
367 | |||
368 | /** |
||
369 | * @return bool |
||
370 | * @since 14.0.0 |
||
371 | */ |
||
372 | public function canRemoveUser() { |
||
373 | foreach ($this->backends as $backend) { |
||
374 | if ($backend->implementsActions(GroupInterface::REMOVE_FROM_GOUP)) { |
||
375 | return true; |
||
376 | } |
||
377 | } |
||
378 | return false; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * @return bool |
||
383 | * @since 14.0.0 |
||
384 | */ |
||
385 | public function canAddUser() { |
||
392 | } |
||
393 | |||
394 | /** |
||
395 | * @return bool |
||
396 | * @since 16.0.0 |
||
397 | */ |
||
398 | public function hideFromCollaboration(): bool { |
||
402 | } |
||
403 | } |
||
404 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.