| Conditions | 10 |
| Paths | 6 |
| Total Lines | 30 |
| Code Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 18 | function smarty_make_timestamp($string) |
||
| 19 | { |
||
| 20 | if (empty($string)) { |
||
| 21 | // use "now": |
||
| 22 | return time(); |
||
| 23 | } elseif ($string instanceof DateTime |
||
| 24 | || (interface_exists('DateTimeInterface', false) && $string instanceof DateTimeInterface) |
||
| 25 | ) { |
||
| 26 | return (int)$string->format('U'); // PHP 5.2 BC |
||
| 27 | } elseif (strlen($string) === 14 && ctype_digit($string)) { |
||
| 28 | // it is mysql timestamp format of YYYYMMDDHHMMSS? |
||
| 29 | return mktime( |
||
| 30 | substr($string, 8, 2), |
||
|
|
|||
| 31 | substr($string, 10, 2), |
||
| 32 | substr($string, 12, 2), |
||
| 33 | substr($string, 4, 2), |
||
| 34 | substr($string, 6, 2), |
||
| 35 | substr($string, 0, 4) |
||
| 36 | ); |
||
| 37 | } elseif (is_numeric($string)) { |
||
| 38 | // it is a numeric string, we handle it as timestamp |
||
| 39 | return (int)$string; |
||
| 40 | } else { |
||
| 41 | // strtotime should handle it |
||
| 42 | $time = strtotime($string); |
||
| 43 | if ($time === -1 || $time === false) { |
||
| 44 | // strtotime() was not able to parse $string, use "now": |
||
| 45 | return time(); |
||
| 46 | } |
||
| 47 | return $time; |
||
| 48 | } |
||
| 50 |