Total Complexity | 59 |
Total Lines | 369 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Node 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 Node, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
48 | abstract class Node implements \Sabre\DAV\INode { |
||
49 | |||
50 | /** |
||
51 | * @var \OC\Files\View |
||
52 | */ |
||
53 | protected $fileView; |
||
54 | |||
55 | /** |
||
56 | * The path to the current node |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $path; |
||
61 | |||
62 | /** |
||
63 | * node properties cache |
||
64 | * |
||
65 | * @var array |
||
66 | */ |
||
67 | protected $property_cache = null; |
||
68 | |||
69 | /** |
||
70 | * @var \OCP\Files\FileInfo |
||
71 | */ |
||
72 | protected $info; |
||
73 | |||
74 | /** |
||
75 | * @var IManager |
||
76 | */ |
||
77 | protected $shareManager; |
||
78 | |||
79 | /** |
||
80 | * Sets up the node, expects a full path name |
||
81 | * |
||
82 | * @param \OC\Files\View $view |
||
83 | * @param \OCP\Files\FileInfo $info |
||
84 | * @param IManager $shareManager |
||
85 | */ |
||
86 | public function __construct(View $view, FileInfo $info, IManager $shareManager = null) { |
||
87 | $this->fileView = $view; |
||
88 | $this->path = $this->fileView->getRelativePath($info->getPath()); |
||
89 | $this->info = $info; |
||
90 | if ($shareManager) { |
||
91 | $this->shareManager = $shareManager; |
||
92 | } else { |
||
93 | $this->shareManager = \OC::$server->getShareManager(); |
||
94 | } |
||
95 | } |
||
96 | |||
97 | protected function refreshInfo() { |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * Returns the name of the node |
||
103 | * |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getName() { |
||
107 | return $this->info->getName(); |
||
108 | } |
||
109 | |||
110 | /** |
||
111 | * Returns the full path |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | public function getPath() { |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Renames the node |
||
121 | * |
||
122 | * @param string $name The new name |
||
123 | * @throws \Sabre\DAV\Exception\BadRequest |
||
124 | * @throws \Sabre\DAV\Exception\Forbidden |
||
125 | */ |
||
126 | public function setName($name) { |
||
127 | |||
128 | // rename is only allowed if the update privilege is granted |
||
129 | if (!$this->info->isUpdateable()) { |
||
130 | throw new \Sabre\DAV\Exception\Forbidden(); |
||
131 | } |
||
132 | |||
133 | list($parentPath,) = \Sabre\Uri\split($this->path); |
||
134 | list(, $newName) = \Sabre\Uri\split($name); |
||
135 | |||
136 | // verify path of the target |
||
137 | $this->verifyPath(); |
||
138 | |||
139 | $newPath = $parentPath . '/' . $newName; |
||
140 | |||
141 | if (!$this->fileView->rename($this->path, $newPath)) { |
||
142 | throw new \Sabre\DAV\Exception('Failed to rename '. $this->path . ' to ' . $newPath); |
||
143 | } |
||
144 | |||
145 | $this->path = $newPath; |
||
146 | |||
147 | $this->refreshInfo(); |
||
148 | } |
||
149 | |||
150 | public function setPropertyCache($property_cache) { |
||
151 | $this->property_cache = $property_cache; |
||
152 | } |
||
153 | |||
154 | /** |
||
155 | * Returns the last modification time, as a unix timestamp |
||
156 | * |
||
157 | * @return int timestamp as integer |
||
158 | */ |
||
159 | public function getLastModified() { |
||
160 | $timestamp = $this->info->getMtime(); |
||
161 | if (!empty($timestamp)) { |
||
162 | return (int)$timestamp; |
||
163 | } |
||
164 | return $timestamp; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * sets the last modification time of the file (mtime) to the value given |
||
169 | * in the second parameter or to now if the second param is empty. |
||
170 | * Even if the modification time is set to a custom value the access time is set to now. |
||
171 | */ |
||
172 | public function touch($mtime) { |
||
173 | $mtime = $this->sanitizeMtime($mtime); |
||
174 | $this->fileView->touch($this->path, $mtime); |
||
175 | $this->refreshInfo(); |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Returns the ETag for a file |
||
180 | * |
||
181 | * An ETag is a unique identifier representing the current version of the |
||
182 | * file. If the file changes, the ETag MUST change. The ETag is an |
||
183 | * arbitrary string, but MUST be surrounded by double-quotes. |
||
184 | * |
||
185 | * Return null if the ETag can not effectively be determined |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getETag() { |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * Sets the ETag |
||
195 | * |
||
196 | * @param string $etag |
||
197 | * |
||
198 | * @return int file id of updated file or -1 on failure |
||
199 | */ |
||
200 | public function setETag($etag) { |
||
202 | } |
||
203 | |||
204 | public function setCreationTime(int $time) { |
||
205 | return $this->fileView->putFileInfo($this->path, array('creation_time' => $time)); |
||
206 | } |
||
207 | |||
208 | public function setUploadTime(int $time) { |
||
209 | return $this->fileView->putFileInfo($this->path, array('upload_time' => $time)); |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Returns the size of the node, in bytes |
||
214 | * |
||
215 | * @return integer |
||
216 | */ |
||
217 | public function getSize() { |
||
218 | return $this->info->getSize(); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * Returns the cache's file id |
||
223 | * |
||
224 | * @return int |
||
225 | */ |
||
226 | public function getId() { |
||
227 | return $this->info->getId(); |
||
228 | } |
||
229 | |||
230 | /** |
||
231 | * @return string|null |
||
232 | */ |
||
233 | public function getFileId() { |
||
234 | if ($this->info->getId()) { |
||
235 | $instanceId = \OC_Util::getInstanceId(); |
||
236 | $id = sprintf('%08d', $this->info->getId()); |
||
237 | return $id . $instanceId; |
||
238 | } |
||
239 | |||
240 | return null; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @return integer |
||
245 | */ |
||
246 | public function getInternalFileId() { |
||
247 | return $this->info->getId(); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * @param string $user |
||
252 | * @return int |
||
253 | */ |
||
254 | public function getSharePermissions($user) { |
||
255 | |||
256 | // check of we access a federated share |
||
257 | if ($user !== null) { |
||
258 | try { |
||
259 | $share = $this->shareManager->getShareByToken($user); |
||
260 | return $share->getPermissions(); |
||
261 | } catch (ShareNotFound $e) { |
||
262 | // ignore |
||
263 | } |
||
264 | } |
||
265 | |||
266 | try { |
||
267 | $storage = $this->info->getStorage(); |
||
268 | } catch (StorageNotAvailableException $e) { |
||
269 | $storage = null; |
||
270 | } |
||
271 | |||
272 | if ($storage && $storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) { |
||
273 | /** @var \OCA\Files_Sharing\SharedStorage $storage */ |
||
274 | $permissions = (int)$storage->getShare()->getPermissions(); |
||
275 | } else { |
||
276 | $permissions = $this->info->getPermissions(); |
||
277 | } |
||
278 | |||
279 | /* |
||
280 | * We can always share non moveable mount points with DELETE and UPDATE |
||
281 | * Eventually we need to do this properly |
||
282 | */ |
||
283 | $mountpoint = $this->info->getMountPoint(); |
||
284 | if (!($mountpoint instanceof MoveableMount)) { |
||
285 | $mountpointpath = $mountpoint->getMountPoint(); |
||
286 | if (substr($mountpointpath, -1) === '/') { |
||
287 | $mountpointpath = substr($mountpointpath, 0, -1); |
||
288 | } |
||
289 | |||
290 | if (!$mountpoint->getOption('readonly', false) && $mountpointpath === $this->info->getPath()) { |
||
291 | $permissions |= \OCP\Constants::PERMISSION_DELETE | \OCP\Constants::PERMISSION_UPDATE; |
||
292 | } |
||
293 | } |
||
294 | |||
295 | /* |
||
296 | * Files can't have create or delete permissions |
||
297 | */ |
||
298 | if ($this->info->getType() === \OCP\Files\FileInfo::TYPE_FILE) { |
||
299 | $permissions &= ~(\OCP\Constants::PERMISSION_CREATE | \OCP\Constants::PERMISSION_DELETE); |
||
300 | } |
||
301 | |||
302 | return $permissions; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * @param string $user |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getNoteFromShare($user) { |
||
310 | if ($user === null) { |
||
311 | return ''; |
||
312 | } |
||
313 | |||
314 | $types = [ |
||
315 | Share::SHARE_TYPE_USER, |
||
316 | Share::SHARE_TYPE_GROUP, |
||
317 | Share::SHARE_TYPE_CIRCLE, |
||
318 | Share::SHARE_TYPE_ROOM |
||
319 | ]; |
||
320 | |||
321 | foreach ($types as $shareType) { |
||
322 | $shares = $this->shareManager->getSharedWith($user, $shareType, $this, -1); |
||
323 | foreach ($shares as $share) { |
||
324 | $note = $share->getNote(); |
||
325 | if($share->getShareOwner() !== $user && !empty($note)) { |
||
326 | return $note; |
||
327 | } |
||
328 | } |
||
329 | } |
||
330 | |||
331 | return ''; |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * @return string |
||
336 | */ |
||
337 | public function getDavPermissions() { |
||
367 | } |
||
368 | |||
369 | public function getOwner() { |
||
370 | return $this->info->getOwner(); |
||
371 | } |
||
372 | |||
373 | protected function verifyPath() { |
||
374 | try { |
||
375 | $fileName = basename($this->info->getPath()); |
||
376 | $this->fileView->verifyPath($this->path, $fileName); |
||
377 | } catch (\OCP\Files\InvalidPathException $ex) { |
||
378 | throw new InvalidPath($ex->getMessage()); |
||
379 | } |
||
380 | } |
||
381 | |||
382 | /** |
||
383 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
384 | */ |
||
385 | public function acquireLock($type) { |
||
386 | $this->fileView->lockFile($this->path, $type); |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
391 | */ |
||
392 | public function releaseLock($type) { |
||
393 | $this->fileView->unlockFile($this->path, $type); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * @param int $type \OCP\Lock\ILockingProvider::LOCK_SHARED or \OCP\Lock\ILockingProvider::LOCK_EXCLUSIVE |
||
398 | */ |
||
399 | public function changeLock($type) { |
||
401 | } |
||
402 | |||
403 | public function getFileInfo() { |
||
404 | return $this->info; |
||
405 | } |
||
406 | |||
407 | protected function sanitizeMtime($mtimeFromRequest) { |
||
417 | } |
||
418 | |||
419 | } |
||
420 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.