BenchmarkResultsController   A
last analyzed

Complexity

Total Complexity 26

Size/Duplication

Total Lines 151
Duplicated Lines 19.87 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 0
dl 30
loc 151
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 27 3
A compareRequests() 0 17 4
A compare() 0 20 4
A firstInHistory() 0 5 4
A isBest() 15 15 3
A isWorst() 15 15 3
A getTime() 0 8 1
A getMemoryUsage() 0 14 2
A storeBestRequest() 0 7 1
A storeWorstRequest() 0 7 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mtolhuys\LaravelRequestBenchmark\Controllers;
4
5
class BenchmarkResultsController
6
{
7
    private $bestRequests = [];
8
9
    private $worstRequests = [];
10
11
    public function index()
12
    {
13
        $file = config('request-benchmark.storage_path') . '/request-benchmark.json';
14
        $requests = [];
15
        $history = [];
16
17
        if (file_exists($file)) {
18
            $data = json_decode(file_get_contents($file), true);
19
20
            if (isset($data['history'])) {
21
                $history = $data['history'];
22
23
                unset($data['history']);
24
            }
25
26
            $requests = $data;
27
28
            $this->compareRequests($history);
29
        }
30
31
        return view('request-benchmark::results', [
32
            'requests' => $requests,
33
            'history' => $history,
34
            'bestRequests' => $this->bestRequests,
35
            'worstRequests' => $this->worstRequests,
36
        ]);
37
    }
38
39
    private function compareRequests(array $history): array
40
    {
41
        $bestRequests = [];
42
43
        foreach ($history as $request => $requestHistory) {
44
            if (count($requestHistory) <= 1) {
45
                continue;
46
            }
47
48
            foreach ($requestHistory as $timestamp => $result) {
49
                $this->compare($request, $timestamp, $result);
50
            }
51
52
        }
53
54
        return $bestRequests;
55
    }
56
57
    private function compare(string $request, string $timestamp, array $result)
58
    {
59
        if ($this->firstInHistory($request)) {
60
            $this->storeBestRequest($request, $timestamp, $result);
61
            $this->storeWorstRequest($request, $timestamp, $result);
62
63
            return;
64
        }
65
66
        $time = $this->getTime($result['time']);
67
        $memoryUsage = $this->getMemoryUsage($result['actual_memory_usage']);
68
69
        if ($this->isBest($request, $time, $memoryUsage)) {
70
            $this->storeBestRequest($request, $timestamp, $result);
71
        }
72
73
        if ($this->isWorst($request, $time, $memoryUsage)) {
74
            $this->storeWorstRequest($request, $timestamp, $result);
75
        }
76
    }
77
78
    private function firstInHistory(string $request): bool
79
    {
80
        return (empty($this->bestRequests) && empty($this->worstRequests))
81
            || (! isset($this->bestRequests[$request]) && ! isset($this->worstRequests[$request]));
82
    }
83
84 View Code Duplication
    private function isBest(string $request, float $time, float $memoryUsage): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        $bestTime = $this->getTime($this->bestRequests[$request]->result->time);
87
        $bestMemoryUsage = $this->getMemoryUsage(
88
            $this->bestRequests[$request]->result->actual_memory_usage
89
        );
90
91
        if ($memoryUsage <= $bestMemoryUsage) {
92
            $difference = $bestMemoryUsage - $memoryUsage;
93
94
            return $difference > 1000 || $time < $bestTime;
95
        }
96
97
        return false;
98
    }
99
100 View Code Duplication
    private function isWorst(string $request, float $time, float $memoryUsage): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $worstTime = $this->getTime($this->worstRequests[$request]->result->time);
103
        $worstMemoryUsage = $this->getMemoryUsage(
104
            $this->worstRequests[$request]->result->actual_memory_usage
105
        );
106
107
        if ($memoryUsage >= $worstMemoryUsage) {
108
            $difference = $memoryUsage - $worstMemoryUsage;
109
110
            return $difference > 1000 || $worstTime < $time;
111
        }
112
113
        return false;
114
    }
115
116
    private function getTime(string $time): float
117
    {
118
        return (float) filter_var(
119
            $time,
120
            FILTER_SANITIZE_NUMBER_FLOAT,
121
            FILTER_FLAG_ALLOW_FRACTION
122
        );
123
    }
124
125
    private function getMemoryUsage(string $actualMemoryUsage): float
126
    {
127
        $memoryUsage = filter_var(
128
            $actualMemoryUsage,
129
            FILTER_SANITIZE_NUMBER_FLOAT,
130
            FILTER_FLAG_ALLOW_FRACTION
131
        );
132
133
        if (strpos($actualMemoryUsage, 'Mb') !== false) {
134
            return (float) $memoryUsage * 1024;
135
        }
136
137
        return (float) $memoryUsage;
138
    }
139
140
    private function storeBestRequest(string $request, string $timestamp, array $result)
141
    {
142
        $this->bestRequests[$request] = (object) [
143
            'timestamp' => $timestamp,
144
            'result' => (object) $result,
145
        ];
146
    }
147
148
    private function storeWorstRequest(string $request, string $timestamp, array $result)
149
    {
150
        $this->worstRequests[$request] = (object) [
151
            'timestamp' => $timestamp,
152
            'result' => (object) $result,
153
        ];
154
    }
155
}
156