| Conditions | 13 |
| Paths | 16 |
| Total Lines | 56 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 defined('SYSPATH') or die('No direct access allowed.'); |
||
| 24 | public static function mime($filename) |
||
| 25 | { |
||
| 26 | // Make sure the file is readable |
||
| 27 | if (! (is_file($filename) and is_readable($filename))) { |
||
| 28 | return false; |
||
|
|
|||
| 29 | } |
||
| 30 | |||
| 31 | // Get the extension from the filename |
||
| 32 | $extension = strtolower(substr(strrchr($filename, '.'), 1)); |
||
| 33 | |||
| 34 | if (preg_match('/^(?:jpe?g|png|[gt]if|bmp|swf)$/', $extension)) { |
||
| 35 | // Disable error reporting |
||
| 36 | $ER = error_reporting(0); |
||
| 37 | |||
| 38 | // Use getimagesize() to find the mime type on images |
||
| 39 | $mime = getimagesize($filename); |
||
| 40 | |||
| 41 | // Turn error reporting back on |
||
| 42 | error_reporting($ER); |
||
| 43 | |||
| 44 | // Return the mime type |
||
| 45 | if (isset($mime['mime'])) { |
||
| 46 | return $mime['mime']; |
||
| 47 | } |
||
| 48 | } |
||
| 49 | |||
| 50 | if (function_exists('finfo_open')) { |
||
| 51 | // Use the fileinfo extension |
||
| 52 | $finfo = finfo_open(FILEINFO_MIME); |
||
| 53 | $mime = finfo_file($finfo, $filename); |
||
| 54 | finfo_close($finfo); |
||
| 55 | |||
| 56 | // Return the mime type |
||
| 57 | return $mime; |
||
| 58 | } |
||
| 59 | |||
| 60 | if (ini_get('mime_magic.magicfile') and function_exists('mime_content_type')) { |
||
| 61 | // Return the mime type using mime_content_type |
||
| 62 | return mime_content_type($filename); |
||
| 63 | } |
||
| 64 | |||
| 65 | if (! KOHANA_IS_WIN) { |
||
| 66 | // Attempt to locate use the file command, checking the return value |
||
| 67 | if ($command = trim(exec('which file', $output, $return)) and $return === 0) { |
||
| 68 | return trim(exec($command.' -bi '.escapeshellarg($filename))); |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | if (! empty($extension) and is_array($mime = Kohana::config('mimes.'.$extension))) { |
||
| 73 | // Return the mime-type guess, based on the extension |
||
| 74 | return $mime[0]; |
||
| 75 | } |
||
| 76 | |||
| 77 | // Unable to find the mime-type |
||
| 78 | return false; |
||
| 79 | } |
||
| 80 | |||
| 180 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.