Total Complexity | 43 |
Total Lines | 432 |
Duplicated Lines | 0 % |
Changes | 8 | ||
Bugs | 1 | Features | 1 |
Complex classes like Monitor 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 Monitor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
44 | class Monitor |
||
45 | { |
||
46 | /** File access |
||
47 | * @var int |
||
48 | */ |
||
49 | const DELETE = 1; |
||
50 | const RENAME = 2; |
||
51 | const WRITE = 3; |
||
52 | const READ = 4; |
||
53 | const CREATE = 5; |
||
54 | |||
55 | /** @var IRequest */ |
||
56 | protected $request; |
||
57 | |||
58 | /** @var IConfig */ |
||
59 | protected $config; |
||
60 | |||
61 | /** @var ITimeFactory */ |
||
62 | protected $time; |
||
63 | |||
64 | /** @var IAppManager */ |
||
65 | protected $appManager; |
||
66 | |||
67 | /** @var ILogger */ |
||
68 | protected $logger; |
||
69 | |||
70 | /** @var IRootFolder */ |
||
71 | protected $rootFolder; |
||
72 | |||
73 | /** @var EntropyAnalyzer */ |
||
74 | protected $entropyAnalyzer; |
||
75 | |||
76 | /** @var FileOperationMapper */ |
||
77 | protected $mapper; |
||
78 | |||
79 | /** @var FileExtensionAnalyzer */ |
||
80 | protected $fileExtensionAnalyzer; |
||
81 | |||
82 | /** @var FileCorruptionAnalyzer */ |
||
83 | protected $fileCorruptionAnalyzer; |
||
84 | |||
85 | /** @var string */ |
||
86 | protected $userId; |
||
87 | |||
88 | /** @var int */ |
||
89 | protected $nestingLevel = 0; |
||
90 | |||
91 | /** |
||
92 | * @param IRequest $request |
||
93 | * @param IConfig $config |
||
94 | * @param ITimeFactory $time |
||
95 | * @param IAppManager $appManager |
||
96 | * @param ILogger $logger |
||
97 | * @param IRootFolder $rootFolder |
||
98 | * @param EntropyAnalyzer $entropyAnalyzer |
||
99 | * @param FileOperationMapper $mapper |
||
100 | * @param FileExtensionAnalyzer $fileExtensionAnalyzer |
||
101 | * @param FileCorruptionAnalyzer $fileCorruptionAnalyzer |
||
102 | * @param string $userId |
||
103 | */ |
||
104 | public function __construct( |
||
128 | } |
||
129 | |||
130 | /** |
||
131 | * Analyze file. |
||
132 | * |
||
133 | * @param array $paths |
||
134 | * @param int $mode |
||
135 | */ |
||
136 | public function analyze($paths, $mode) |
||
137 | { |
||
138 | $path = $paths[0]; |
||
139 | |||
140 | $storage = $this->rootFolder->getUserFolder($this->userId)->get(dirname($path))->getStorage(); |
||
141 | if ($this->userId === null || $this->nestingLevel !== 0 || /*!$this->isUploadedFile($storage, $path) ||*/ $this->isCreatingSkeletonFiles()) { |
||
142 | // check only cloud files and no system files |
||
143 | return; |
||
144 | } |
||
145 | |||
146 | if (!$this->request->isUserAgent([ |
||
147 | IRequest::USER_AGENT_CLIENT_DESKTOP, |
||
148 | IRequest::USER_AGENT_CLIENT_ANDROID, |
||
149 | IRequest::USER_AGENT_CLIENT_IOS, |
||
150 | ])) { |
||
151 | // not a sync client |
||
152 | return; |
||
153 | } |
||
154 | |||
155 | $this->nestingLevel++; |
||
156 | |||
157 | switch ($mode) { |
||
158 | case self::RENAME: |
||
159 | $path = $paths[1]; |
||
160 | $this->logger->debug("Rename ".$paths[0]." to ".$paths[1], ['app' => Application::APP_ID]); |
||
161 | if (preg_match('/.+\.d[0-9]+/', pathinfo($paths[1])['basename']) > 0) { |
||
162 | return; |
||
163 | } |
||
164 | // reset PROPFIND_COUNT |
||
165 | $this->resetProfindCount(); |
||
166 | |||
167 | try { |
||
168 | $userRoot = $this->rootFolder->getUserFolder($this->userId); |
||
169 | $node = $userRoot->get($path); |
||
170 | } catch (\OCP\Files\NotFoundException $exception) { |
||
171 | $this->logger->error("File Not Found ".$path, ['app' => Application::APP_ID]); |
||
172 | return; |
||
173 | } |
||
174 | |||
175 | // not a file no need to analyze |
||
176 | if (!($node instanceof File)) { |
||
177 | $this->addFolderOperation($paths, $node, self::RENAME); |
||
178 | $this->nestingLevel--; |
||
179 | |||
180 | return; |
||
181 | } |
||
182 | |||
183 | $this->addFileOperation($paths, $node, self::RENAME); |
||
184 | |||
185 | $this->nestingLevel--; |
||
186 | |||
187 | return; |
||
188 | case self::WRITE: |
||
189 | $this->logger->debug("Write ".$path, ['app' => Application::APP_ID]); |
||
190 | // reset PROPFIND_COUNT |
||
191 | $this->resetProfindCount(); |
||
192 | |||
193 | try { |
||
194 | $userRoot = $this->rootFolder->getUserFolder($this->userId); |
||
195 | $node = $userRoot->get($path); |
||
196 | } catch (\OCP\Files\NotFoundException $exception) { |
||
197 | $this->logger->error("File Not Found ".$path, ['app' => Application::APP_ID]); |
||
198 | return; |
||
199 | } |
||
200 | |||
201 | // not a file no need to analyze |
||
202 | if (!($node instanceof File)) { |
||
203 | $this->addFolderOperation($paths, $node, self::WRITE); |
||
204 | $this->nestingLevel--; |
||
205 | |||
206 | return; |
||
207 | } |
||
208 | |||
209 | $this->addFileOperation($paths, $node, self::WRITE); |
||
210 | |||
211 | $this->nestingLevel--; |
||
212 | |||
213 | return; |
||
214 | case self::READ: |
||
215 | $this->nestingLevel--; |
||
216 | |||
217 | return; |
||
218 | case self::DELETE: |
||
219 | $this->logger->debug("Delete", ['app' => Application::APP_ID]); |
||
220 | // reset PROPFIND_COUNT |
||
221 | $this->resetProfindCount(); |
||
222 | |||
223 | try { |
||
224 | $userRoot = $this->rootFolder->getUserFolder($this->userId); |
||
225 | $node = $userRoot->get($path); |
||
226 | } catch (\OCP\Files\NotFoundException $exception) { |
||
227 | $this->logger->error("File Not Found ".$path, ['app' => Application::APP_ID]); |
||
228 | return; |
||
229 | } |
||
230 | |||
231 | // not a file no need to analyze |
||
232 | if (!($node instanceof File)) { |
||
233 | $this->addFolderOperation($paths, $node, self::DELETE); |
||
234 | $this->nestingLevel--; |
||
235 | |||
236 | return; |
||
237 | } |
||
238 | |||
239 | $this->addFileOperation($paths, $node, self::DELETE); |
||
240 | |||
241 | $this->nestingLevel--; |
||
242 | |||
243 | return; |
||
244 | case self::CREATE: |
||
245 | $this->logger->debug("Create", ['app' => Application::APP_ID]); |
||
246 | // reset PROPFIND_COUNT |
||
247 | $this->resetProfindCount(); |
||
248 | |||
249 | try { |
||
250 | $userRoot = $this->rootFolder->getUserFolder($this->userId); |
||
251 | $node = $userRoot->get($path); |
||
252 | } catch (\OCP\Files\NotFoundException $exception) { |
||
253 | $this->logger->error("File Not Found ".$path, ['app' => Application::APP_ID]); |
||
254 | return; |
||
255 | } |
||
256 | if (!($node instanceof File)) { |
||
257 | |||
258 | $fileOperation = new FileOperation(); |
||
259 | $fileOperation->setUserId($this->userId); |
||
260 | $fileOperation->setPath(str_replace('files', '', pathinfo($path)['dirname'])); |
||
261 | $fileOperation->setOriginalName(pathinfo($path)['basename']); |
||
262 | $fileOperation->setType('folder'); |
||
263 | $fileOperation->setMimeType('httpd/unix-directory'); |
||
264 | $fileOperation->setSize(0); |
||
265 | $fileOperation->setTimestamp(time()); |
||
266 | $fileOperation->setCorrupted(false); |
||
267 | $fileOperation->setCommand(self::CREATE); |
||
268 | $sequenceId = $this->config->getUserValue($this->userId, Application::APP_ID, 'sequence_id', 0); |
||
269 | $fileOperation->setSequence($sequenceId); |
||
270 | |||
271 | // entropy analysis |
||
272 | $fileOperation->setEntropy(0.0); |
||
273 | $fileOperation->setStandardDeviation(0.0); |
||
274 | $fileOperation->setFileClass(EntropyResult::NORMAL); |
||
275 | |||
276 | // file extension analysis |
||
277 | $fileOperation->setFileExtensionClass(FileExtensionResult::NOT_SUSPICIOUS); |
||
278 | |||
279 | $this->mapper->insert($fileOperation); |
||
280 | $this->nestingLevel--; |
||
281 | } else { |
||
282 | $this->addFileOperation($paths, $node, self::CREATE); |
||
283 | |||
284 | $this->nestingLevel--; |
||
285 | } |
||
286 | |||
287 | return; |
||
288 | default: |
||
289 | $this->nestingLevel--; |
||
290 | |||
291 | return; |
||
292 | } |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Check if we are in the LoginController and if so, ignore the firewall. |
||
297 | * |
||
298 | * @return bool |
||
299 | */ |
||
300 | protected function isCreatingSkeletonFiles() |
||
301 | { |
||
302 | $exception = new \Exception(); |
||
303 | $trace = $exception->getTrace(); |
||
304 | foreach ($trace as $step) { |
||
305 | if (isset($step['class'], $step['function']) && |
||
306 | $step['class'] === 'OC\Core\Controller\LoginController' && |
||
307 | $step['function'] === 'tryLogin') { |
||
308 | return true; |
||
309 | } |
||
310 | } |
||
311 | |||
312 | return false; |
||
313 | } |
||
314 | |||
315 | /** |
||
316 | * Reset PROPFIND_COUNT. |
||
317 | */ |
||
318 | protected function resetProfindCount() |
||
319 | { |
||
320 | $userKeys = $this->config->getUserKeys($this->userId, Application::APP_ID); |
||
321 | foreach ($userKeys as $key) { |
||
322 | if (strpos($key, 'propfind_count') !== false) { |
||
323 | $this->config->deleteUserValue($this->userId, Application::APP_ID, $key); |
||
324 | } |
||
325 | } |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * Return file size of a path. |
||
330 | * |
||
331 | * @param string $path |
||
332 | * |
||
333 | * @return int |
||
334 | */ |
||
335 | private function getFileSize($path) |
||
354 | } |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * Check if file is a uploaded file. |
||
359 | * |
||
360 | * @param IStorage $storage |
||
361 | * @param string $path |
||
362 | * |
||
363 | * @return bool |
||
364 | */ |
||
365 | private function isUploadedFile(IStorage $storage, $path) |
||
366 | { |
||
367 | $fullPath = $path; |
||
368 | if (property_exists($storage, 'mountPoint')) { |
||
369 | /* @var StorageWrapper $storage */ |
||
370 | try { |
||
371 | $fullPath = $storage->mountPoint.$path; |
||
372 | } catch (\Exception $ex) { |
||
373 | return true; |
||
374 | } |
||
375 | } |
||
376 | |||
377 | // ignore transfer files |
||
378 | if (strpos($fullPath, 'ocTransferId') > 0) { |
||
379 | return false; |
||
380 | } |
||
381 | |||
382 | if (substr_count($fullPath, '/') < 3) { |
||
383 | return false; |
||
384 | } |
||
385 | |||
386 | // '', admin, 'files', 'path/to/file.txt' |
||
387 | $segment = explode('/', $fullPath, 4); |
||
388 | |||
389 | return isset($segment[2]) && in_array($segment[2], [ |
||
390 | 'files', |
||
391 | 'thumbnails', |
||
392 | 'files_versions', |
||
393 | ], true); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Add a folder to the operations. |
||
398 | * |
||
399 | * @param array $paths |
||
400 | * @param INode $node |
||
401 | * @param int $operation |
||
402 | */ |
||
403 | private function addFolderOperation($paths, $node, $operation) |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * Add a file to the operations. |
||
435 | * |
||
436 | * @param array $paths |
||
437 | * @param INode $node |
||
438 | * @param int $operation |
||
439 | */ |
||
440 | private function addFileOperation($paths, $node, $operation) |
||
476 | } |
||
477 | } |
||
478 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths