| Conditions | 36 |
| Paths | > 20000 |
| Total Lines | 117 |
| Code Lines | 90 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 69 | function ls($curpath) |
||
| 70 | { |
||
| 71 | global $_lang, $theme_image_path, $_style; |
||
| 72 | global $excludes, $protected_path, $editablefiles, $inlineviewablefiles, $viewablefiles, $enablefileunzip, $enablefiledownload, $uploadablefiles, $folders, $files, $filesizes, $len, $dirs_array, $files_array, $webstart_path, $modx; |
||
| 73 | $dircounter = 0; |
||
| 74 | $filecounter = 0; |
||
| 75 | $curpath = str_replace('//', '/', $curpath . '/'); |
||
| 76 | |||
| 77 | if (!is_dir($curpath)) { |
||
| 78 | echo 'Invalid path "', $curpath, '"<br />'; |
||
| 79 | |||
| 80 | return; |
||
| 81 | } |
||
| 82 | $dir = scandir($curpath); |
||
| 83 | |||
| 84 | // first, get info |
||
| 85 | foreach ($dir as $file) { |
||
| 86 | $newpath = $curpath . $file; |
||
| 87 | if ($file === '..' || $file === '.') { |
||
| 88 | continue; |
||
| 89 | } |
||
| 90 | if (is_dir($newpath)) { |
||
| 91 | $dirs_array[$dircounter]['dir'] = $newpath; |
||
| 92 | $dirs_array[$dircounter]['stats'] = lstat($newpath); |
||
| 93 | if ($file === '..' || $file === '.') { |
||
| 94 | continue; |
||
| 95 | } elseif (!in_array($file, $excludes) && !in_array($newpath, $protected_path)) { |
||
| 96 | $dirs_array[$dircounter]['text'] = '<i class="' . $_style['files_folder'] . ' FilesFolder"></i> <a href="index.php?a=31&mode=drill&path=' . urlencode($newpath) . '"><b>' . $file . '</b></a>'; |
||
| 97 | |||
| 98 | $dfiles = scandir($newpath); |
||
| 99 | foreach ($dfiles as $i => $infile) { |
||
| 100 | switch ($infile) { |
||
| 101 | case '..': |
||
| 102 | case '.': |
||
| 103 | unset($dfiles[$i]); |
||
| 104 | break; |
||
| 105 | } |
||
| 106 | } |
||
| 107 | $file_exists = (0 < count($dfiles)) ? 'file_exists' : ''; |
||
| 108 | |||
| 109 | $dirs_array[$dircounter]['delete'] = is_writable($curpath) ? '<a href="javascript: deleteFolder(\'' . urlencode($file) . '\',\'' . $file_exists . '\');"><i class="' . $_style['files_delete'] . '" title="' . $_lang['file_delete_folder'] . '"></i></a>' : ''; |
||
| 110 | } else { |
||
| 111 | $dirs_array[$dircounter]['text'] = '<span><i class="' . $_style['files_deleted_folder'] . ' FilesDeletedFolder"></i> ' . $file . '</span>'; |
||
| 112 | $dirs_array[$dircounter]['delete'] = is_writable($curpath) ? '<span class="disabled"><i class="' . $_style['files_delete'] . '" title="' . $_lang['file_delete_folder'] . '"></i></span>' : ''; |
||
| 113 | } |
||
| 114 | |||
| 115 | $dirs_array[$dircounter]['rename'] = is_writable($curpath) ? '<a href="javascript:renameFolder(\'' . urlencode($file) . '\');"><i class="' . $_style['files_rename'] . '" title="' . $_lang['rename'] . '"></i></a> ' : ''; |
||
| 116 | |||
| 117 | // increment the counter |
||
| 118 | $dircounter++; |
||
| 119 | } else { |
||
| 120 | $type = getExtension($newpath); |
||
| 121 | $files_array[$filecounter]['file'] = $newpath; |
||
| 122 | $files_array[$filecounter]['stats'] = lstat($newpath); |
||
| 123 | $files_array[$filecounter]['text'] = determineIcon($newpath, $_REQUEST['path'], |
||
| 124 | $_REQUEST['mode']) . ' ' . $file; |
||
| 125 | $files_array[$filecounter]['view'] = (in_array($type, |
||
| 126 | $viewablefiles)) ? '<a href="javascript:;" onclick="viewfile(\'' . $webstart_path . substr($newpath, |
||
| 127 | $len, |
||
| 128 | strlen($newpath)) . '\');"><i class="' . $_style['files_view'] . '" title="' . $_lang['files_viewfile'] . '"></i></a>' : (($enablefiledownload && in_array($type, |
||
| 129 | $uploadablefiles)) ? '<a href="' . $webstart_path . implode('/', array_map('rawurlencode', |
||
| 130 | explode('/', substr($newpath, $len, |
||
| 131 | strlen($newpath))))) . '" style="cursor:pointer;"><i class="' . $_style['files_download'] . '" title="' . $_lang['file_download_file'] . '"></i></a>' : '<span class="disabled"><i class="' . $_style['files_view'] . '" title="' . $_lang['files_viewfile'] . '"></i></span>'); |
||
| 132 | $files_array[$filecounter]['view'] = (in_array($type, |
||
| 133 | $inlineviewablefiles)) ? '<a href="index.php?a=31&mode=view&path=' . urlencode($newpath) . '"><i class="' . $_style['files_view'] . '" title="' . $_lang['files_viewfile'] . '"></i></a>' : $files_array[$filecounter]['view']; |
||
| 134 | $files_array[$filecounter]['unzip'] = ($enablefileunzip && $type == '.zip') ? '<a href="javascript:unzipFile(\'' . urlencode($file) . '\');"><i class="' . $_style['files_unzip'] . '" title="' . $_lang['file_download_unzip'] . '"></i></a>' : ''; |
||
| 135 | $files_array[$filecounter]['edit'] = (in_array($type, |
||
| 136 | $editablefiles) && is_writable($curpath) && is_writable($newpath)) ? '<a href="index.php?a=31&mode=edit&path=' . urlencode($newpath) . '#file_editfile"><i class="' . $_style['files_edit'] . '" title="' . $_lang['files_editfile'] . '"></i></a>' : '<span class="disabled"><i class="' . $_style['files_edit'] . '" title="' . $_lang['files_editfile'] . '"></i></span>'; |
||
| 137 | $files_array[$filecounter]['duplicate'] = (in_array($type, |
||
| 138 | $editablefiles) && is_writable($curpath) && is_writable($newpath)) ? '<a href="javascript:duplicateFile(\'' . urlencode($file) . '\');"><i class="' . $_style['files_duplicate'] . '" title="' . $_lang['duplicate'] . '"></i></a>' : '<span class="disabled"><i class="' . $_style['files_duplicate'] . '" align="absmiddle" title="' . $_lang['duplicate'] . '"></i></span>'; |
||
| 139 | $files_array[$filecounter]['rename'] = (in_array($type, |
||
| 140 | $editablefiles) && is_writable($curpath) && is_writable($newpath)) ? '<a href="javascript:renameFile(\'' . urlencode($file) . '\');"><i class="' . $_style['files_rename'] . '" align="absmiddle" title="' . $_lang['rename'] . '"></i></a>' : '<span class="disabled"><i class="' . $_style['files_rename'] . '" align="absmiddle" title="' . $_lang['rename'] . '"></i></span>'; |
||
| 141 | $files_array[$filecounter]['delete'] = is_writable($curpath) && is_writable($newpath) ? '<a href="javascript:deleteFile(\'' . urlencode($file) . '\');"><i class="' . $_style['files_delete'] . '" title="' . $_lang['file_delete_file'] . '"></i></a>' : '<span class="disabled"><i class="' . $_style['files_delete'] . '" title="' . $_lang['file_delete_file'] . '"></i></span>'; |
||
| 142 | |||
| 143 | // increment the counter |
||
| 144 | $filecounter++; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | // dump array entries for directories |
||
| 149 | $folders = count($dirs_array); |
||
| 150 | sort($dirs_array); // sorting the array alphabetically (Thanks pxl8r!) |
||
| 151 | for ($i = 0; $i < $folders; $i++) { |
||
| 152 | $filesizes += $dirs_array[$i]['stats']['7']; |
||
| 153 | echo '<tr>'; |
||
| 154 | echo '<td>' . $dirs_array[$i]['text'] . '</td>'; |
||
| 155 | echo '<td class="text-nowrap">' . $modx->toDateFormat($dirs_array[$i]['stats']['9']) . '</td>'; |
||
| 156 | echo '<td class="text-right">' . $modx->nicesize($dirs_array[$i]['stats']['7']) . '</td>'; |
||
| 157 | echo '<td class="actions text-right">'; |
||
| 158 | echo $dirs_array[$i]['rename']; |
||
| 159 | echo $dirs_array[$i]['delete']; |
||
| 160 | echo '</td>'; |
||
| 161 | echo '</tr>'; |
||
| 162 | } |
||
| 163 | |||
| 164 | // dump array entries for files |
||
| 165 | $files = count($files_array); |
||
| 166 | sort($files_array); // sorting the array alphabetically (Thanks pxl8r!) |
||
| 167 | for ($i = 0; $i < $files; $i++) { |
||
| 168 | $filesizes += $files_array[$i]['stats']['7']; |
||
| 169 | echo '<tr ' . markRow($files_array[$i]['file'], $_REQUEST['path'], $_REQUEST['mode']) . '>'; |
||
| 170 | echo '<td>' . $files_array[$i]['text'] . '</td>'; |
||
| 171 | echo '<td class="text-nowrap">' . $modx->toDateFormat($files_array[$i]['stats']['9']) . '</td>'; |
||
| 172 | echo '<td class="text-right">' . $modx->nicesize($files_array[$i]['stats']['7']) . '</td>'; |
||
| 173 | echo '<td class="actions text-right">'; |
||
| 174 | echo $files_array[$i]['unzip']; |
||
| 175 | echo $files_array[$i]['view']; |
||
| 176 | echo $files_array[$i]['edit']; |
||
| 177 | echo $files_array[$i]['duplicate']; |
||
| 178 | echo $files_array[$i]['rename']; |
||
| 179 | echo $files_array[$i]['delete']; |
||
| 180 | echo '</td>'; |
||
| 181 | echo '</tr>'; |
||
| 182 | } |
||
| 183 | |||
| 184 | return; |
||
| 185 | } |
||
| 186 | } |
||
| 573 |
Learn more about camelCase.