| Conditions | 18 |
| Paths | 18 |
| Total Lines | 54 |
| Code Lines | 29 |
| 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 |
||
| 38 | public static function guess($filePath) |
||
| 39 | { |
||
| 40 | if (!@file_exists($filePath)) { |
||
| 41 | return false; |
||
| 42 | } |
||
| 43 | /* |
||
| 44 | Not using pathinfo, as it is locale aware, and I'm not sure if that could lead to problems |
||
| 45 | |||
| 46 | if (!function_exists('pathinfo')) { |
||
| 47 | // This is really a just in case! - We do not expect this to happen. |
||
| 48 | // - in fact we have a test case asserting that this does not happen. |
||
| 49 | return null; |
||
| 50 | // |
||
| 51 | $fileExtension = pathinfo($filePath, PATHINFO_EXTENSION); |
||
| 52 | $fileExtension = strtolower($fileExtension); |
||
| 53 | }*/ |
||
| 54 | |||
| 55 | $result = preg_match('#\\.([^.]*)$#', $filePath, $matches); |
||
| 56 | if ($result !== 1) { |
||
| 57 | return null; |
||
| 58 | } |
||
| 59 | $fileExtension = $matches[1]; |
||
| 60 | |||
| 61 | switch ($fileExtension) { |
||
| 62 | case 'bmp': |
||
| 63 | case 'gif': |
||
| 64 | case 'jpeg': |
||
| 65 | case 'png': |
||
| 66 | case 'tiff': |
||
| 67 | case 'webp': |
||
| 68 | return 'image/' . $fileExtension; |
||
| 69 | |||
| 70 | case 'ico': |
||
| 71 | return 'image/vnd.microsoft.icon'; // or perhaps 'x-icon' ? |
||
| 72 | |||
| 73 | case 'jpg': |
||
| 74 | return 'image/jpeg'; |
||
| 75 | |||
| 76 | case 'svg': |
||
| 77 | return 'image/svg+xml'; |
||
| 78 | |||
| 79 | case 'tif': |
||
| 80 | return 'image/tiff'; |
||
| 81 | |||
| 82 | case 'txt': |
||
| 83 | case 'doc': |
||
| 84 | case 'exe': |
||
| 85 | case 'zip': |
||
| 86 | case 'gz': |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | |||
| 90 | // We do not know this extension, return null |
||
| 91 | return null; |
||
| 92 | } |
||
| 95 |