| Conditions | 32 |
| Paths | 676 |
| Total Lines | 117 |
| Code Lines | 90 |
| 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 |
||
| 37 | function smarty_function_html_options($params, Smarty_Internal_Template $template) |
||
| 38 | { |
||
| 39 | $template->_checkPlugins( |
||
| 40 | array( |
||
| 41 | array( |
||
| 42 | 'function' => 'smarty_function_escape_special_chars', |
||
| 43 | 'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php' |
||
| 44 | ) |
||
| 45 | ) |
||
| 46 | ); |
||
| 47 | $name = null; |
||
| 48 | $values = null; |
||
| 49 | $options = null; |
||
| 50 | $selected = null; |
||
| 51 | $output = null; |
||
| 52 | $id = null; |
||
| 53 | $class = null; |
||
| 54 | $extra = ''; |
||
| 55 | foreach ($params as $_key => $_val) { |
||
| 56 | switch ($_key) { |
||
| 57 | case 'name': |
||
| 58 | case 'class': |
||
| 59 | case 'id': |
||
| 60 | $$_key = (string)$_val; |
||
| 61 | break; |
||
| 62 | case 'options': |
||
| 63 | $options = (array)$_val; |
||
| 64 | break; |
||
| 65 | case 'values': |
||
| 66 | case 'output': |
||
| 67 | $$_key = array_values((array)$_val); |
||
| 68 | break; |
||
| 69 | case 'selected': |
||
| 70 | if (is_array($_val)) { |
||
| 71 | $selected = array(); |
||
| 72 | foreach ($_val as $_sel) { |
||
| 73 | if (is_object($_sel)) { |
||
| 74 | if (method_exists($_sel, '__toString')) { |
||
| 75 | $_sel = smarty_function_escape_special_chars((string)$_sel->__toString()); |
||
| 76 | } else { |
||
| 77 | trigger_error( |
||
| 78 | 'html_options: selected attribute contains an object of class \'' . |
||
| 79 | get_class($_sel) . '\' without __toString() method', |
||
| 80 | E_USER_NOTICE |
||
| 81 | ); |
||
| 82 | continue; |
||
| 83 | } |
||
| 84 | } else { |
||
| 85 | $_sel = smarty_function_escape_special_chars((string)$_sel); |
||
| 86 | } |
||
| 87 | $selected[ $_sel ] = true; |
||
| 88 | } |
||
| 89 | } elseif (is_object($_val)) { |
||
| 90 | if (method_exists($_val, '__toString')) { |
||
| 91 | $selected = smarty_function_escape_special_chars((string)$_val->__toString()); |
||
| 92 | } else { |
||
| 93 | trigger_error( |
||
| 94 | 'html_options: selected attribute is an object of class \'' . get_class($_val) . |
||
| 95 | '\' without __toString() method', |
||
| 96 | E_USER_NOTICE |
||
| 97 | ); |
||
| 98 | } |
||
| 99 | } else { |
||
| 100 | $selected = smarty_function_escape_special_chars((string)$_val); |
||
| 101 | } |
||
| 102 | break; |
||
| 103 | case 'strict': |
||
| 104 | break; |
||
| 105 | case 'disabled': |
||
| 106 | case 'readonly': |
||
| 107 | if (!empty($params[ 'strict' ])) { |
||
| 108 | if (!is_scalar($_val)) { |
||
| 109 | trigger_error( |
||
| 110 | "html_options: {$_key} attribute must be a scalar, only boolean true or string '{$_key}' will actually add the attribute", |
||
| 111 | E_USER_NOTICE |
||
| 112 | ); |
||
| 113 | } |
||
| 114 | if ($_val === true || $_val === $_key) { |
||
| 115 | $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_key) . '"'; |
||
| 116 | } |
||
| 117 | break; |
||
| 118 | } |
||
| 119 | // omit break; to fall through! |
||
| 120 | // no break |
||
| 121 | default: |
||
| 122 | if (!is_array($_val)) { |
||
| 123 | $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"'; |
||
| 124 | } else { |
||
| 125 | trigger_error("html_options: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE); |
||
| 126 | } |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | if (!isset($options) && !isset($values)) { |
||
| 131 | /* raise error here? */ |
||
| 132 | return ''; |
||
| 133 | } |
||
| 134 | $_html_result = ''; |
||
| 135 | $_idx = 0; |
||
| 136 | if (isset($options)) { |
||
| 137 | foreach ($options as $_key => $_val) { |
||
| 138 | $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx); |
||
| 139 | } |
||
| 140 | } else { |
||
| 141 | foreach ($values as $_i => $_key) { |
||
| 142 | $_val = isset($output[ $_i ]) ? $output[ $_i ] : ''; |
||
| 143 | $_html_result .= smarty_function_html_options_optoutput($_key, $_val, $selected, $id, $class, $_idx); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | if (!empty($name)) { |
||
|
|
|||
| 147 | $_html_class = !empty($class) ? ' class="' . $class . '"' : ''; |
||
| 148 | $_html_id = !empty($id) ? ' id="' . $id . '"' : ''; |
||
| 149 | $_html_result = |
||
| 150 | '<select name="' . $name . '"' . $_html_class . $_html_id . $extra . '>' . "\n" . $_html_result . |
||
| 151 | '</select>' . "\n"; |
||
| 152 | } |
||
| 153 | return $_html_result; |
||
| 154 | } |
||
| 231 |