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