| Total Complexity | 46 |
| Total Lines | 380 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Utility often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Utility, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Utility extends Common\SysUtility |
||
| 13 | { |
||
| 14 | //--------------- Custom module methods ----------------------------- |
||
| 15 | |||
| 16 | public static function getMonthlyStats($year) |
||
| 17 | { |
||
| 18 | global $xoopsDB, $xoopsTpl; |
||
| 19 | |||
| 20 | $now = date('d-m-Y'); |
||
| 21 | $dot = explode('-', $now); |
||
| 22 | $nowmonth = $dot[1]; |
||
| 23 | |||
| 24 | $l_size = getimagesize('assets/images/leftbar.gif'); |
||
| 25 | $m_size = getimagesize('assets/images/mainbar.gif'); |
||
| 26 | $r_size = getimagesize('assets/images/rightbar.gif'); |
||
| 27 | |||
| 28 | // get monthly stats and display |
||
| 29 | $resultmonth = $xoopsDB->queryF('select sum(hits) as totalhitsmonth from ' . $xoopsDB->prefix('stats_month') . " where year='$year'"); |
||
| 30 | [$totalhitsmonth] = $xoopsDB->fetchRow($resultmonth); |
||
|
|
|||
| 31 | $xoopsDB->freeRecordSet($resultmonth); |
||
| 32 | $result = $xoopsDB->queryF('select month,hits from ' . $xoopsDB->prefix('stats_month') . " where year='$year'"); |
||
| 33 | $xoopsTpl->assign('lang_stat_monthlystats', STATS_MONTHLYSTATS . ' - ' . $year); |
||
| 34 | $xoopsTpl->assign('lang_stat_monthhead', STATS_MONTH); |
||
| 35 | $resulttotal = $xoopsDB->queryF('select hits from ' . $xoopsDB->prefix('stats_year') . " where year='$year'"); |
||
| 36 | [$hitsforyear] = $xoopsDB->fetchRow($resulttotal); |
||
| 37 | $monthlist = []; |
||
| 38 | $i = 0; |
||
| 39 | while (list($month, $hits) = $xoopsDB->fetchRow($result)) { |
||
| 40 | $monthlist[$i]['month'] = getMonth($month); |
||
| 41 | $monthlist[$i]['hits'] = $hits; |
||
| 42 | if ($month != $nowmonth) { |
||
| 43 | $monthlist[$i]['link'] = "<a href=\"statdetails.php?op=monthlystats&year=$year&month=$month\">" . $monthlist[$i]['month'] . '</a>'; |
||
| 44 | } else { |
||
| 45 | $monthlist[$i]['link'] = ''; |
||
| 46 | } |
||
| 47 | |||
| 48 | $midbarsize = mb_substr(100 * $hits / $hitsforyear, 0, 5); |
||
| 49 | $monthlist[$i]['percent'] = $midbarsize . '%'; |
||
| 50 | $m_name = getMonth($month); |
||
| 51 | $monthlist[$i]['graph'] = "<img src=\"assets/images/leftbar.gif\" height=\"$l_size[1]\" width=\"$l_size[0]\" Alt=\"$m_name - $hits\"><img src=\"assets/images/mainbar.gif\" Alt=\"$m_name - $hits\" height=\"$m_size[1]\" width=\"$midbarsize\"><img src=\"assets/images/rightbar.gif\" height=\"$r_size[1]\" width=\"$r_size[0]\" Alt=\"$m_name - $hits\">"; |
||
| 52 | ++$i; |
||
| 53 | } |
||
| 54 | $xoopsDB->freeRecordSet($result); |
||
| 55 | $xoopsTpl->assign('monthlist', $monthlist); |
||
| 56 | } |
||
| 57 | |||
| 58 | public static function getDailyStats($year, $month) |
||
| 59 | { |
||
| 60 | global $xoopsDB, $xoopsTpl; |
||
| 61 | |||
| 62 | $now = date('d-m-Y'); |
||
| 63 | $dot = explode('-', $now); |
||
| 64 | $nowdate = $dot[0]; |
||
| 65 | |||
| 66 | $l_size = getimagesize('assets/images/leftbar.gif'); |
||
| 67 | $m_size = getimagesize('assets/images/mainbar.gif'); |
||
| 68 | $r_size = getimagesize('assets/images/rightbar.gif'); |
||
| 69 | |||
| 70 | // get the daily stats and show |
||
| 71 | $resulttotal = $xoopsDB->queryF('select sum(hits) as totalhitsdate from ' . $xoopsDB->prefix('stats_date') . " where year='$year' and month='$month'"); |
||
| 72 | [$totalhitsdate] = $xoopsDB->fetchRow($resulttotal); |
||
| 73 | $xoopsDB->freeRecordSet($resulttotal); |
||
| 74 | $result = $xoopsDB->queryF('select year,month,date,hits from ' . $xoopsDB->prefix('stats_date') . " where year='$year' and month='$month' order by date"); |
||
| 75 | $total = $xoopsDB->getRowsNum($result); |
||
| 76 | $xoopsTpl->assign('lang_stat_dailystats', STATS_DAILYSTATS . ' - ' . getMonth($month) . ' - ' . $year); |
||
| 77 | $xoopsTpl->assign('lang_stat_dailyhead', STATS_DAILY); |
||
| 78 | $dailylist = []; |
||
| 79 | $i = 0; |
||
| 80 | while (list($year, $month, $date, $hits) = $xoopsDB->fetchRow($result)) { |
||
| 81 | $dailylist[$i]['year'] = $year; |
||
| 82 | $dailylist[$i]['month'] = getMonth($month); |
||
| 83 | $dailylist[$i]['date'] = $date; |
||
| 84 | $dailylist[$i]['hits'] = $hits; |
||
| 85 | if ($date != $nowdate) { |
||
| 86 | $dailylist[$i]['link'] = "<a href=\"statdetails.php?op=dailystats&year=$year&month=$month&date=$date\">" . $date . '</a>'; |
||
| 87 | } else { |
||
| 88 | $dailylist[$i]['link'] = ''; |
||
| 89 | } |
||
| 90 | $midbarsize = mb_substr(100 * $hits / $totalhitsdate, 0, 5); |
||
| 91 | $dailylist[$i]['percent'] = $midbarsize . '%'; |
||
| 92 | $dailylist[$i]['graph'] = "<img src=\"assets/images/leftbar.gif\" height=\"$l_size[1]\" width=\"$l_size[0]\" Alt=\"$date - $hits\"><img src=\"assets/images/mainbar.gif\" Alt=\"$date - $hits\" height=\"$m_size[1]\" width=\"$midbarsize\"><img src=\"assets/images/rightbar.gif\" height=\"$r_size[1]\" width=\"$r_size[0]\" Alt=\"$date - $hits\">"; |
||
| 93 | ++$i; |
||
| 94 | } |
||
| 95 | $xoopsDB->freeRecordSet($result); |
||
| 96 | $xoopsTpl->assign('dailylist', $dailylist); |
||
| 97 | } |
||
| 98 | |||
| 99 | public static function getHourlyStats($year, $month, $date) |
||
| 100 | { |
||
| 101 | global $xoopsDB, $xoopsTpl; |
||
| 102 | |||
| 103 | $l_size = getimagesize('assets/images/leftbar.gif'); |
||
| 104 | $m_size = getimagesize('assets/images/mainbar.gif'); |
||
| 105 | $r_size = getimagesize('assets/images/rightbar.gif'); |
||
| 106 | |||
| 107 | $now = date('d-m-Y'); |
||
| 108 | |||
| 109 | // show hourly stats and display |
||
| 110 | $resulttotal = $xoopsDB->queryF('select sum(hits) as totalhitshour from ' . $xoopsDB->prefix('stats_hour') . " where year='$year' and month='$month' and date='$date'"); |
||
| 111 | [$totalhitshour] = $xoopsDB->fetchRow($resulttotal); |
||
| 112 | $xoopsDB->freeRecordSet($resulttotal); |
||
| 113 | $date_arr = explode('-', $now); |
||
| 114 | $xoopsTpl->assign('lang_stat_hourlystats', STATS_HOURLYSTATS . ' - ' . getMonth($month) . ' ' . $date . ' ' . $year); |
||
| 115 | $xoopsTpl->assign('lang_stat_hourlyhead', STATS_HOURLY); |
||
| 116 | $hourlist = []; |
||
| 117 | |||
| 118 | for ($k = 0; $k <= 23; ++$k) { |
||
| 119 | $result = $xoopsDB->queryF('select hour,hits from ' . $xoopsDB->prefix('stats_hour') . " where year='$year' and month='$month' and date='$date' and hour='$k'"); |
||
| 120 | if (0 == $xoopsDB->getRowsNum($result)) { |
||
| 121 | $hits = 0; |
||
| 122 | $hour = $k; |
||
| 123 | } else { |
||
| 124 | [$hour, $hits] = $xoopsDB->fetchRow($result); |
||
| 125 | } |
||
| 126 | |||
| 127 | $a = (($k < 10) ? "0$k" : $k); |
||
| 128 | $hourlist[$k]['hour'] = "$a:00 - $a:59"; |
||
| 129 | |||
| 130 | $a = ''; |
||
| 131 | |||
| 132 | $midbarsize = ((0 == $hits) ? 0 : mb_substr(100 * $hits / $totalhitshour, 0, 5)); |
||
| 133 | $hourlist[$k]['hits'] = $hits; |
||
| 134 | $hourlist[$k]['percent'] = $midbarsize . '%'; |
||
| 135 | $hourlist[$k]['graph'] = "<img src=\"assets/images/leftbar.gif\" height=\"$l_size[1]\" width=\"$l_size[0]\" Alt=\"$hour - $hits\"><img src=\"assets/images/mainbar.gif\" Alt=\"$hour - $hits\" height=\"$m_size[1]\" width=\"$midbarsize\"><img src=\"assets/images/rightbar.gif\" height=\"$r_size[1]\" width=\"$r_size[0]\" Alt=\"$hour - $hits\">"; |
||
| 136 | } |
||
| 137 | |||
| 138 | $xoopsDB->freeRecordSet($result); |
||
| 139 | $xoopsTpl->assign('hourlist', $hourlist); |
||
| 140 | } |
||
| 141 | |||
| 142 | public static function getBlockedMonthlyStats($year) |
||
| 143 | { |
||
| 144 | global $xoopsDB, $xoopsTpl; |
||
| 145 | |||
| 146 | $now = date('d-m-Y'); |
||
| 147 | $dot = explode('-', $now); |
||
| 148 | $nowmonth = $dot[1]; |
||
| 149 | |||
| 150 | $l_size = getimagesize('assets/images/leftbar.gif'); |
||
| 151 | $m_size = getimagesize('assets/images/mainbar.gif'); |
||
| 152 | $r_size = getimagesize('assets/images/rightbar.gif'); |
||
| 153 | |||
| 154 | // get monthly stats and display |
||
| 155 | $resultmonth = $xoopsDB->queryF('select sum(hits) as totalhitsmonth from ' . $xoopsDB->prefix('stats_blockedmonth') . " where year='$year'"); |
||
| 156 | [$totalhitsmonth] = $xoopsDB->fetchRow($resultmonth); |
||
| 157 | $xoopsDB->freeRecordSet($resultmonth); |
||
| 158 | $result = $xoopsDB->queryF('select month,hits from ' . $xoopsDB->prefix('stats_blockedmonth') . " where year='$year'"); |
||
| 159 | $xoopsTpl->assign('lang_stat_monthlystats', STATS_BLOCKEDMONTHLYSTATS . ' - ' . $year); |
||
| 160 | $xoopsTpl->assign('lang_stat_monthhead', STATS_MONTH); |
||
| 161 | $resulttotal = $xoopsDB->queryF('select hits from ' . $xoopsDB->prefix('stats_blockedyear') . " where year='$year'"); |
||
| 162 | [$hitsforyear] = $xoopsDB->fetchRow($resulttotal); |
||
| 163 | $monthlist = []; |
||
| 164 | $i = 0; |
||
| 165 | while (list($month, $hits) = $xoopsDB->fetchRow($result)) { |
||
| 166 | $monthlist[$i]['month'] = getMonth($month); |
||
| 167 | $monthlist[$i]['hits'] = $hits; |
||
| 168 | if ($month != $nowmonth) { |
||
| 169 | $monthlist[$i]['link'] = "<a href=\"statdetails.php?op=b_monthlystats&year=$year&month=$month\">" . $monthlist[$i]['month'] . '</a>'; |
||
| 170 | } else { |
||
| 171 | $monthlist[$i]['link'] = ''; |
||
| 172 | } |
||
| 173 | |||
| 174 | $midbarsize = mb_substr(100 * $hits / $hitsforyear, 0, 5); |
||
| 175 | $monthlist[$i]['percent'] = $midbarsize . '%'; |
||
| 176 | $m_name = getMonth($month); |
||
| 177 | $monthlist[$i]['graph'] = "<img src=\"assets/images/leftbar.gif\" height=\"$l_size[1]\" width=\"$l_size[0]\" Alt=\"$m_name - $hits\"><img src=\"assets/images/mainbar.gif\" Alt=\"$m_name - $hits\" height=\"$m_size[1]\" width=\"$midbarsize\"><img src=\"assets/images/rightbar.gif\" height=\"$r_size[1]\" width=\"$r_size[0]\" Alt=\"$m_name - $hits\">"; |
||
| 178 | ++$i; |
||
| 179 | } |
||
| 180 | $xoopsDB->freeRecordSet($result); |
||
| 181 | $xoopsTpl->assign('monthlist', $monthlist); |
||
| 182 | } |
||
| 183 | |||
| 184 | public static function getBlockedDailyStats($year, $month) |
||
| 185 | { |
||
| 186 | global $xoopsDB, $xoopsTpl; |
||
| 187 | |||
| 188 | $now = date('d-m-Y'); |
||
| 189 | $dot = explode('-', $now); |
||
| 190 | $nowdate = $dot[0]; |
||
| 191 | |||
| 192 | $l_size = getimagesize('assets/images/leftbar.gif'); |
||
| 193 | $m_size = getimagesize('assets/images/mainbar.gif'); |
||
| 194 | $r_size = getimagesize('assets/images/rightbar.gif'); |
||
| 195 | |||
| 196 | // get the daily stats and show |
||
| 197 | $resulttotal = $xoopsDB->queryF('select sum(hits) as totalhitsdate from ' . $xoopsDB->prefix('stats_blockeddate') . " where year='$year' and month='$month'"); |
||
| 198 | [$totalhitsdate] = $xoopsDB->fetchRow($resulttotal); |
||
| 199 | $xoopsDB->freeRecordSet($resulttotal); |
||
| 200 | $result = $xoopsDB->queryF('select year,month,date,hits from ' . $xoopsDB->prefix('stats_blockeddate') . " where year='$year' and month='$month' order by date"); |
||
| 201 | $total = $xoopsDB->getRowsNum($result); |
||
| 202 | $xoopsTpl->assign('lang_stat_dailystats', STATS_BLOCKEDDAILYSTATS . ' - ' . getMonth($month) . ' - ' . $year); |
||
| 203 | $xoopsTpl->assign('lang_stat_dailyhead', STATS_DAILY); |
||
| 204 | $dailylist = []; |
||
| 205 | $i = 0; |
||
| 206 | while (list($year, $month, $date, $hits) = $xoopsDB->fetchRow($result)) { |
||
| 207 | $dailylist[$i]['year'] = $year; |
||
| 208 | $dailylist[$i]['month'] = getMonth($month); |
||
| 209 | $dailylist[$i]['date'] = $date; |
||
| 210 | $dailylist[$i]['hits'] = $hits; |
||
| 211 | if ($date != $nowdate) { |
||
| 212 | $dailylist[$i]['link'] = "<a href=\"statdetails.php?op=b_dailystats&year=$year&month=$month&date=$date\">" . $date . '</a>'; |
||
| 213 | } else { |
||
| 214 | $dailylist[$i]['link'] = ''; |
||
| 215 | } |
||
| 216 | $midbarsize = mb_substr(100 * $hits / $totalhitsdate, 0, 5); |
||
| 217 | $dailylist[$i]['percent'] = $midbarsize . '%'; |
||
| 218 | $dailylist[$i]['graph'] = "<img src=\"assets/images/leftbar.gif\" height=\"$l_size[1]\" width=\"$l_size[0]\" Alt=\"$date - $hits\"><img src=\"assets/images/mainbar.gif\" Alt=\"$date - $hits\" height=\"$m_size[1]\" width=\"$midbarsize\"><img src=\"assets/images/rightbar.gif\" height=\"$r_size[1]\" width=\"$r_size[0]\" Alt=\"$date - $hits\">"; |
||
| 219 | ++$i; |
||
| 220 | } |
||
| 221 | $xoopsDB->freeRecordSet($result); |
||
| 222 | $xoopsTpl->assign('dailylist', $dailylist); |
||
| 223 | } |
||
| 224 | |||
| 225 | public static function getBlockedHourlyStats($year, $month, $date) |
||
| 266 | } |
||
| 267 | |||
| 268 | public static function getMonth($month) |
||
| 269 | { |
||
| 270 | switch ($month) { |
||
| 271 | case 1: |
||
| 272 | return STATS_JANUARY; |
||
| 273 | case 2: |
||
| 274 | return STATS_FEBRUARY; |
||
| 275 | case 3: |
||
| 276 | return STATS_MARCH; |
||
| 277 | case 4: |
||
| 278 | return STATS_APRIL; |
||
| 279 | case 5: |
||
| 280 | return STATS_MAY; |
||
| 281 | case 6: |
||
| 282 | return STATS_JUNE; |
||
| 283 | case 7: |
||
| 284 | return STATS_JULY; |
||
| 285 | case 8: |
||
| 286 | return STATS_AUGUST; |
||
| 287 | case 9: |
||
| 288 | return STATS_SEPTEMBER; |
||
| 289 | case 10: |
||
| 290 | return STATS_OCTOBER; |
||
| 291 | case 11: |
||
| 292 | return STATS_NOVEMBER; |
||
| 293 | default: |
||
| 294 | return STATS_DECEMBER; |
||
| 295 | } |
||
| 296 | } |
||
| 297 | |||
| 298 | /** |
||
| 299 | * adminmenu() |
||
| 300 | * |
||
| 301 | * @param string $header optional : You can gice the menu a nice header |
||
| 302 | * @param string $extra optional : You can gice the menu a nice footer |
||
| 303 | * @param array|string $menu required : This is an array of links. U can |
||
| 304 | * @param int $scount required : This will difine the amount of cells long the menu will have. |
||
| 305 | * NB: using a value of 3 at the moment will break the menu where the cell colours will be off display. |
||
| 306 | */ |
||
| 307 | public static function stats_adminmenu($header = '', $extra = '', $menu = '', $scount = 4) |
||
| 392 | } |
||
| 393 | } |
||
| 394 | |||
| 395 | } |
||
| 396 |