smarty_function_html_select_time()   F
last analyzed

Complexity

Conditions 39
Paths > 20000

Size

Total Lines 166
Code Lines 125

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 125
dl 0
loc 166
rs 0
c 0
b 0
f 0
cc 39
nc 408969
nop 2

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Smarty plugin
4
 * @package Smarty
5
 * @subpackage plugins
6
 */
7
8
9
/**
10
 * Smarty {html_select_time} function plugin
11
 *
12
 * Type:     function<br>
13
 * Name:     html_select_time<br>
14
 * Purpose:  Prints the dropdowns for time selection
15
 * @link http://smarty.php.net/manual/en/language.function.html.select.time.php {html_select_time}
16
 *          (Smarty online manual)
17
 * @author Roberto Berto <[email protected]>
18
 * @credits Monte Ohrt <monte AT ohrt DOT com>
19
 * @param array
20
 * @param Smarty
21
 * @return string
22
 * @uses smarty_make_timestamp()
23
 */
24
function smarty_function_html_select_time($params, &$smarty)
25
{
26
    require_once $smarty->_get_plugin_filepath('shared','make_timestamp');
27
    require_once $smarty->_get_plugin_filepath('function','html_options');
28
    /* Default values. */
29
    $prefix             = "Time_";
30
    $time               = time();
31
    $display_hours      = true;
32
    $display_minutes    = true;
33
    $display_seconds    = true;
34
    $display_meridian   = true;
35
    $use_24_hours       = true;
36
    $minute_interval    = 1;
37
    $second_interval    = 1;
38
    /* Should the select boxes be part of an array when returned from PHP?
39
       e.g. setting it to "birthday", would create "birthday[Hour]",
40
       "birthday[Minute]", "birthday[Seconds]" & "birthday[Meridian]".
41
       Can be combined with prefix. */
42
    $field_array        = null;
43
    $all_extra          = null;
44
    $hour_extra         = null;
45
    $minute_extra       = null;
46
    $second_extra       = null;
47
    $meridian_extra     = null;
48
49
    foreach ($params as $_key=>$_value) {
50
        switch ($_key) {
51
            case 'prefix':
52
            case 'time':
53
            case 'field_array':
54
            case 'all_extra':
55
            case 'hour_extra':
56
            case 'minute_extra':
57
            case 'second_extra':
58
            case 'meridian_extra':
59
                $$_key = (string)$_value;
60
                break;
61
62
            case 'display_hours':
63
            case 'display_minutes':
64
            case 'display_seconds':
65
            case 'display_meridian':
66
            case 'use_24_hours':
67
                $$_key = (bool)$_value;
68
                break;
69
70
            case 'minute_interval':
71
            case 'second_interval':
72
                $$_key = (int)$_value;
73
                break;
74
75
            default:
76
                $smarty->trigger_error("[html_select_time] unknown parameter $_key", E_USER_WARNING);
77
        }
78
    }
79
80
    $time = smarty_make_timestamp($time);
81
82
    $html_result = '';
83
84
    if ($display_hours) {
0 ignored issues
show
introduced by beckmi
The condition $display_hours is always true.
Loading history...
85
        $hours       = $use_24_hours ? range(0, 23) : range(1, 12);
0 ignored issues
show
introduced by geekwright
The condition $use_24_hours is always true.
Loading history...
86
        $hour_fmt = $use_24_hours ? '%H' : '%I';
0 ignored issues
show
introduced by beckmi
The condition $use_24_hours is always true.
Loading history...
87
        for ($i = 0, $for_max = count($hours); $i < $for_max; $i++)
88
            $hours[$i] = sprintf('%02d', $hours[$i]);
89
        $html_result .= '<select name=';
90
        if (null !== $field_array) {
0 ignored issues
show
introduced by beckmi
The condition null !== $field_array is always false.
Loading history...
91
            $html_result .= '"' . $field_array . '[' . $prefix . 'Hour]"';
92
        } else {
93
            $html_result .= '"' . $prefix . 'Hour"';
94
        }
95
        if (null !== $hour_extra){
0 ignored issues
show
introduced by geekwright
The condition null !== $hour_extra is always false.
Loading history...
96
            $html_result .= ' ' . $hour_extra;
97
        }
98
        if (null !== $all_extra){
0 ignored issues
show
introduced by geekwright
The condition null !== $all_extra is always false.
Loading history...
99
            $html_result .= ' ' . $all_extra;
100
        }
101
        $html_result .= '>'."\n";
102
        $html_result .= smarty_function_html_options(array('output'          => $hours,
103
                                                           'values'          => $hours,
104
                                                           'selected'      => strftime($hour_fmt, $time),
0 ignored issues
show
Bug introduced by geekwright
$time of type string is incompatible with the type integer|null expected by parameter $timestamp of strftime(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

104
                                                           'selected'      => strftime($hour_fmt, /** @scrutinizer ignore-type */ $time),
Loading history...
105
                                                           'print_result' => false),
106
                                                     $smarty);
107
        $html_result .= "</select>\n";
108
    }
109
110
    if ($display_minutes) {
0 ignored issues
show
introduced by beckmi
The condition $display_minutes is always true.
Loading history...
111
        $all_minutes = range(0, 59);
112
        for ($i = 0, $for_max = count($all_minutes); $i < $for_max; $i+= $minute_interval)
113
            $minutes[] = sprintf('%02d', $all_minutes[$i]);
114
        $selected = intval(floor(strftime('%M', $time) / $minute_interval) * $minute_interval);
115
        $html_result .= '<select name=';
116
        if (null !== $field_array) {
0 ignored issues
show
introduced by beckmi
The condition null !== $field_array is always false.
Loading history...
117
            $html_result .= '"' . $field_array . '[' . $prefix . 'Minute]"';
118
        } else {
119
            $html_result .= '"' . $prefix . 'Minute"';
120
        }
121
        if (null !== $minute_extra){
0 ignored issues
show
introduced by geekwright
The condition null !== $minute_extra is always false.
Loading history...
122
            $html_result .= ' ' . $minute_extra;
123
        }
124
        if (null !== $all_extra){
0 ignored issues
show
introduced by geekwright
The condition null !== $all_extra is always false.
Loading history...
125
            $html_result .= ' ' . $all_extra;
126
        }
127
        $html_result .= '>'."\n";
128
        
129
        $html_result .= smarty_function_html_options(array('output'          => $minutes,
0 ignored issues
show
Comprehensibility Best Practice introduced by geekwright
The variable $minutes does not seem to be defined for all execution paths leading up to this point.
Loading history...
130
                                                           'values'          => $minutes,
131
                                                           'selected'      => $selected,
132
                                                           'print_result' => false),
133
                                                     $smarty);
134
        $html_result .= "</select>\n";
135
    }
136
137
    if ($display_seconds) {
0 ignored issues
show
introduced by beckmi
The condition $display_seconds is always true.
Loading history...
138
        $all_seconds = range(0, 59);
139
        for ($i = 0, $for_max = count($all_seconds); $i < $for_max; $i+= $second_interval)
140
            $seconds[] = sprintf('%02d', $all_seconds[$i]);
141
        $selected = intval(floor(strftime('%S', $time) / $second_interval) * $second_interval);
142
        $html_result .= '<select name=';
143
        if (null !== $field_array) {
0 ignored issues
show
introduced by beckmi
The condition null !== $field_array is always false.
Loading history...
144
            $html_result .= '"' . $field_array . '[' . $prefix . 'Second]"';
145
        } else {
146
            $html_result .= '"' . $prefix . 'Second"';
147
        }
148
        
149
        if (null !== $second_extra){
0 ignored issues
show
introduced by geekwright
The condition null !== $second_extra is always false.
Loading history...
150
            $html_result .= ' ' . $second_extra;
151
        }
152
        if (null !== $all_extra){
0 ignored issues
show
introduced by geekwright
The condition null !== $all_extra is always false.
Loading history...
153
            $html_result .= ' ' . $all_extra;
154
        }
155
        $html_result .= '>'."\n";
156
        
157
        $html_result .= smarty_function_html_options(array('output'          => $seconds,
0 ignored issues
show
Comprehensibility Best Practice introduced by geekwright
The variable $seconds does not seem to be defined for all execution paths leading up to this point.
Loading history...
158
                                                           'values'          => $seconds,
159
                                                           'selected'      => $selected,
160
                                                           'print_result' => false),
161
                                                     $smarty);
162
        $html_result .= "</select>\n";
163
    }
164
165
    if ($display_meridian && !$use_24_hours) {
0 ignored issues
show
introduced by beckmi
The condition $use_24_hours is always true.
Loading history...
166
        $html_result .= '<select name=';
167
        if (null !== $field_array) {
168
            $html_result .= '"' . $field_array . '[' . $prefix . 'Meridian]"';
169
        } else {
170
            $html_result .= '"' . $prefix . 'Meridian"';
171
        }
172
        
173
        if (null !== $meridian_extra){
174
            $html_result .= ' ' . $meridian_extra;
175
        }
176
        if (null !== $all_extra){
177
            $html_result .= ' ' . $all_extra;
178
        }
179
        $html_result .= '>'."\n";
180
        
181
        $html_result .= smarty_function_html_options(array('output'          => array('AM', 'PM'),
182
                                                           'values'          => array('am', 'pm'),
183
                                                           'selected'      => strtolower(strftime('%p', $time)),
184
                                                           'print_result' => false),
185
                                                     $smarty);
186
        $html_result .= "</select>\n";
187
    }
188
189
    return $html_result;
190
}
191
192
/* vim: set expandtab: */
193
194
?>
0 ignored issues
show
Best Practice introduced by geekwright
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
195