Conditions | 34 |
Paths | 7753 |
Total Lines | 114 |
Code Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 1190 |
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 |
||
49 | function smarty_function_html_table($params, &$smarty) |
||
50 | { |
||
51 | $table_attr = 'border="1"'; |
||
52 | $tr_attr = ''; |
||
53 | $th_attr = ''; |
||
54 | $td_attr = ''; |
||
55 | $cols = $cols_count = 3; |
||
56 | $rows = 3; |
||
57 | $trailpad = ' '; |
||
58 | $vdir = 'down'; |
||
59 | $hdir = 'right'; |
||
60 | $inner = 'cols'; |
||
61 | $caption = ''; |
||
62 | |||
63 | if (!isset($params['loop'])) { |
||
64 | $smarty->trigger_error("html_table: missing 'loop' parameter"); |
||
65 | return; |
||
66 | } |
||
67 | |||
68 | foreach ($params as $_key=>$_value) { |
||
69 | switch ($_key) { |
||
70 | case 'loop': |
||
71 | $$_key = (array)$_value; |
||
72 | break; |
||
73 | |||
74 | case 'cols': |
||
75 | if (is_array($_value) && !empty($_value)) { |
||
76 | $cols = $_value; |
||
77 | $cols_count = count($_value); |
||
78 | } elseif (!is_numeric($_value) && is_string($_value) && !empty($_value)) { |
||
79 | $cols = explode(',', $_value); |
||
80 | $cols_count = count($cols); |
||
81 | } elseif (!empty($_value)) { |
||
82 | $cols_count = (int)$_value; |
||
83 | } else { |
||
84 | $cols_count = $cols; |
||
85 | } |
||
86 | break; |
||
87 | |||
88 | case 'rows': |
||
89 | $$_key = (int)$_value; |
||
90 | break; |
||
91 | |||
92 | case 'table_attr': |
||
93 | case 'trailpad': |
||
94 | case 'hdir': |
||
95 | case 'vdir': |
||
96 | case 'inner': |
||
97 | case 'caption': |
||
98 | $$_key = (string)$_value; |
||
99 | break; |
||
100 | |||
101 | case 'tr_attr': |
||
102 | case 'td_attr': |
||
103 | case 'th_attr': |
||
104 | $$_key = $_value; |
||
105 | break; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | $loop_count = count($loop); |
||
110 | if (empty($params['rows'])) { |
||
111 | /* no rows specified */ |
||
112 | $rows = ceil($loop_count/$cols_count); |
||
113 | } elseif (empty($params['cols'])) { |
||
114 | if (!empty($params['rows'])) { |
||
115 | /* no cols specified, but rows */ |
||
116 | $cols_count = ceil($loop_count/$rows); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | $output = "<table $table_attr>\n"; |
||
121 | |||
122 | if (!empty($caption)) { |
||
123 | $output .= '<caption>' . $caption . "</caption>\n"; |
||
124 | } |
||
125 | |||
126 | if (is_array($cols)) { |
||
127 | $cols = ($hdir == 'right') ? $cols : array_reverse($cols); |
||
128 | $output .= "<thead><tr>\n"; |
||
129 | |||
130 | for ($r=0; $r<$cols_count; $r++) { |
||
131 | $output .= '<th' . smarty_function_html_table_cycle('th', $th_attr, $r) . '>'; |
||
132 | $output .= $cols[$r]; |
||
133 | $output .= "</th>\n"; |
||
134 | } |
||
135 | $output .= "</tr></thead>\n"; |
||
136 | } |
||
137 | |||
138 | $output .= "<tbody>\n"; |
||
139 | for ($r=0; $r<$rows; $r++) { |
||
140 | $output .= "<tr" . smarty_function_html_table_cycle('tr', $tr_attr, $r) . ">\n"; |
||
141 | $rx = ($vdir == 'down') ? $r*$cols_count : ($rows-1-$r)*$cols_count; |
||
142 | |||
143 | for ($c=0; $c<$cols_count; $c++) { |
||
144 | $x = ($hdir == 'right') ? $rx+$c : $rx+$cols_count-1-$c; |
||
145 | if ($inner!='cols') { |
||
146 | /* shuffle x to loop over rows*/ |
||
147 | $x = floor($x/$cols_count) + ($x%$cols_count)*$rows; |
||
148 | } |
||
149 | |||
150 | if ($x<$loop_count) { |
||
151 | $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">" . $loop[$x] . "</td>\n"; |
||
152 | } else { |
||
153 | $output .= "<td" . smarty_function_html_table_cycle('td', $td_attr, $c) . ">$trailpad</td>\n"; |
||
154 | } |
||
155 | } |
||
156 | $output .= "</tr>\n"; |
||
157 | } |
||
158 | $output .= "</tbody>\n"; |
||
159 | $output .= "</table>\n"; |
||
160 | |||
161 | return $output; |
||
162 | } |
||
163 | |||
178 |