| Conditions | 14 |
| Paths | 970 |
| Total Lines | 102 |
| Code Lines | 64 |
| 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 |
||
| 23 | public function get(array $logEntries) |
||
| 24 | { |
||
| 25 | $statsData = []; |
||
| 26 | $timeConnection = []; |
||
| 27 | $uniqueUsers = []; |
||
| 28 | $activeUserCount = 0; |
||
| 29 | |||
| 30 | foreach ($logEntries as $entry) { |
||
| 31 | // determine user_id |
||
| 32 | |||
| 33 | $userId = substr($entry['common_name'], 0, strpos($entry['common_name'], '_')); |
||
| 34 | $connectedAt = $entry['connected_at']; |
||
| 35 | $disconnectedAt = $entry['disconnected_at']; |
||
| 36 | $dateOfConnection = date('Y-m-d', $connectedAt); |
||
| 37 | |||
| 38 | if (!array_key_exists($dateOfConnection, $statsData)) { |
||
| 39 | $statsData[$dateOfConnection] = [ |
||
| 40 | 'number_of_connections' => 0, |
||
| 41 | 'bytes_transferred' => 0, |
||
| 42 | 'unique_user_list' => [], |
||
| 43 | ]; |
||
| 44 | } |
||
| 45 | |||
| 46 | if (is_null($disconnectedAt)) { |
||
| 47 | $activeUserCount += 1; |
||
| 48 | } |
||
| 49 | |||
| 50 | $statsData[$dateOfConnection]['number_of_connections'] += 1; |
||
| 51 | $statsData[$dateOfConnection]['bytes_transferred'] += $entry['bytes_transferred']; |
||
| 52 | |||
| 53 | // add it to table to be able to determine max concurrent connection |
||
| 54 | // count |
||
| 55 | if (!array_key_exists($connectedAt, $timeConnection)) { |
||
| 56 | $timeConnection[$connectedAt] = []; |
||
| 57 | } |
||
| 58 | $timeConnection[$connectedAt][] = 'C'; |
||
| 59 | |||
| 60 | if (!is_null($disconnectedAt)) { |
||
| 61 | if (!array_key_exists($disconnectedAt, $timeConnection)) { |
||
| 62 | $timeConnection[$disconnectedAt] = []; |
||
| 63 | } |
||
| 64 | $timeConnection[$disconnectedAt][] = 'D'; |
||
| 65 | } |
||
| 66 | |||
| 67 | // unique user list per day |
||
| 68 | if (!in_array($userId, $statsData[$dateOfConnection]['unique_user_list'])) { |
||
| 69 | $statsData[$dateOfConnection]['unique_user_list'][] = $userId; |
||
| 70 | } |
||
| 71 | |||
| 72 | // unique user list for the whole logging period |
||
| 73 | if (!in_array($userId, $uniqueUsers)) { |
||
| 74 | $uniqueUsers[] = $userId; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | ksort($timeConnection); |
||
| 79 | $firstEntryTime = intval(key($timeConnection)); |
||
| 80 | end($timeConnection); |
||
| 81 | $lastEntryTime = intval(key($timeConnection)); |
||
| 82 | reset($timeConnection); |
||
| 83 | |||
| 84 | $maxConcurrentConnections = 0; |
||
| 85 | $maxConcurrentConnectionsTime = 0; |
||
| 86 | $concurrentConnections = 0; |
||
| 87 | foreach ($timeConnection as $unixTime => $eventArray) { |
||
| 88 | foreach ($eventArray as $event) { |
||
| 89 | if ('C' === $event) { |
||
| 90 | ++$concurrentConnections; |
||
| 91 | if ($concurrentConnections > $maxConcurrentConnections) { |
||
| 92 | $maxConcurrentConnections = $concurrentConnections; |
||
| 93 | $maxConcurrentConnectionsTime = $unixTime; |
||
| 94 | } |
||
| 95 | } else { |
||
| 96 | --$concurrentConnections; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | |||
| 101 | $totalTraffic = 0; |
||
| 102 | // convert the user list in unique user count for that day, rework array |
||
| 103 | // key and determine total amount of traffic |
||
| 104 | foreach ($statsData as $date => $entry) { |
||
| 105 | $statsData[$date]['date'] = $date; |
||
| 106 | $statsData[$date]['unique_user_count'] = count($entry['unique_user_list']); |
||
| 107 | unset($statsData[$date]['unique_user_list']); |
||
| 108 | $totalTraffic += $entry['bytes_transferred']; |
||
| 109 | } |
||
| 110 | |||
| 111 | return [ |
||
| 112 | 'days' => array_values($statsData), |
||
| 113 | 'total_traffic' => $totalTraffic, |
||
| 114 | 'generated_at' => time(), |
||
| 115 | 'max_concurrent_connections' => $maxConcurrentConnections, |
||
| 116 | 'max_concurrent_connections_time' => $maxConcurrentConnectionsTime, |
||
| 117 | 'first_entry' => $firstEntryTime, |
||
| 118 | 'last_entry' => $lastEntryTime, |
||
| 119 | 'unique_user_count' => count($uniqueUsers), |
||
| 120 | 'active_user_count' => $activeUserCount, |
||
| 121 | ]; |
||
| 122 | |||
| 123 | return $statsData; |
||
|
|
|||
| 124 | } |
||
| 125 | } |
||
| 126 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.