Conditions | 16 |
Paths | 14 |
Total Lines | 86 |
Lines | 10 |
Ratio | 11.63 % |
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 |
||
53 | public function work(array $arguments = []) |
||
54 | { |
||
55 | |||
56 | // Possible clean up of missing files if the User has clicked so. |
||
57 | if (!empty($arguments['deleteDuplicateFiles'])) { |
||
58 | $this->deleteMissingFilesAction($arguments['files']); |
||
59 | } |
||
60 | |||
61 | $templateNameAndPath = 'EXT:media/Resources/Private/Standalone/Tool/DuplicateFilesFinder/WorkResult.html'; |
||
62 | $view = $this->initializeStandaloneView($templateNameAndPath); |
||
63 | |||
64 | $duplicateFilesReports = []; |
||
65 | |||
66 | if ($this->getBackendUser()->isAdmin()) { |
||
67 | View Code Duplication | foreach ($this->getStorageRepository()->findAll() as $storage) { |
|
|
|||
68 | if ($storage->isOnline()) { |
||
69 | $duplicateFiles = $this->getIndexAnalyser()->searchForDuplicateSha1($storage); |
||
70 | $duplicateFilesReports[] = array( |
||
71 | 'storage' => $storage, |
||
72 | 'duplicateFiles' => $duplicateFiles, |
||
73 | 'numberOfDuplicateFiles' => count($duplicateFiles), |
||
74 | ); |
||
75 | } |
||
76 | } |
||
77 | } else { |
||
78 | |||
79 | $fileMounts = $this->getBackendUser()->getFileMountRecords(); |
||
80 | |||
81 | $allowedStorages = []; |
||
82 | foreach ($fileMounts as $fileMount) { |
||
83 | if ((bool)$fileMount['read_only']) { |
||
84 | continue; |
||
85 | } |
||
86 | |||
87 | if (!isset($allowedStorages[$fileMount['base']])) { |
||
88 | $allowedStorages[$fileMount['base']] = []; |
||
89 | } |
||
90 | if (!in_array($fileMount['base'], $allowedStorages)) { |
||
91 | $allowedStorages[$fileMount['base']][] = $fileMount['path']; |
||
92 | } |
||
93 | } |
||
94 | |||
95 | foreach ($allowedStorages as $storageIdentifier => $allowedMountPoints) { |
||
96 | $storage = ResourceFactory::getInstance()->getStorageObject($storageIdentifier); |
||
97 | |||
98 | if ($storage->isOnline()) { |
||
99 | |||
100 | $duplicateFiles = $this->getIndexAnalyser()->searchForDuplicateSha1($storage); |
||
101 | |||
102 | // Filter duplicates files |
||
103 | foreach ($duplicateFiles as $key => $files) { |
||
104 | |||
105 | $filteredFiles = []; |
||
106 | foreach ($files as $file) { |
||
107 | |||
108 | foreach ($allowedMountPoints as $allowedMountPoint) { |
||
109 | |||
110 | $pattern = '%^' . $allowedMountPoint . '%isU'; |
||
111 | if (preg_match($pattern, $file['identifier'])) { |
||
112 | $filteredFiles[] = $file; |
||
113 | break; // no need to further loop around, stop the loop. |
||
114 | } |
||
115 | } |
||
116 | } |
||
117 | |||
118 | // We need more than 1 files to be shown as duplicate. |
||
119 | if (count($filteredFiles) > 1) { |
||
120 | $duplicateFiles[$key] = $filteredFiles; |
||
121 | } else { |
||
122 | unset($duplicateFiles[$key]); |
||
123 | } |
||
124 | } |
||
125 | $duplicateFilesReports[] = array( |
||
126 | 'storage' => $storage, |
||
127 | 'duplicateFiles' => $duplicateFiles, |
||
128 | 'numberOfDuplicateFiles' => count($duplicateFiles), |
||
129 | ); |
||
130 | |||
131 | } |
||
132 | } |
||
133 | } |
||
134 | |||
135 | $view->assign('duplicateFilesReports', $duplicateFilesReports); |
||
136 | |||
137 | return $view->render(); |
||
138 | } |
||
139 | |||
226 |
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.