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