| Conditions | 12 |
| Paths | 11 |
| Total Lines | 35 |
| Code Lines | 23 |
| 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 |
||
| 27 | function smarty_modifier_truncate($string, $length = 80, $etc = '...', $break_words = false, $middle = false) |
||
| 28 | { |
||
| 29 | if ($length === 0 || $string === null) { |
||
| 30 | return ''; |
||
| 31 | } |
||
| 32 | if (Smarty::$_MBSTRING) { |
||
| 33 | if (mb_strlen($string, Smarty::$_CHARSET) > $length) { |
||
| 34 | $length -= min($length, mb_strlen($etc, Smarty::$_CHARSET)); |
||
| 35 | if (!$break_words && !$middle) { |
||
| 36 | $string = preg_replace( |
||
| 37 | '/\s+?(\S+)?$/' . Smarty::$_UTF8_MODIFIER, |
||
| 38 | '', |
||
| 39 | mb_substr($string, 0, $length + 1, Smarty::$_CHARSET) |
||
| 40 | ); |
||
| 41 | } |
||
| 42 | if (!$middle) { |
||
| 43 | return mb_substr($string, 0, $length, Smarty::$_CHARSET) . $etc; |
||
| 44 | } |
||
| 45 | return mb_substr($string, 0, intval($length / 2), Smarty::$_CHARSET) . $etc . |
||
| 46 | mb_substr($string, -intval($length / 2), $length, Smarty::$_CHARSET); |
||
| 47 | } |
||
| 48 | return $string; |
||
| 49 | } |
||
| 50 | // no MBString fallback |
||
| 51 | if (isset($string[ $length ])) { |
||
| 52 | $length -= min($length, strlen($etc)); |
||
| 53 | if (!$break_words && !$middle) { |
||
| 54 | $string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length + 1)); |
||
| 55 | } |
||
| 56 | if (!$middle) { |
||
| 57 | return substr($string, 0, $length) . $etc; |
||
| 58 | } |
||
| 59 | return substr($string, 0, intval($length / 2)) . $etc . substr($string, -intval($length / 2)); |
||
| 60 | } |
||
| 61 | return $string; |
||
| 62 | } |
||
| 63 |