Conditions | 31 |
Paths | > 20000 |
Total Lines | 198 |
Lines | 39 |
Ratio | 19.7 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
101 | public static function getUsersSharingFile($path, $ownerUser, $includeOwner = false, $returnUserPaths = false, $recursive = true) { |
||
102 | // FIXME: make ths use IShareProvider::getSharesByPath and extract users |
||
103 | $userManager = \OC::$server->getUserManager(); |
||
104 | $userObject = $userManager->get($ownerUser); |
||
105 | |||
106 | View Code Duplication | if ($userObject === null) { |
|
|
|||
107 | $msg = "Backends provided no user object for $ownerUser"; |
||
108 | \OC::$server->getLogger()->error($msg, ['app' => __CLASS__]); |
||
109 | throw new \OC\User\NoUserException($msg); |
||
110 | } |
||
111 | |||
112 | $ownerUser = $userObject->getUID(); |
||
113 | |||
114 | Filesystem::initMountPoints($ownerUser); |
||
115 | $shares = $sharePaths = $fileTargets = []; |
||
116 | $publicShare = false; |
||
117 | $remoteShare = false; |
||
118 | $source = -1; |
||
119 | $cache = $mountPath = false; |
||
120 | |||
121 | $view = new \OC\Files\View('/' . $ownerUser . '/files'); |
||
122 | $meta = $view->getFileInfo($path); |
||
123 | if ($meta) { |
||
124 | $path = \substr($meta->getPath(), \strlen('/' . $ownerUser . '/files')); |
||
125 | } else { |
||
126 | // if the file doesn't exists yet we start with the parent folder |
||
127 | $meta = $view->getFileInfo(\dirname($path)); |
||
128 | } |
||
129 | |||
130 | if ($meta !== false) { |
||
131 | $source = $meta['fileid']; |
||
132 | $cache = new \OC\Files\Cache\Cache($meta['storage']); |
||
133 | |||
134 | $mountPath = $meta->getMountPoint()->getMountPoint(); |
||
135 | if ($mountPath !== false) { |
||
136 | $mountPath = \substr($mountPath, \strlen('/' . $ownerUser . '/files')); |
||
137 | } |
||
138 | } |
||
139 | |||
140 | $paths = []; |
||
141 | while ($source !== -1) { |
||
142 | // Fetch all shares with another user |
||
143 | if (!$returnUserPaths) { |
||
144 | $query = \OC_DB::prepare( |
||
145 | 'SELECT `share_with`, `file_source`, `file_target` |
||
146 | FROM |
||
147 | `*PREFIX*share` |
||
148 | WHERE |
||
149 | `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')' |
||
150 | ); |
||
151 | $result = $query->execute([$source, self::SHARE_TYPE_USER]); |
||
152 | } else { |
||
153 | $query = \OC_DB::prepare( |
||
154 | 'SELECT `share_with`, `file_source`, `file_target` |
||
155 | FROM |
||
156 | `*PREFIX*share` |
||
157 | WHERE |
||
158 | `item_source` = ? AND `share_type` IN (?, ?) AND `item_type` IN (\'file\', \'folder\')' |
||
159 | ); |
||
160 | $result = $query->execute([$source, self::SHARE_TYPE_USER, self::$shareTypeGroupUserUnique]); |
||
161 | } |
||
162 | |||
163 | if (\OCP\DB::isError($result)) { |
||
164 | \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
||
165 | } else { |
||
166 | while ($row = $result->fetchRow()) { |
||
167 | $shares[] = $row['share_with']; |
||
168 | if ($returnUserPaths) { |
||
169 | $fileTargets[(int) $row['file_source']][$row['share_with']] = $row; |
||
170 | } |
||
171 | } |
||
172 | } |
||
173 | |||
174 | // We also need to take group shares into account |
||
175 | $query = \OC_DB::prepare( |
||
176 | 'SELECT `share_with`, `file_source`, `file_target` |
||
177 | FROM |
||
178 | `*PREFIX*share` |
||
179 | WHERE |
||
180 | `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')' |
||
181 | ); |
||
182 | |||
183 | $result = $query->execute([$source, self::SHARE_TYPE_GROUP]); |
||
184 | |||
185 | if (\OCP\DB::isError($result)) { |
||
186 | \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
||
187 | } else { |
||
188 | while ($row = $result->fetchRow()) { |
||
189 | $usersInGroup = self::usersInGroup($row['share_with']); |
||
190 | $shares = \array_merge($shares, $usersInGroup); |
||
191 | if ($returnUserPaths) { |
||
192 | foreach ($usersInGroup as $user) { |
||
193 | if (!isset($fileTargets[(int) $row['file_source']][$user])) { |
||
194 | // When the user already has an entry for this file source |
||
195 | // the file is either shared directly with him as well, or |
||
196 | // he has an exception entry (because of naming conflict). |
||
197 | $fileTargets[(int) $row['file_source']][$user] = $row; |
||
198 | } |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | } |
||
203 | |||
204 | //check for public link shares |
||
205 | View Code Duplication | if (!$publicShare) { |
|
206 | $query = \OC_DB::prepare(' |
||
207 | SELECT `share_with` |
||
208 | FROM `*PREFIX*share` |
||
209 | WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')', 1 |
||
210 | ); |
||
211 | |||
212 | $result = $query->execute([$source, self::SHARE_TYPE_LINK]); |
||
213 | |||
214 | if (\OCP\DB::isError($result)) { |
||
215 | \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
||
216 | } else { |
||
217 | if ($result->fetchRow()) { |
||
218 | $publicShare = true; |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | |||
223 | //check for remote share |
||
224 | View Code Duplication | if (!$remoteShare) { |
|
225 | $query = \OC_DB::prepare(' |
||
226 | SELECT `share_with` |
||
227 | FROM `*PREFIX*share` |
||
228 | WHERE `item_source` = ? AND `share_type` = ? AND `item_type` IN (\'file\', \'folder\')', 1 |
||
229 | ); |
||
230 | |||
231 | $result = $query->execute([$source, self::SHARE_TYPE_REMOTE]); |
||
232 | |||
233 | if (\OCP\DB::isError($result)) { |
||
234 | \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
||
235 | } else { |
||
236 | if ($result->fetchRow()) { |
||
237 | $remoteShare = true; |
||
238 | } |
||
239 | } |
||
240 | } |
||
241 | |||
242 | // let's get the parent for the next round |
||
243 | $meta = $cache->get((int)$source); |
||
244 | if ($recursive === true && $meta !== false) { |
||
245 | $paths[$source] = $meta['path']; |
||
246 | $source = (int)$meta['parent']; |
||
247 | } else { |
||
248 | $source = -1; |
||
249 | } |
||
250 | } |
||
251 | |||
252 | // Include owner in list of users, if requested |
||
253 | if ($includeOwner) { |
||
254 | $shares[] = $ownerUser; |
||
255 | } |
||
256 | |||
257 | if ($returnUserPaths) { |
||
258 | $fileTargetIDs = \array_keys($fileTargets); |
||
259 | $fileTargetIDs = \array_unique($fileTargetIDs); |
||
260 | |||
261 | if (!empty($fileTargetIDs)) { |
||
262 | $query = \OC_DB::prepare( |
||
263 | 'SELECT `fileid`, `path` |
||
264 | FROM `*PREFIX*filecache` |
||
265 | WHERE `fileid` IN (' . \implode(',', $fileTargetIDs) . ')' |
||
266 | ); |
||
267 | $result = $query->execute(); |
||
268 | |||
269 | if (\OCP\DB::isError($result)) { |
||
270 | \OCP\Util::writeLog('OCP\Share', \OC_DB::getErrorMessage(), \OCP\Util::ERROR); |
||
271 | } else { |
||
272 | while ($row = $result->fetchRow()) { |
||
273 | foreach ($fileTargets[$row['fileid']] as $uid => $shareData) { |
||
274 | if ($mountPath !== false) { |
||
275 | $sharedPath = $shareData['file_target']; |
||
276 | $sharedPath .= \substr($path, \strlen($mountPath) + \strlen($paths[$row['fileid']])); |
||
277 | $sharePaths[$uid] = $sharedPath; |
||
278 | } else { |
||
279 | $sharedPath = $shareData['file_target']; |
||
280 | $sharedPath .= \substr($path, \strlen($row['path']) -5); |
||
281 | $sharePaths[$uid] = $sharedPath; |
||
282 | } |
||
283 | } |
||
284 | } |
||
285 | } |
||
286 | } |
||
287 | |||
288 | if ($includeOwner) { |
||
289 | $sharePaths[$ownerUser] = $path; |
||
290 | } else { |
||
291 | unset($sharePaths[$ownerUser]); |
||
292 | } |
||
293 | |||
294 | return $sharePaths; |
||
295 | } |
||
296 | |||
297 | return ['users' => \array_unique($shares), 'public' => $publicShare, 'remote' => $remoteShare]; |
||
298 | } |
||
299 | |||
513 |
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.