Conditions | 17 |
Paths | 595 |
Total Lines | 120 |
Code Lines | 77 |
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 |
||
24 | function visualize($userId) { |
||
25 | global $template_result; |
||
26 | |||
27 | $activityPeriod = PERIOD_HOUR * 1; |
||
28 | // $activityPeriod = PERIOD_MINUTE_10; |
||
29 | |||
30 | $userIdSafe = round(floatval($userId)); |
||
31 | |||
32 | $iter = SN::$gc->db->selectIterator( |
||
33 | "SELECT c.visit_time, c.visit_length, c.counter_id |
||
34 | FROM `{{counter}}` as c |
||
35 | where |
||
36 | user_id={$userIdSafe} |
||
37 | AND visit_time > '2018-01-01' |
||
38 | order by visit_time desc;" |
||
39 | ); |
||
40 | |||
41 | $user = SN::$db->doQueryAndFetch("SELECT `username` FROM `{{users}}` WHERE `id` = {$userIdSafe}"); |
||
42 | |||
43 | $template_result += [ |
||
44 | 'RECORDS' => count($iter), |
||
45 | 'USER_ID' => $userId, |
||
46 | 'USER_NAME' => $user['username'], |
||
47 | ]; |
||
48 | |||
49 | if (!count($iter)) { |
||
50 | return; |
||
51 | } |
||
52 | |||
53 | $from = null; |
||
54 | $to = null; |
||
55 | |||
56 | $perHour = []; |
||
57 | foreach ($iter as $record) { |
||
58 | empty($to) ? $to = $record['visit_time'] : false; |
||
59 | |||
60 | $from = $record['visit_time']; |
||
61 | |||
62 | $time = strtotime($record['visit_time']); |
||
63 | $hourStart = floor($time / $activityPeriod) * $activityPeriod; |
||
64 | |||
65 | $length = $record['visit_length']; |
||
66 | $length == 0 ? $length = 1 : false; |
||
67 | |||
68 | do { |
||
69 | $leftOfThisHour = $hourStart + $activityPeriod - $time; |
||
70 | |||
71 | if ($length < $leftOfThisHour) { |
||
72 | $spendOnThisHour = $length; |
||
73 | $length = 0; |
||
74 | } else { |
||
75 | $spendOnThisHour = $leftOfThisHour; |
||
76 | $length -= $leftOfThisHour; |
||
77 | } |
||
78 | $perHour[$hourStart] += $spendOnThisHour; |
||
79 | |||
80 | $hourStart += $activityPeriod; |
||
81 | } while ($length > 0); |
||
82 | |||
83 | } |
||
84 | |||
85 | $template_result += [ |
||
86 | 'PERIOD' => $activityPeriod, |
||
87 | |||
88 | 'DATE_FROM' => $from, |
||
89 | 'DATE_TO' => $to, |
||
90 | ]; |
||
91 | |||
92 | ksort($perHour); |
||
93 | |||
94 | end($perHour); |
||
95 | $lastHour = key($perHour); |
||
96 | |||
97 | reset($perHour); |
||
98 | $firstHour = key($perHour); |
||
99 | $thisHour = $firstHour; |
||
100 | |||
101 | do { |
||
102 | if (empty($perHour[$thisHour])) { |
||
103 | $perHour[$thisHour] = 0; |
||
104 | } |
||
105 | $thisHour += $activityPeriod; |
||
106 | } while ($thisHour < $lastHour); |
||
107 | |||
108 | krsort($perHour); |
||
109 | |||
110 | end($perHour); |
||
111 | $lastHour = key($perHour); |
||
112 | |||
113 | $dayOpened = null; |
||
114 | $toTemplate = []; |
||
115 | foreach ($perHour as $hour => $length) { |
||
116 | $openDay = false; |
||
117 | $closeDay = false; |
||
118 | |||
119 | if (!$dayOpened) { |
||
120 | $openDay = true; |
||
121 | $dayOpened = 1; |
||
122 | } |
||
123 | |||
124 | if ($dayOpened && (date('H', $hour) == 0 || $hour == $lastHour)) { |
||
125 | $closeDay = true; |
||
126 | $dayOpened = 0; |
||
127 | } |
||
128 | |||
129 | $lengthPercent = $length / $activityPeriod * 100; |
||
130 | $toTemplate[] = [ |
||
131 | 'TIME' => date(FMT_TIME, $hour), |
||
132 | 'LENGTH' => $length, |
||
133 | 'LENGTH_PERCENT' => $lengthPercent > 100 ? 100 : $lengthPercent, |
||
134 | 'MINUTES' => round($length / PERIOD_MINUTE, 1), // unused? |
||
135 | 'TIME_CLASS' => $length ? 'present' : 'none', |
||
136 | |||
137 | 'OPEN_DAY' => $openDay, |
||
138 | 'CLOSE_DAY' => $closeDay, |
||
139 | 'DATE' => date(FMT_DATE, $hour), |
||
140 | 'DAY_CLASS' => in_array(date('w', $hour), [0, 6]) ? 'weekend' : '', |
||
141 | ]; |
||
142 | |||
143 | $template_result['.']['hourly'] = $toTemplate; |
||
144 | } |
||
147 |