| Conditions | 29 |
| Paths | 42 |
| Total Lines | 68 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 39 | public static function getFileHandler(string $file): array |
||
| 40 | { |
||
| 41 | |||
| 42 | // Get get directory, name and file extension |
||
| 43 | $pathParts = pathinfo($file); |
||
| 44 | $ftype = strtolower($pathParts['extension']); |
||
| 45 | $fdir = $pathParts['dirname']; |
||
| 46 | $fname = $pathParts['filename']; |
||
| 47 | $fhandler = ""; |
||
| 48 | |||
| 49 | // Define filehandlers |
||
| 50 | $pdfHandler = '/usr/bin/zathura'; |
||
| 51 | $imageHandler = '/usr/bin/feh --scale-down'; |
||
| 52 | $webHandler = '/usr/bin/x-www-browser'; |
||
| 53 | foreach (["/usr/lib/palma", "./scripts"] as $dir) { |
||
| 54 | $palmaBrowser = $dir . "/palma-browser"; |
||
| 55 | if (file_exists($palmaBrowser)) { |
||
| 56 | $webHandler = $palmaBrowser; |
||
| 57 | break; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | $avHandler = '/usr/bin/cvlc --no-audio'; |
||
| 61 | $officeApp = "writer"; |
||
| 62 | |||
| 63 | // $params; |
||
| 64 | // echo $ftype; |
||
| 65 | if ($ftype === 'pdf') { |
||
| 66 | $fhandler = $pdfHandler; |
||
| 67 | } elseif ($ftype === 'gif' || $ftype === 'jpg' || $ftype === 'png') { |
||
| 68 | $fhandler = $imageHandler; |
||
| 69 | } elseif ($ftype === 'html' || $ftype === 'url') { |
||
| 70 | $fhandler = $webHandler; |
||
| 71 | } elseif ( |
||
| 72 | $ftype === 'mpg' || $ftype === 'mpeg' || $ftype === 'avi' || |
||
| 73 | $ftype === 'mp3' || $ftype === 'mp4' || $ftype === 'wmv' |
||
| 74 | ) { |
||
| 75 | $fhandler = $avHandler; |
||
| 76 | } else { |
||
| 77 | if ($ftype === 'doc' || $ftype === 'docx' || $ftype === 'odt' || $ftype === 'txt') { |
||
| 78 | $officeApp = "writer"; |
||
| 79 | } elseif ($ftype === 'ppt' || $ftype === 'pptx' || $ftype === 'pps' || $ftype === 'ppsx' || $ftype === 'odp') { |
||
| 80 | $officeApp = "impress"; |
||
| 81 | } elseif ($ftype === 'xls' || $ftype === 'xlsx' || $ftype === 'ods') { |
||
| 82 | $officeApp = "calc"; |
||
| 83 | } elseif (shell_exec("/usr/bin/file -b '$file'") === "ASCII text") { |
||
| 84 | $officeApp = "writer"; |
||
| 85 | } |
||
| 86 | $convertedFile = convertOffice($file, $officeApp, $fdir, $fname); |
||
| 87 | if ($convertedFile) { |
||
| 88 | $file = $convertedFile; |
||
| 89 | $fhandler = $pdfHandler; |
||
| 90 | } else { |
||
| 91 | $fhandler = "/usr/bin/libreoffice --'$officeApp' --nologo --norestore -o"; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /* |
||
| 96 | alternatively with mime-types |
||
| 97 | |||
| 98 | // $ftype = mime_content_type($this->UPLOAD_PATH.$file); |
||
| 99 | // if($ftype=='application/pdf') |
||
| 100 | // if($ftype=='image/gif' || $ftype=='image/jpg' || $ftype=='image/png' ) |
||
| 101 | // if($ftype=='html' || $ftype=='url' || $ftype="text/plain") |
||
| 102 | // (...) |
||
| 103 | |||
| 104 | */ |
||
| 105 | |||
| 106 | return array($fhandler, $file); |
||
| 107 | } |
||
| 124 |