| Conditions | 14 |
| Paths | 970 |
| Total Lines | 100 |
| Code Lines | 60 |
| 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 |
||
| 33 | public function get(array $logEntries) |
||
| 34 | { |
||
| 35 | $statsData = []; |
||
| 36 | $timeConnection = []; |
||
| 37 | $uniqueUsers = []; |
||
| 38 | $activeUserCount = 0; |
||
| 39 | |||
| 40 | foreach ($logEntries as $entry) { |
||
| 41 | // determine user_id |
||
| 42 | |||
| 43 | $userId = substr($entry['common_name'], 0, strpos($entry['common_name'], '_')); |
||
| 44 | $connectedAt = $entry['connected_at']; |
||
| 45 | $disconnectedAt = $entry['disconnected_at']; |
||
| 46 | $dateOfConnection = date('Y-m-d', $connectedAt); |
||
| 47 | |||
| 48 | if (!array_key_exists($dateOfConnection, $statsData)) { |
||
| 49 | $statsData[$dateOfConnection] = [ |
||
| 50 | 'number_of_connections' => 0, |
||
| 51 | 'bytes_transferred' => 0, |
||
| 52 | 'unique_user_list' => [], |
||
| 53 | ]; |
||
| 54 | } |
||
| 55 | |||
| 56 | if (is_null($disconnectedAt)) { |
||
| 57 | $activeUserCount += 1; |
||
| 58 | } |
||
| 59 | |||
| 60 | $statsData[$dateOfConnection]['number_of_connections'] += 1; |
||
| 61 | $statsData[$dateOfConnection]['bytes_transferred'] += $entry['bytes_transferred']; |
||
| 62 | |||
| 63 | // add it to table to be able to determine max concurrent connection |
||
| 64 | // count |
||
| 65 | if (!array_key_exists($connectedAt, $timeConnection)) { |
||
| 66 | $timeConnection[$connectedAt] = []; |
||
| 67 | } |
||
| 68 | $timeConnection[$connectedAt][] = 'C'; |
||
| 69 | |||
| 70 | if (!is_null($disconnectedAt)) { |
||
| 71 | if (!array_key_exists($disconnectedAt, $timeConnection)) { |
||
| 72 | $timeConnection[$disconnectedAt] = []; |
||
| 73 | } |
||
| 74 | $timeConnection[$disconnectedAt][] = 'D'; |
||
| 75 | } |
||
| 76 | |||
| 77 | // unique user list per day |
||
| 78 | if (!in_array($userId, $statsData[$dateOfConnection]['unique_user_list'])) { |
||
| 79 | $statsData[$dateOfConnection]['unique_user_list'][] = $userId; |
||
| 80 | } |
||
| 81 | |||
| 82 | // unique user list for the whole logging period |
||
| 83 | if (!in_array($userId, $uniqueUsers)) { |
||
| 84 | $uniqueUsers[] = $userId; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | ksort($timeConnection); |
||
| 89 | $firstEntryTime = intval(key($timeConnection)); |
||
|
|
|||
| 90 | end($timeConnection); |
||
| 91 | $lastEntryTime = intval(key($timeConnection)); |
||
| 92 | reset($timeConnection); |
||
| 93 | |||
| 94 | $maxConcurrentConnections = 0; |
||
| 95 | $maxConcurrentConnectionsTime = 0; |
||
| 96 | $concurrentConnections = 0; |
||
| 97 | foreach ($timeConnection as $unixTime => $eventArray) { |
||
| 98 | foreach ($eventArray as $event) { |
||
| 99 | if ('C' === $event) { |
||
| 100 | ++$concurrentConnections; |
||
| 101 | if ($concurrentConnections > $maxConcurrentConnections) { |
||
| 102 | $maxConcurrentConnections = $concurrentConnections; |
||
| 103 | $maxConcurrentConnectionsTime = $unixTime; |
||
| 104 | } |
||
| 105 | } else { |
||
| 106 | --$concurrentConnections; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | $totalTraffic = 0; |
||
| 112 | // convert the user list in unique user count for that day, rework array |
||
| 113 | // key and determine total amount of traffic |
||
| 114 | foreach ($statsData as $date => $entry) { |
||
| 115 | $statsData[$date]['date'] = $date; |
||
| 116 | $statsData[$date]['unique_user_count'] = count($entry['unique_user_list']); |
||
| 117 | unset($statsData[$date]['unique_user_list']); |
||
| 118 | $totalTraffic += $entry['bytes_transferred']; |
||
| 119 | } |
||
| 120 | |||
| 121 | return [ |
||
| 122 | 'days' => array_values($statsData), |
||
| 123 | 'total_traffic' => $totalTraffic, |
||
| 124 | 'generated_at' => $this->now->getTimestamp(), |
||
| 125 | 'max_concurrent_connections' => $maxConcurrentConnections, |
||
| 126 | // 'max_concurrent_connections_time' => $maxConcurrentConnectionsTime, |
||
| 127 | // 'first_entry' => $firstEntryTime, |
||
| 128 | // 'last_entry' => $lastEntryTime, |
||
| 129 | 'unique_user_count' => count($uniqueUsers), |
||
| 130 | 'active_user_count' => $activeUserCount, |
||
| 131 | ]; |
||
| 132 | } |
||
| 133 | } |
||
| 134 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.