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) { |
|
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) { |
|
177 | |||
178 | /** |
||
179 | * Returns a "username => path" map for all affected users |
||
180 | * |
||
181 | * @param string $path |
||
182 | * @param string $uidOwner |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function getUserPathsFromPath($path, $uidOwner) { |
||
188 | |||
189 | /** |
||
190 | * Return the source |
||
191 | * |
||
192 | * @param string $path |
||
193 | * @return array |
||
194 | */ |
||
195 | protected function getSourcePathAndOwner($path) { |
||
211 | |||
212 | /** |
||
213 | * Manage sharing events |
||
214 | * @param array $params The hook params |
||
215 | */ |
||
216 | 3 | View Code Duplication | public function share($params) { |
227 | |||
228 | /** |
||
229 | * Manage sharing events |
||
230 | * @param array $params The hook params |
||
231 | */ |
||
232 | View Code Duplication | public function unShare($params) { |
|
243 | |||
244 | /** |
||
245 | * Sharing a file or folder with a user |
||
246 | * |
||
247 | * @param string $shareWith |
||
248 | * @param int $fileSource File ID that is being shared |
||
249 | * @param string $itemType File type that is being shared (file or folder) |
||
250 | * @param string $fileTarget File path |
||
251 | * @param bool $isSharing True if sharing, false if unsharing |
||
252 | */ |
||
253 | 2 | protected function shareFileOrFolderWithUser($shareWith, $fileSource, $itemType, $fileTarget, $isSharing) { |
|
276 | |||
277 | /** |
||
278 | * Sharing a file or folder with a group |
||
279 | * |
||
280 | * @param string $shareWith |
||
281 | * @param int $fileSource File ID that is being shared |
||
282 | * @param string $itemType File type that is being shared (file or folder) |
||
283 | * @param string $fileTarget File path |
||
284 | * @param int $shareId The Share ID of this share |
||
285 | * @param bool $isSharing True if sharing, false if unsharing |
||
286 | */ |
||
287 | 6 | protected function shareFileOrFolderWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId, $isSharing) { |
|
316 | |||
317 | /** |
||
318 | * @param IUser[] $usersInGroup |
||
319 | * @param string $actionUser |
||
320 | * @param int $fileSource File ID that is being shared |
||
321 | * @param string $itemType File type that is being shared (file or folder) |
||
322 | * @param string $fileTarget File path |
||
323 | * @param int $shareId The Share ID of this share |
||
324 | */ |
||
325 | 4 | protected function addNotificationsForGroupUsers(array $usersInGroup, $actionUser, $fileSource, $itemType, $fileTarget, $shareId) { |
|
357 | |||
358 | /** |
||
359 | * Check when there was a naming conflict and the target is different |
||
360 | * for some of the users |
||
361 | * |
||
362 | * @param array $affectedUsers |
||
363 | * @param int $shareId |
||
364 | * @return mixed |
||
365 | */ |
||
366 | protected function fixPathsForShareExceptions(array $affectedUsers, $shareId) { |
||
380 | |||
381 | /** |
||
382 | * Sharing a file or folder via link/public |
||
383 | * |
||
384 | * @param int $fileSource File ID that is being shared |
||
385 | * @param string $itemType File type that is being shared (file or folder) |
||
386 | * @param string $linkOwner |
||
387 | * @param bool $isSharing True if sharing, false if unsharing |
||
388 | */ |
||
389 | 2 | protected function shareFileOrFolderByLink($fileSource, $itemType, $linkOwner, $isSharing) { |
|
421 | |||
422 | /** |
||
423 | * Add notifications for the user that shares a file/folder |
||
424 | * |
||
425 | * @param string $subject |
||
426 | * @param string $shareWith |
||
427 | * @param int $fileSource |
||
428 | * @param string $itemType |
||
429 | */ |
||
430 | 2 | protected function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) { |
|
446 | |||
447 | /** |
||
448 | * Add notifications for the user that shares a file/folder |
||
449 | * |
||
450 | * @param string $owner |
||
451 | * @param string $subject |
||
452 | * @param string $shareWith |
||
453 | * @param int $fileSource |
||
454 | * @param string $itemType |
||
455 | */ |
||
456 | 2 | protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType) { |
|
472 | |||
473 | /** |
||
474 | * Add notifications for the owners whose files have been reshared |
||
475 | * |
||
476 | * @param string $currentOwner |
||
477 | * @param string $subject |
||
478 | * @param string $shareWith |
||
479 | * @param int $fileSource |
||
480 | * @param string $itemType |
||
481 | */ |
||
482 | 10 | protected function shareNotificationForOriginalOwners($currentOwner, $subject, $shareWith, $fileSource, $itemType) { |
|
522 | |||
523 | /** |
||
524 | * Adds the activity and email for a user when the settings require it |
||
525 | * |
||
526 | * @param string $user |
||
527 | * @param string $subject |
||
528 | * @param array $subjectParams |
||
529 | * @param int $fileId |
||
530 | * @param string $path |
||
531 | * @param bool $isFile If the item is a file, we link to the parent directory |
||
532 | * @param bool $streamSetting |
||
533 | * @param int $emailSetting |
||
534 | * @param string $type |
||
535 | */ |
||
536 | 11 | protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) { |
|
570 | } |
||
571 |
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.