GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 9372c3...512480 )
by François
01:56
created

Stats::get()   F

Complexity

Conditions 14
Paths 970

Size

Total Lines 102
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 102
rs 2.4369
cc 14
eloc 64
nc 970
nop 1

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
/**
3
 *  Copyright (C) 2016 SURFnet.
4
 *
5
 *  This program is free software: you can redistribute it and/or modify
6
 *  it under the terms of the GNU Affero General Public License as
7
 *  published by the Free Software Foundation, either version 3 of the
8
 *  License, or (at your option) any later version.
9
 *
10
 *  This program is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 *  GNU Affero General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Affero General Public License
16
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace SURFnet\VPN\Server;
20
21
class Stats
22
{
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;
0 ignored issues
show
Unused Code introduced by
return $statsData; does not seem to be reachable.

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, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
124
    }
125
}
126