| Conditions | 17 |
| Paths | 241 |
| Total Lines | 70 |
| Code Lines | 53 |
| 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 |
||
| 34 | public function handle($params, $content, Template $template, &$repeat) { |
||
| 35 | if (is_null($content)) { |
||
| 36 | return; |
||
| 37 | } |
||
| 38 | $style = null; |
||
| 39 | $indent = 0; |
||
| 40 | $indent_first = 0; |
||
| 41 | $indent_char = ' '; |
||
| 42 | $wrap = 80; |
||
| 43 | $wrap_char = "\n"; |
||
| 44 | $wrap_cut = false; |
||
| 45 | $assign = null; |
||
| 46 | foreach ($params as $_key => $_val) { |
||
| 47 | switch ($_key) { |
||
| 48 | case 'style': |
||
| 49 | case 'indent_char': |
||
| 50 | case 'wrap_char': |
||
| 51 | case 'assign': |
||
| 52 | $$_key = (string)$_val; |
||
| 53 | break; |
||
| 54 | case 'indent': |
||
| 55 | case 'indent_first': |
||
| 56 | case 'wrap': |
||
| 57 | $$_key = (int)$_val; |
||
| 58 | break; |
||
| 59 | case 'wrap_cut': |
||
| 60 | $$_key = (bool)$_val; |
||
| 61 | break; |
||
| 62 | default: |
||
| 63 | trigger_error("textformat: unknown attribute '{$_key}'"); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | if ($style === 'email') { |
||
|
|
|||
| 67 | $wrap = 72; |
||
| 68 | } |
||
| 69 | // split into paragraphs |
||
| 70 | $_paragraphs = preg_split('![\r\n]{2}!', $content); |
||
| 71 | foreach ($_paragraphs as &$_paragraph) { |
||
| 72 | if (!$_paragraph) { |
||
| 73 | continue; |
||
| 74 | } |
||
| 75 | // convert mult. spaces & special chars to single space |
||
| 76 | $_paragraph = |
||
| 77 | preg_replace( |
||
| 78 | array( |
||
| 79 | '!\s+!' . Smarty::$_UTF8_MODIFIER, |
||
| 80 | '!(^\s+)|(\s+$)!' . Smarty::$_UTF8_MODIFIER |
||
| 81 | ), |
||
| 82 | array( |
||
| 83 | ' ', |
||
| 84 | '' |
||
| 85 | ), |
||
| 86 | $_paragraph |
||
| 87 | ); |
||
| 88 | // indent first line |
||
| 89 | if ($indent_first > 0) { |
||
| 90 | $_paragraph = str_repeat($indent_char, $indent_first) . $_paragraph; |
||
| 91 | } |
||
| 92 | // wordwrap sentences |
||
| 93 | $_paragraph = smarty_mb_wordwrap($_paragraph, $wrap - $indent, $wrap_char, $wrap_cut); |
||
| 94 | // indent lines |
||
| 95 | if ($indent > 0) { |
||
| 96 | $_paragraph = preg_replace('!^!m', str_repeat($indent_char, $indent), $_paragraph); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | $_output = implode($wrap_char . $wrap_char, $_paragraphs); |
||
| 100 | if ($assign) { |
||
| 101 | $template->assign($assign, $_output); |
||
| 102 | } else { |
||
| 103 | return $_output; |
||
| 104 | } |
||
| 110 | } |