| Conditions | 25 |
| Paths | 12 |
| Total Lines | 64 |
| Code Lines | 34 |
| 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 |
||
| 105 | public static function getFileHandler($file) |
||
| 106 | { |
||
| 107 | |||
| 108 | // Get get directory, name and file extension |
||
| 109 | $pathParts = pathinfo($file); |
||
| 110 | $ftype = strtolower($pathParts['extension']); |
||
| 111 | $fdir = $pathParts['dirname']; |
||
| 112 | $fname = $pathParts['filename']; |
||
| 113 | $fhandler = ""; |
||
| 114 | |||
| 115 | // Define filehandlers |
||
| 116 | $pdfHandler = '/usr/bin/zathura'; |
||
| 117 | $imageHandler = '/usr/bin/feh --scale-down'; |
||
| 118 | $webHandler = '/usr/bin/midori -p'; |
||
| 119 | $avHandler = '/usr/bin/cvlc --no-audio'; |
||
| 120 | $officeApp = ""; |
||
| 121 | |||
| 122 | // $params; |
||
| 123 | // echo $ftype; |
||
| 124 | if ($ftype === 'pdf') { |
||
| 125 | $fhandler=$pdfHandler; |
||
| 126 | |||
| 127 | } elseif ($ftype === 'gif' || $ftype === 'jpg' || $ftype === 'png') { |
||
| 128 | $fhandler=$imageHandler; |
||
| 129 | |||
| 130 | } elseif ($ftype === 'html' || $ftype === 'url') { |
||
| 131 | $fhandler=$webHandler; |
||
| 132 | |||
| 133 | } elseif ($ftype === 'mpg' || $ftype === 'mpeg' || $ftype === 'avi' || |
||
| 134 | $ftype === 'mp3' || $ftype === 'mp4') { |
||
| 135 | $fhandler=$avHandler; |
||
| 136 | |||
| 137 | } else { |
||
| 138 | if ($ftype === 'doc' || $ftype === 'docx' || $ftype === 'odt' || $ftype === 'txt') { |
||
| 139 | $officeApp = "writer"; |
||
| 140 | |||
| 141 | } elseif ($ftype === 'ppt' || $ftype === 'pptx' || $ftype === 'pps' || $ftype === 'ppsx' || $ftype === 'odp') { |
||
| 142 | $officeApp = "impress"; |
||
| 143 | |||
| 144 | } elseif ($ftype === 'xls' || $ftype === 'xlsx' || $ftype === 'ods') { |
||
| 145 | $officeApp = "calc"; |
||
| 146 | } |
||
| 147 | $convertedFile = convertOffice($file, $officeApp, $fdir, $fname); |
||
| 148 | if ($convertedFile) { |
||
| 149 | $file = $convertedFile; |
||
| 150 | $fhandler = $pdfHandler; |
||
| 151 | } else { |
||
| 152 | $fhandler = "/usr/bin/libreoffice --'$officeApp' --nologo --norestore -o"; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | /* |
||
| 157 | alternatively with mime-types |
||
| 158 | |||
| 159 | // $ftype = mime_content_type($this->UPLOAD_PATH.$file); |
||
| 160 | // if($ftype=='application/pdf') |
||
| 161 | // if($ftype=='image/gif' || $ftype=='image/jpg' || $ftype=='image/png' ) |
||
| 162 | // if($ftype=='html' || $ftype=='url' || $ftype="text/plain") |
||
| 163 | // (...) |
||
| 164 | |||
| 165 | */ |
||
| 166 | |||
| 167 | return array($fhandler, $file); |
||
| 168 | } |
||
| 169 | } |
||
| 191 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.