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 FilesHooks 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 FilesHooks, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class FilesHooks { |
||
44 | const USER_BATCH_SIZE = 50; |
||
45 | |||
46 | /** @var \OCP\Activity\IManager */ |
||
47 | protected $manager; |
||
48 | |||
49 | /** @var \OCA\Activity\Data */ |
||
50 | protected $activityData; |
||
51 | |||
52 | /** @var \OCA\Activity\UserSettings */ |
||
53 | protected $userSettings; |
||
54 | |||
55 | /** @var \OCP\IGroupManager */ |
||
56 | protected $groupManager; |
||
57 | |||
58 | /** @var \OCP\IDBConnection */ |
||
59 | protected $connection; |
||
60 | |||
61 | /** @var \OC\Files\View */ |
||
62 | protected $view; |
||
63 | |||
64 | /** @var IURLGenerator */ |
||
65 | protected $urlGenerator; |
||
66 | |||
67 | /** @var string|false */ |
||
68 | protected $currentUser; |
||
69 | |||
70 | /** |
||
71 | * Constructor |
||
72 | * |
||
73 | * @param IManager $manager |
||
74 | * @param Data $activityData |
||
75 | * @param UserSettings $userSettings |
||
76 | * @param IGroupManager $groupManager |
||
77 | * @param View $view |
||
78 | * @param IDBConnection $connection |
||
79 | * @param IURLGenerator $urlGenerator |
||
80 | * @param string|false $currentUser |
||
81 | */ |
||
82 | 49 | public function __construct(IManager $manager, Data $activityData, UserSettings $userSettings, IGroupManager $groupManager, View $view, IDBConnection $connection, IURLGenerator $urlGenerator, $currentUser) { |
|
92 | |||
93 | /** |
||
94 | * @return string|false Current UserID if logged in, false otherwise |
||
95 | */ |
||
96 | 2 | protected function getCurrentUser() { |
|
99 | |||
100 | /** |
||
101 | * Store the create hook events |
||
102 | * @param string $path Path of the file that has been created |
||
103 | */ |
||
104 | 2 | public function fileCreate($path) { |
|
105 | 2 | if ($this->getCurrentUser() !== false) { |
|
106 | 1 | $this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, 'created_self', 'created_by'); |
|
107 | } else { |
||
108 | 1 | $this->addNotificationsForFileAction($path, Files::TYPE_SHARE_CREATED, '', 'created_public'); |
|
109 | } |
||
110 | 2 | } |
|
111 | |||
112 | /** |
||
113 | * Store the update hook events |
||
114 | * @param string $path Path of the file that has been modified |
||
115 | */ |
||
116 | 1 | public function fileUpdate($path) { |
|
119 | |||
120 | /** |
||
121 | * Store the delete hook events |
||
122 | * @param string $path Path of the file that has been deleted |
||
123 | */ |
||
124 | 1 | public function fileDelete($path) { |
|
127 | |||
128 | /** |
||
129 | * Store the restore hook events |
||
130 | * @param string $path Path of the file that has been restored |
||
131 | */ |
||
132 | 1 | public function fileRestore($path) { |
|
135 | |||
136 | /** |
||
137 | * Creates the entries for file actions on $file_path |
||
138 | * |
||
139 | * @param string $filePath The file that is being changed |
||
140 | * @param int $activityType The activity type |
||
141 | * @param string $subject The subject for the actor |
||
142 | * @param string $subjectBy The subject for other users (with "by $actor") |
||
143 | */ |
||
144 | 3 | protected function addNotificationsForFileAction($filePath, $activityType, $subject, $subjectBy) { |
|
145 | // Do not add activities for .part-files |
||
146 | 3 | if (\substr($filePath, -5) === '.part') { |
|
147 | 1 | return; |
|
148 | } |
||
149 | |||
150 | 2 | list($filePath, $uidOwner, $fileId) = $this->getSourcePathAndOwner($filePath); |
|
151 | 2 | if (!$fileId) { |
|
152 | // no owner, possibly deleted or unknown |
||
153 | // skip notifications |
||
154 | return; |
||
155 | } |
||
156 | 2 | $affectedUsers = $this->getUserPathsFromPath($filePath, $uidOwner); |
|
157 | 2 | $filteredStreamUsers = $this->userSettings->filterUsersBySetting(\array_keys($affectedUsers), 'stream', $activityType); |
|
158 | 2 | $filteredEmailUsers = $this->userSettings->filterUsersBySetting(\array_keys($affectedUsers), 'email', $activityType); |
|
159 | |||
160 | 2 | foreach ($affectedUsers as $user => $path) { |
|
161 | 2 | if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) { |
|
162 | 2 | continue; |
|
163 | } |
||
164 | |||
165 | 2 | if ($user === $this->currentUser) { |
|
166 | 1 | $userSubject = $subject; |
|
167 | 1 | $userParams = [[$fileId => $path]]; |
|
168 | } else { |
||
169 | 1 | $userSubject = $subjectBy; |
|
170 | 1 | $userParams = [[$fileId => $path], $this->currentUser]; |
|
171 | } |
||
172 | |||
173 | 2 | $this->addNotificationsForUser( |
|
174 | 2 | $user, $userSubject, $userParams, |
|
175 | 2 | $fileId, $path, true, |
|
176 | 2 | !empty($filteredStreamUsers[$user]), |
|
177 | 2 | !empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0, |
|
178 | 2 | $activityType |
|
179 | ); |
||
180 | } |
||
181 | 2 | } |
|
182 | |||
183 | /** |
||
184 | * Returns a "username => path" map for all affected users |
||
185 | * |
||
186 | * @param string $path |
||
187 | * @param string $uidOwner |
||
188 | * @return array |
||
189 | */ |
||
190 | protected function getUserPathsFromPath($path, $uidOwner) { |
||
193 | |||
194 | /** |
||
195 | * Return the source |
||
196 | * |
||
197 | * @param string $path |
||
198 | * @return array |
||
199 | */ |
||
200 | protected function getSourcePathAndOwner($path) { |
||
224 | |||
225 | /** |
||
226 | * Manage sharing events |
||
227 | * @param array $params The hook params |
||
228 | */ |
||
229 | 3 | View Code Duplication | public function share($params) { |
240 | |||
241 | /** |
||
242 | * Manage sharing events |
||
243 | * @param array $params The hook params |
||
244 | */ |
||
245 | View Code Duplication | public function unShare($params) { |
|
256 | |||
257 | /** |
||
258 | * Sharing a file or folder with a user |
||
259 | * |
||
260 | * @param string $shareWith |
||
261 | * @param int $fileSource File ID that is being shared |
||
262 | * @param string $itemType File type that is being shared (file or folder) |
||
263 | * @param string $fileTarget File path |
||
264 | * @param bool $isSharing True if sharing, false if unsharing |
||
265 | */ |
||
266 | 2 | protected function shareFileOrFolderWithUser($shareWith, $fileSource, $itemType, $fileTarget, $isSharing) { |
|
289 | |||
290 | /** |
||
291 | * Sharing a file or folder with a group |
||
292 | * |
||
293 | * @param string $shareWith |
||
294 | * @param int $fileSource File ID that is being shared |
||
295 | * @param string $itemType File type that is being shared (file or folder) |
||
296 | * @param string $fileTarget File path |
||
297 | * @param int $shareId The Share ID of this share |
||
298 | * @param bool $isSharing True if sharing, false if unsharing |
||
299 | */ |
||
300 | 6 | protected function shareFileOrFolderWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId, $isSharing) { |
|
329 | |||
330 | /** |
||
331 | * @param IUser[] $usersInGroup |
||
332 | * @param string $actionUser |
||
333 | * @param int $fileSource File ID that is being shared |
||
334 | * @param string $itemType File type that is being shared (file or folder) |
||
335 | * @param string $fileTarget File path |
||
336 | * @param int $shareId The Share ID of this share |
||
337 | */ |
||
338 | 4 | protected function addNotificationsForGroupUsers(array $usersInGroup, $actionUser, $fileSource, $itemType, $fileTarget, $shareId) { |
|
370 | |||
371 | /** |
||
372 | * Check when there was a naming conflict and the target is different |
||
373 | * for some of the users |
||
374 | * |
||
375 | * @param array $affectedUsers |
||
376 | * @param int $shareId |
||
377 | * @return mixed |
||
378 | */ |
||
379 | protected function fixPathsForShareExceptions(array $affectedUsers, $shareId) { |
||
393 | |||
394 | /** |
||
395 | * Sharing a file or folder via link/public |
||
396 | * |
||
397 | * @param int $fileSource File ID that is being shared |
||
398 | * @param string $itemType File type that is being shared (file or folder) |
||
399 | * @param string $linkOwner |
||
400 | * @param bool $isSharing True if sharing, false if unsharing |
||
401 | */ |
||
402 | 2 | protected function shareFileOrFolderByLink($fileSource, $itemType, $linkOwner, $isSharing) { |
|
434 | |||
435 | /** |
||
436 | * Add notifications for the user that shares a file/folder |
||
437 | * |
||
438 | * @param string $subject |
||
439 | * @param string $shareWith |
||
440 | * @param int $fileSource |
||
441 | * @param string $itemType |
||
442 | */ |
||
443 | 2 | protected function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) { |
|
459 | |||
460 | /** |
||
461 | * Add notifications for the user that shares a file/folder |
||
462 | * |
||
463 | * @param string $owner |
||
464 | * @param string $subject |
||
465 | * @param string $shareWith |
||
466 | * @param int $fileSource |
||
467 | * @param string $itemType |
||
468 | */ |
||
469 | 2 | protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType) { |
|
485 | |||
486 | /** |
||
487 | * Add notifications for the owners whose files have been reshared |
||
488 | * |
||
489 | * @param string $currentOwner |
||
490 | * @param string $subject |
||
491 | * @param string $shareWith |
||
492 | * @param int $fileSource |
||
493 | * @param string $itemType |
||
494 | */ |
||
495 | 10 | protected function shareNotificationForOriginalOwners($currentOwner, $subject, $shareWith, $fileSource, $itemType) { |
|
535 | |||
536 | /** |
||
537 | * Adds the activity and email for a user when the settings require it |
||
538 | * |
||
539 | * @param string $user |
||
540 | * @param string $subject |
||
541 | * @param array $subjectParams |
||
542 | * @param int $fileId |
||
543 | * @param string $path |
||
544 | * @param bool $isFile If the item is a file, we link to the parent directory |
||
545 | * @param bool $streamSetting |
||
546 | * @param int $emailSetting |
||
547 | * @param string $type |
||
548 | */ |
||
549 | 11 | protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) { |
|
583 | } |
||
584 |
This checks looks for assignemnts to variables using the
list(...)
function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$a
and$c
are used. There was no need to assign$b
.Instead, the list call could have been.