Conditions | 12 |
Paths | 18 |
Total Lines | 48 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
47 | function sys_schedule_get_prev_run($scheduleList, $recorded_run = SN_TIME_NOW, $return_next_run = false) |
||
48 | { |
||
49 | static $date_part_names_reverse = array('seconds', 'minutes', 'hours', 'days', 'months', 'years',); |
||
50 | |||
51 | $possible_schedules = array(); |
||
52 | |||
53 | $recorded_run = strtotime($recorded_run); |
||
54 | |||
55 | $prev_run_array = getdate($recorded_run); |
||
56 | $prev_run_array = array($prev_run_array['seconds'],$prev_run_array['minutes'],$prev_run_array['hours'],$prev_run_array['mday'],$prev_run_array['mon'],$prev_run_array['year']); |
||
57 | $today_array = getdate(SN_TIME_NOW); |
||
58 | $today_array = array($today_array['seconds'],$today_array['minutes'],$today_array['hours'],$today_array['mday'],$today_array['mon'],$today_array['year']); |
||
59 | $scheduleList = explode(',', $scheduleList); |
||
60 | array_walk($scheduleList, function(&$schedule) use ($prev_run_array, $today_array, $date_part_names_reverse, &$possible_schedules) { |
||
61 | $schedule = array('schedule_array' => array_reverse(explode(':', trim($schedule)))); |
||
62 | |||
63 | $interval = $date_part_names_reverse[count($schedule['schedule_array'])]; |
||
64 | |||
65 | foreach($prev_run_array as $index => $date_part) { |
||
66 | $schedule['array']['recorded'][$index] = isset($schedule['schedule_array'][$index]) ? intval($schedule['schedule_array'][$index]) : $date_part; |
||
67 | $schedule['array']['now'][$index] = isset($schedule['schedule_array'][$index]) ? intval($schedule['schedule_array'][$index]) : $today_array[$index]; |
||
68 | } |
||
69 | if($schedule['array']['recorded'] == $schedule['array']['now']) { |
||
70 | unset($schedule['array']['now']); |
||
71 | } |
||
72 | |||
73 | foreach($schedule['array'] as $name => $array) { |
||
74 | $schedule['string'][$name] = "{$array[5]}-{$array[4]}-{$array[3]} {$array[2]}:{$array[1]}:{$array[0]}"; |
||
75 | $schedule['string'][$name . '_next'] = $schedule['string'][$name] . ' +1 ' . $interval; |
||
76 | $schedule['string'][$name . '_prev'] = $schedule['string'][$name] . ' -1 ' . $interval; |
||
77 | } |
||
78 | |||
79 | foreach($schedule['string'] as $string) { |
||
80 | $timestamp = strtotime($string); |
||
81 | $schedule['timestamp'][$timestamp] = $possible_schedules[$timestamp] = date(FMT_DATE_TIME_SQL, strtotime($string)); |
||
82 | } |
||
83 | }); |
||
84 | |||
85 | ksort($possible_schedules); |
||
86 | |||
87 | $prev_run = 0; |
||
88 | $next_run = 0; |
||
89 | foreach($possible_schedules as $timestamp => $string_date) { |
||
90 | $prev_run = SN_TIME_NOW >= $timestamp ? $timestamp : $prev_run; |
||
91 | $next_run = SN_TIME_NOW < $timestamp && !$next_run ? $timestamp : $next_run; |
||
92 | } |
||
93 | |||
94 | return $return_next_run ? $next_run : $prev_run; |
||
95 | } |
||
96 |