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 |
||
44 | class FilesHooks { |
||
45 | const USER_BATCH_SIZE = 50; |
||
46 | |||
47 | /** @var \OCP\Activity\IManager */ |
||
48 | protected $manager; |
||
49 | |||
50 | /** @var \OCA\Activity\Data */ |
||
51 | protected $activityData; |
||
52 | |||
53 | /** @var \OCA\Activity\UserSettings */ |
||
54 | protected $userSettings; |
||
55 | |||
56 | /** @var \OCP\IGroupManager */ |
||
57 | protected $groupManager; |
||
58 | |||
59 | /** @var \OCP\IDBConnection */ |
||
60 | protected $connection; |
||
61 | |||
62 | /** @var \OC\Files\View */ |
||
63 | protected $view; |
||
64 | |||
65 | /** @var IURLGenerator */ |
||
66 | protected $urlGenerator; |
||
67 | |||
68 | /** @var string|false */ |
||
69 | protected $currentUser; |
||
70 | |||
71 | /** |
||
72 | * Constructor |
||
73 | * |
||
74 | * @param IManager $manager |
||
75 | * @param Data $activityData |
||
76 | * @param UserSettings $userSettings |
||
77 | * @param IGroupManager $groupManager |
||
78 | * @param View $view |
||
79 | * @param IDBConnection $connection |
||
80 | * @param IURLGenerator $urlGenerator |
||
81 | * @param string|false $currentUser |
||
82 | */ |
||
83 | 49 | public function __construct(IManager $manager, Data $activityData, UserSettings $userSettings, IGroupManager $groupManager, View $view, IDBConnection $connection, IURLGenerator $urlGenerator, $currentUser) { |
|
84 | 49 | $this->manager = $manager; |
|
85 | 49 | $this->activityData = $activityData; |
|
86 | 49 | $this->userSettings = $userSettings; |
|
87 | 49 | $this->groupManager = $groupManager; |
|
88 | 49 | $this->view = $view; |
|
89 | 49 | $this->connection = $connection; |
|
90 | 49 | $this->urlGenerator = $urlGenerator; |
|
91 | 49 | $this->currentUser = $currentUser; |
|
92 | 49 | } |
|
93 | |||
94 | /** |
||
95 | * @return string|false Current UserID if logged in, false otherwise |
||
96 | */ |
||
97 | 2 | protected function getCurrentUser() { |
|
100 | |||
101 | /** |
||
102 | * Store the create hook events |
||
103 | * @param string $path Path of the file that has been created |
||
104 | */ |
||
105 | 2 | public function fileCreate($path) { |
|
112 | |||
113 | /** |
||
114 | * Store the update hook events |
||
115 | * @param string $path Path of the file that has been modified |
||
116 | */ |
||
117 | 1 | public function fileUpdate($path) { |
|
120 | |||
121 | /** |
||
122 | * Store the delete hook events |
||
123 | * @param string $path Path of the file that has been deleted |
||
124 | */ |
||
125 | 1 | public function fileDelete($path) { |
|
128 | |||
129 | /** |
||
130 | * Store the restore hook events |
||
131 | * @param string $path Path of the file that has been restored |
||
132 | */ |
||
133 | 1 | public function fileRestore($path) { |
|
136 | |||
137 | /** |
||
138 | * Creates the entries for file actions on $file_path |
||
139 | * |
||
140 | * @param string $filePath The file that is being changed |
||
141 | * @param int $activityType The activity type |
||
142 | * @param string $subject The subject for the actor |
||
143 | * @param string $subjectBy The subject for other users (with "by $actor") |
||
144 | */ |
||
145 | 3 | protected function addNotificationsForFileAction($filePath, $activityType, $subject, $subjectBy) { |
|
146 | // Do not add activities for .part-files |
||
147 | 3 | if (substr($filePath, -5) === '.part') { |
|
148 | 1 | return; |
|
149 | } |
||
150 | |||
151 | 2 | if (Files::TYPE_SHARE_CREATED) { |
|
152 | try { |
||
153 | 2 | list($filePath, $uidOwner, $fileId) = $this->getSourcePathAndOwner($filePath); |
|
154 | } catch (\OCP\Files\NotFoundException $e) { |
||
|
|||
155 | // File not found? Sounds weird, but this happens before 9.1: |
||
156 | // https://github.com/owncloud/core/issues/23212 |
||
157 | // Chunk assembling triggered the exact same hooks twice. |
||
158 | // The first call however is before the file is in the database. |
||
159 | // So when trying to get the owner, the file can not be found. |
||
160 | // But since the second hook will come along, we simply ignore this. |
||
161 | 2 | return; |
|
162 | } |
||
163 | } else { |
||
164 | list($filePath, $uidOwner, $fileId) = $this->getSourcePathAndOwner($filePath); |
||
165 | } |
||
166 | |||
167 | 2 | if ($fileId === 0) { |
|
168 | // Could not find the file for the owner ... |
||
169 | return; |
||
170 | } |
||
171 | |||
172 | 2 | $affectedUsers = $this->getUserPathsFromPath($filePath, $uidOwner); |
|
173 | 2 | $filteredStreamUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'stream', $activityType); |
|
174 | 2 | $filteredEmailUsers = $this->userSettings->filterUsersBySetting(array_keys($affectedUsers), 'email', $activityType); |
|
175 | |||
176 | 2 | foreach ($affectedUsers as $user => $path) { |
|
177 | 2 | if (empty($filteredStreamUsers[$user]) && empty($filteredEmailUsers[$user])) { |
|
178 | 2 | continue; |
|
179 | } |
||
180 | |||
181 | 2 | if ($user === $this->currentUser) { |
|
182 | 1 | $userSubject = $subject; |
|
183 | 1 | $userParams = [[$fileId => $path]]; |
|
184 | } else { |
||
185 | 1 | $userSubject = $subjectBy; |
|
186 | 1 | $userParams = [[$fileId => $path], $this->currentUser]; |
|
187 | } |
||
188 | |||
189 | 2 | $this->addNotificationsForUser( |
|
190 | $user, $userSubject, $userParams, |
||
191 | 2 | $fileId, $path, true, |
|
192 | 2 | !empty($filteredStreamUsers[$user]), |
|
193 | 2 | !empty($filteredEmailUsers[$user]) ? $filteredEmailUsers[$user] : 0, |
|
194 | $activityType |
||
195 | ); |
||
196 | } |
||
197 | 2 | } |
|
198 | |||
199 | /** |
||
200 | * Returns a "username => path" map for all affected users |
||
201 | * |
||
202 | * @param string $path |
||
203 | * @param string $uidOwner |
||
204 | * @return array |
||
205 | */ |
||
206 | protected function getUserPathsFromPath($path, $uidOwner) { |
||
209 | |||
210 | /** |
||
211 | * Return the source |
||
212 | * |
||
213 | * @param string $path |
||
214 | * @return array |
||
215 | */ |
||
216 | protected function getSourcePathAndOwner($path) { |
||
242 | |||
243 | /** |
||
244 | * Manage sharing events |
||
245 | * @param array $params The hook params |
||
246 | */ |
||
247 | 3 | View Code Duplication | public function share($params) { |
258 | |||
259 | /** |
||
260 | * Manage sharing events |
||
261 | * @param array $params The hook params |
||
262 | */ |
||
263 | View Code Duplication | public function unShare($params) { |
|
274 | |||
275 | /** |
||
276 | * Sharing a file or folder with a user |
||
277 | * |
||
278 | * @param string $shareWith |
||
279 | * @param int $fileSource File ID that is being shared |
||
280 | * @param string $itemType File type that is being shared (file or folder) |
||
281 | * @param string $fileTarget File path |
||
282 | * @param bool $isSharing True if sharing, false if unsharing |
||
283 | */ |
||
284 | 2 | protected function shareFileOrFolderWithUser($shareWith, $fileSource, $itemType, $fileTarget, $isSharing) { |
|
307 | |||
308 | /** |
||
309 | * Sharing a file or folder with a group |
||
310 | * |
||
311 | * @param string $shareWith |
||
312 | * @param int $fileSource File ID that is being shared |
||
313 | * @param string $itemType File type that is being shared (file or folder) |
||
314 | * @param string $fileTarget File path |
||
315 | * @param int $shareId The Share ID of this share |
||
316 | * @param bool $isSharing True if sharing, false if unsharing |
||
317 | */ |
||
318 | 6 | protected function shareFileOrFolderWithGroup($shareWith, $fileSource, $itemType, $fileTarget, $shareId, $isSharing) { |
|
347 | |||
348 | /** |
||
349 | * @param IUser[] $usersInGroup |
||
350 | * @param string $actionUser |
||
351 | * @param int $fileSource File ID that is being shared |
||
352 | * @param string $itemType File type that is being shared (file or folder) |
||
353 | * @param string $fileTarget File path |
||
354 | * @param int $shareId The Share ID of this share |
||
355 | */ |
||
356 | 4 | protected function addNotificationsForGroupUsers(array $usersInGroup, $actionUser, $fileSource, $itemType, $fileTarget, $shareId) { |
|
388 | |||
389 | /** |
||
390 | * Check when there was a naming conflict and the target is different |
||
391 | * for some of the users |
||
392 | * |
||
393 | * @param array $affectedUsers |
||
394 | * @param int $shareId |
||
395 | * @return mixed |
||
396 | */ |
||
397 | protected function fixPathsForShareExceptions(array $affectedUsers, $shareId) { |
||
411 | |||
412 | /** |
||
413 | * Sharing a file or folder via link/public |
||
414 | * |
||
415 | * @param int $fileSource File ID that is being shared |
||
416 | * @param string $itemType File type that is being shared (file or folder) |
||
417 | * @param string $linkOwner |
||
418 | * @param bool $isSharing True if sharing, false if unsharing |
||
419 | */ |
||
420 | 2 | protected function shareFileOrFolderByLink($fileSource, $itemType, $linkOwner, $isSharing) { |
|
452 | |||
453 | /** |
||
454 | * Add notifications for the user that shares a file/folder |
||
455 | * |
||
456 | * @param string $subject |
||
457 | * @param string $shareWith |
||
458 | * @param int $fileSource |
||
459 | * @param string $itemType |
||
460 | */ |
||
461 | 2 | protected function shareNotificationForSharer($subject, $shareWith, $fileSource, $itemType) { |
|
477 | |||
478 | /** |
||
479 | * Add notifications for the user that shares a file/folder |
||
480 | * |
||
481 | * @param string $owner |
||
482 | * @param string $subject |
||
483 | * @param string $shareWith |
||
484 | * @param int $fileSource |
||
485 | * @param string $itemType |
||
486 | */ |
||
487 | 2 | protected function reshareNotificationForSharer($owner, $subject, $shareWith, $fileSource, $itemType) { |
|
503 | |||
504 | /** |
||
505 | * Add notifications for the owners whose files have been reshared |
||
506 | * |
||
507 | * @param string $currentOwner |
||
508 | * @param string $subject |
||
509 | * @param string $shareWith |
||
510 | * @param int $fileSource |
||
511 | * @param string $itemType |
||
512 | */ |
||
513 | 10 | protected function shareNotificationForOriginalOwners($currentOwner, $subject, $shareWith, $fileSource, $itemType) { |
|
553 | |||
554 | /** |
||
555 | * Adds the activity and email for a user when the settings require it |
||
556 | * |
||
557 | * @param string $user |
||
558 | * @param string $subject |
||
559 | * @param array $subjectParams |
||
560 | * @param int $fileId |
||
561 | * @param string $path |
||
562 | * @param bool $isFile If the item is a file, we link to the parent directory |
||
563 | * @param bool $streamSetting |
||
564 | * @param int $emailSetting |
||
565 | * @param string $type |
||
566 | */ |
||
567 | 11 | protected function addNotificationsForUser($user, $subject, $subjectParams, $fileId, $path, $isFile, $streamSetting, $emailSetting, $type = Files_Sharing::TYPE_SHARED) { |
|
601 | } |
||
602 |
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.