| Conditions | 11 |
| Paths | 9 |
| Total Lines | 31 |
| Code Lines | 11 |
| 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 |
||
| 18 | public static function get($default) : callable { |
||
| 19 | |||
| 20 | # Check basic types |
||
| 21 | |||
| 22 | if (is_string ($default)) return function(string $value) { return $value; }; |
||
| 23 | |||
| 24 | if (is_bool ($default)) return function(bool $value) { return $value; }; |
||
| 25 | |||
| 26 | if (is_int ($default)) return function(int $value) { return $value; }; |
||
| 27 | |||
| 28 | if (is_float ($default)) return function(float $value) { return $value; }; |
||
| 29 | |||
| 30 | if (is_array ($default)) return function(array $value) { return $value; }; |
||
| 31 | |||
| 32 | if (is_callable ($default)) return function(callable $value) { return $value; }; |
||
| 33 | |||
| 34 | # Check object type |
||
| 35 | |||
| 36 | if (is_object($default)) return function($value) use($default) { |
||
| 37 | |||
| 38 | return (is_a($value, get_class($default)) ? $value : null); |
||
| 39 | }; |
||
| 40 | |||
| 41 | # Check resource type |
||
| 42 | |||
| 43 | if (is_resource($default)) return function($value) { return (is_resource($value) ? $value : null); }; |
||
| 44 | |||
| 45 | # ------------------------ |
||
| 46 | |||
| 47 | return function() { return null; }; |
||
| 48 | } |
||
| 49 | } |
||
| 51 |