| Conditions | 21 |
| Paths | 1200 |
| Total Lines | 87 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| Bugs | 1 | 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 |
||
| 94 | public static function create($name, $selected = 'UTC', $opts = []) |
||
| 95 | { |
||
| 96 | //handle a null selected |
||
| 97 | if (empty($selected)) { |
||
| 98 | $selected = 'UTC'; |
||
| 99 | } |
||
| 100 | |||
| 101 | // Attributes for select element |
||
| 102 | $attrSet = ''; |
||
| 103 | if (isset($opts['attr']) && is_array($opts['attr']) && !empty($opts['attr'])) { |
||
| 104 | $attr = $opts['attr']; |
||
| 105 | |||
| 106 | foreach ($attr as $attr_name => $attr_value) { |
||
| 107 | $attrSet .= ' '.$attr_name.'="'.$attr_value.'"'; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | //setup grouping |
||
| 112 | $with_regions = false; |
||
| 113 | if (isset($opts['with_regions']) && is_bool($opts['with_regions']) && $opts['with_regions']) { |
||
| 114 | $with_regions = true; |
||
| 115 | } |
||
| 116 | |||
| 117 | $limit_regions = []; |
||
| 118 | //setup specfic regions - could be better and validate them here too, but, eh... |
||
| 119 | if (isset($opts['regions']) && is_array($opts['regions']) && !empty($opts['regions'])) { |
||
| 120 | $limit_regions = $opts['regions']; |
||
| 121 | } |
||
| 122 | |||
| 123 | // start select element |
||
| 124 | $listbox = '<select name="'.$name.'"'.$attrSet.'>'; |
||
| 125 | |||
| 126 | $regions = []; |
||
| 127 | if ($with_regions) { |
||
| 128 | $regions = self::$regions; |
||
| 129 | } elseif (!empty($limit_regions)) { |
||
| 130 | foreach ($limit_regions as $region) { |
||
| 131 | $regions[$region] = self::$regions[$region]; |
||
| 132 | } |
||
| 133 | } else { |
||
| 134 | $regions = ['All'=>DateTimeZone::ALL]; |
||
| 135 | } |
||
| 136 | // Add all timezones of the regions |
||
| 137 | // depending on with_regions, this may be one or muiltiple loops |
||
| 138 | foreach ($regions as $continent => $mask) { |
||
| 139 | $opts = []; |
||
| 140 | $timezones = DateTimeZone::listIdentifiers($mask); |
||
| 141 | |||
| 142 | if ($with_regions) { |
||
| 143 | // start optgroup tag |
||
| 144 | $listbox .= '<optgroup label="'.$continent.'">'; |
||
| 145 | } else { |
||
| 146 | //when including everything, don't let formatTimeZone truncate the label. |
||
| 147 | $continent = null; |
||
| 148 | } |
||
| 149 | |||
| 150 | // create option tags |
||
| 151 | $offsets = []; |
||
| 152 | $labels = []; |
||
| 153 | foreach ($timezones as $timezone) { |
||
| 154 | $opt = self::formatTimezone($timezone, $continent); |
||
| 155 | $opt['tz'] = $timezone; |
||
| 156 | $opt['selected'] = ($selected == $timezone) ? ' selected="selected"' : ''; |
||
| 157 | $offsets[$timezone] = $opt['offset']; |
||
| 158 | $labels[$timezone] = $opt['label']; |
||
| 159 | $opts[] = $opt; |
||
| 160 | } |
||
| 161 | |||
| 162 | array_multisort($offsets, SORT_DESC, |
||
|
|
|||
| 163 | $labels, SORT_ASC, $opts |
||
| 164 | ); |
||
| 165 | |||
| 166 | foreach ($opts as $opt) { |
||
| 167 | $listbox .= '<option value="'.$opt['tz'].'"'.$opt['selected'].'>'; |
||
| 168 | $listbox .= $opt['label']; |
||
| 169 | $listbox .= '</option>'; |
||
| 170 | } |
||
| 171 | if ($with_regions) { |
||
| 172 | // end optgroup tag |
||
| 173 | $listbox .= '</optgroup>'; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | // end select element |
||
| 178 | $listbox .= '</select>'; |
||
| 179 | |||
| 180 | return $listbox; |
||
| 181 | } |
||
| 250 |