| Conditions | 16 |
| Paths | 962 |
| Total Lines | 139 |
| Lines | 5 |
| Ratio | 3.6 % |
| 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 |
||
| 140 | public function index($dir = '', $view = '', $fileid = null, $fileNotFound = false) { |
||
| 141 | if ($fileid !== null) { |
||
| 142 | try { |
||
| 143 | return $this->showFile($fileid); |
||
| 144 | } catch (NotFoundException $e) { |
||
| 145 | return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', ['fileNotFound' => true])); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | $nav = new \OCP\Template('files', 'appnavigation', ''); |
||
| 150 | |||
| 151 | // Load the files we need |
||
| 152 | \OCP\Util::addStyle('files', 'merged'); |
||
| 153 | \OCP\Util::addScript('files', 'merged-index'); |
||
| 154 | |||
| 155 | // mostly for the home storage's free space |
||
| 156 | // FIXME: Make non static |
||
| 157 | $storageInfo = $this->getStorageInfo(); |
||
| 158 | |||
| 159 | $user = $this->userSession->getUser()->getUID(); |
||
| 160 | |||
| 161 | // Get all the user favorites to create a submenu |
||
| 162 | try { |
||
| 163 | $favElements = $this->activityHelper->getFavoriteFilePaths($this->userSession->getUser()->getUID()); |
||
| 164 | } catch (\RuntimeException $e) { |
||
| 165 | $favElements['folders'] = null; |
||
| 166 | } |
||
| 167 | |||
| 168 | $collapseClasses = ''; |
||
| 169 | if (count($favElements['folders']) > 0) { |
||
| 170 | $collapseClasses = 'collapsible'; |
||
| 171 | } |
||
| 172 | |||
| 173 | $favoritesSublistArray = Array(); |
||
| 174 | |||
| 175 | $navBarPositionPosition = 6; |
||
| 176 | $currentCount = 0; |
||
| 177 | foreach ($favElements['folders'] as $dir) { |
||
| 178 | |||
| 179 | $id = substr($dir, strrpos($dir, '/') + 1, strlen($dir)); |
||
| 180 | $link = $this->urlGenerator->linkToRoute('files.view.index', ['dir' => $dir, 'view' => 'files']); |
||
| 181 | $sortingValue = ++$currentCount; |
||
| 182 | $element = [ |
||
| 183 | 'id' => str_replace('/', '-', $dir), |
||
| 184 | 'view' => 'files', |
||
| 185 | 'href' => $link, |
||
| 186 | 'dir' => $dir, |
||
| 187 | 'order' => $navBarPositionPosition, |
||
| 188 | 'folderPosition' => $sortingValue, |
||
| 189 | 'name' => $id, |
||
| 190 | 'icon' => 'files', |
||
| 191 | 'quickaccesselement' => 'true' |
||
| 192 | ]; |
||
| 193 | |||
| 194 | array_push($favoritesSublistArray, $element); |
||
| 195 | $navBarPositionPosition++; |
||
| 196 | } |
||
| 197 | |||
| 198 | $navItems = \OCA\Files\App::getNavigationManager()->getAll(); |
||
| 199 | |||
| 200 | // add the favorites entry in menu |
||
| 201 | $navItems['favorites']['sublist'] = $favoritesSublistArray; |
||
| 202 | $navItems['favorites']['classes'] = $collapseClasses; |
||
| 203 | |||
| 204 | // parse every menu and add the expandedState user value |
||
| 205 | foreach ($navItems as $key => $item) { |
||
| 206 | if (isset($item['expandedState'])) { |
||
| 207 | $navItems[$key]['defaultExpandedState'] = $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', $item['expandedState'], '0') === '1'; |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | $nav->assign('navigationItems', $navItems); |
||
| 212 | |||
| 213 | $nav->assign('usage', \OC_Helper::humanFileSize($storageInfo['used'])); |
||
| 214 | View Code Duplication | if ($storageInfo['quota'] === \OCP\Files\FileInfo::SPACE_UNLIMITED) { |
|
| 215 | $totalSpace = $this->l10n->t('Unlimited'); |
||
| 216 | } else { |
||
| 217 | $totalSpace = \OC_Helper::humanFileSize($storageInfo['total']); |
||
| 218 | } |
||
| 219 | $nav->assign('total_space', $totalSpace); |
||
| 220 | $nav->assign('quota', $storageInfo['quota']); |
||
| 221 | $nav->assign('usage_relative', $storageInfo['relative']); |
||
| 222 | |||
| 223 | $contentItems = []; |
||
| 224 | |||
| 225 | // render the container content for every navigation item |
||
| 226 | foreach ($navItems as $item) { |
||
| 227 | $content = ''; |
||
| 228 | if (isset($item['script'])) { |
||
| 229 | $content = $this->renderScript($item['appname'], $item['script']); |
||
| 230 | } |
||
| 231 | // parse submenus |
||
| 232 | if (isset($item['sublist'])) { |
||
| 233 | foreach ($item['sublist'] as $subitem) { |
||
| 234 | $subcontent = ''; |
||
| 235 | if (isset($subitem['script'])) { |
||
| 236 | $subcontent = $this->renderScript($subitem['appname'], $subitem['script']); |
||
| 237 | } |
||
| 238 | $contentItems[$subitem['id']] = [ |
||
| 239 | 'id' => $subitem['id'], |
||
| 240 | 'content' => $subcontent |
||
| 241 | ]; |
||
| 242 | } |
||
| 243 | } |
||
| 244 | $contentItems[$item['id']] = [ |
||
| 245 | 'id' => $item['id'], |
||
| 246 | 'content' => $content |
||
| 247 | ]; |
||
| 248 | } |
||
| 249 | |||
| 250 | $event = new GenericEvent(null, ['hiddenFields' => []]); |
||
| 251 | $this->eventDispatcher->dispatch('OCA\Files::loadAdditionalScripts', $event); |
||
| 252 | |||
| 253 | $params = []; |
||
| 254 | $params['usedSpacePercent'] = (int) $storageInfo['relative']; |
||
| 255 | $params['owner'] = $storageInfo['owner']; |
||
| 256 | $params['ownerDisplayName'] = $storageInfo['ownerDisplayName']; |
||
| 257 | $params['isPublic'] = false; |
||
| 258 | $params['allowShareWithLink'] = $this->config->getAppValue('core', 'shareapi_allow_links', 'yes'); |
||
| 259 | $params['defaultFileSorting'] = $this->config->getUserValue($user, 'files', 'file_sorting', 'name'); |
||
| 260 | $params['defaultFileSortingDirection'] = $this->config->getUserValue($user, 'files', 'file_sorting_direction', 'asc'); |
||
| 261 | $showHidden = (bool) $this->config->getUserValue($this->userSession->getUser()->getUID(), 'files', 'show_hidden', false); |
||
| 262 | $params['showHiddenFiles'] = $showHidden ? 1 : 0; |
||
| 263 | $params['fileNotFound'] = $fileNotFound ? 1 : 0; |
||
| 264 | $params['appNavigation'] = $nav; |
||
| 265 | $params['appContents'] = $contentItems; |
||
| 266 | $params['hiddenFields'] = $event->getArgument('hiddenFields'); |
||
| 267 | |||
| 268 | $response = new TemplateResponse( |
||
| 269 | $this->appName, |
||
| 270 | 'index', |
||
| 271 | $params |
||
| 272 | ); |
||
| 273 | $policy = new ContentSecurityPolicy(); |
||
| 274 | $policy->addAllowedFrameDomain('\'self\''); |
||
| 275 | $response->setContentSecurityPolicy($policy); |
||
| 276 | |||
| 277 | return $response; |
||
| 278 | } |
||
| 279 | |||
| 316 |
If you suppress an error, we recommend checking for the error condition explicitly: