RequestPerRoute   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Test Coverage

Coverage 52.63%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 37
c 2
b 0
f 0
dl 0
loc 100
rs 10
ccs 20
cts 38
cp 0.5263
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A requestCountMetric() 0 15 1
A handle() 0 17 1
A requestLatencyMetric() 0 24 2
A __construct() 0 3 1
1
<?php
0 ignored issues
show
Coding Style introduced by
Filename "RequestPerRoute.php" doesn't match the expected filename "requestperroute.php"
Loading history...
2
namespace Triadev\PrometheusExporter\Middleware;
3
4
use Illuminate\Http\Request;
5
use Closure;
6
use Illuminate\Http\Response;
7
use Prometheus\Exception\MetricsRegistrationException;
8
use Triadev\PrometheusExporter\Contract\PrometheusExporterContract;
9
10
class RequestPerRoute
11
{
12
    /** @var PrometheusExporterContract */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
13
    private $prometheusExporter;
0 ignored issues
show
Coding Style introduced by
Private member variable "prometheusExporter" must contain a leading underscore
Loading history...
Coding Style introduced by
Expected 1 blank line before member var; 0 found
Loading history...
Coding Style introduced by
Private member variable "prometheusExporter" must be prefixed with an underscore
Loading history...
14
    
15
    /**
16
     * RequestPerRoute constructor.
17
     * @param PrometheusExporterContract $prometheusExporter
0 ignored issues
show
Coding Style introduced by
There must be exactly one blank line before the tags in a doc comment
Loading history...
Coding Style introduced by
Missing parameter comment
Loading history...
18 2
     */
19
    public function __construct(PrometheusExporterContract $prometheusExporter)
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines before function; 1 found
Loading history...
20 2
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
21 2
        $this->prometheusExporter = $prometheusExporter;
22
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end __construct()
Loading history...
23
    
24
    /**
25
     * Handle an incoming request.
26
     *
27
     * @param  Request $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 2 spaces after parameter type; 1 found
Loading history...
28
     * @param  \Closure $next
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
29
     * @return mixed
0 ignored issues
show
Coding Style introduced by
Tag cannot be grouped with parameter tags in a doc comment
Loading history...
30
     *
31
     * @throws MetricsRegistrationException
32 2
     */
33
    public function handle(Request $request, Closure $next)
34 2
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
35
        $start = microtime(true);
36
    
37 2
        /** @var Response $response */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
38
        $response = $next($request);
39 2
    
40
        $durationMilliseconds = (microtime(true) - $start) * 1000.0;
0 ignored issues
show
Coding Style introduced by
Arithmetic operation must be bracketed
Loading history...
41 2
        
42 2
        $path = $request->path();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
43 2
        $method = $request->getMethod();
44
        $status = $response->getStatusCode();
45 2
        
46
        $this->requestCountMetric($path, $method, $status);
47
        $this->requestLatencyMetric($path, $method, $status, $durationMilliseconds);
48
    
49
        return $response;
50
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end handle()
Loading history...
51
    
52
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
53
     * @param string $routeName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
54
     * @param string $method
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
55
     * @param int $status
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
56
     *
57 2
     * @throws MetricsRegistrationException
58
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
59 2
    private function requestCountMetric(string $routeName, string $method, int $status)
0 ignored issues
show
Coding Style introduced by
Private method name "RequestPerRoute::requestCountMetric" must be prefixed with an underscore
Loading history...
60 2
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
61 2
        $this->prometheusExporter->incCounter(
62 2
            'requests_total',
63
            'the number of http requests',
64 2
            config('prometheus_exporter.namespace_http'),
65
            [
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
66
                'route',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 13 spaces, but found 16.
Loading history...
67
                'method',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 13 spaces, but found 16.
Loading history...
68
                'status_code'
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
69 2
            ],
70 2
            [
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
71 2
                $routeName,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 13 spaces, but found 16.
Loading history...
72
                $method,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 13 spaces, but found 16.
Loading history...
73
                $status
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
74
            ]
75
        );
76
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 1 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end requestCountMetric()
Loading history...
77
    
78
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
79
     * @param string $routeName
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
80
     * @param string $method
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
81
     * @param int $status
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
82
     * @param int $duration
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Expected 4 spaces after parameter type; 1 found
Loading history...
83
     *
84
     * @throws MetricsRegistrationException
85
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
86
    private function requestLatencyMetric(string $routeName, string $method, int $status, int $duration)
0 ignored issues
show
Coding Style introduced by
Private method name "RequestPerRoute::requestLatencyMetric" must be prefixed with an underscore
Loading history...
Coding Style introduced by
This line exceeds maximum limit of 100 characters; contains 104 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
87
    {
0 ignored issues
show
Coding Style introduced by
Opening brace should be on the same line as the declaration
Loading history...
88
        $bucketsPerRoute = null;
89
        
90
        if ($bucketsPerRouteConfig = config('prometheus-exporter.buckets_per_route')) {
0 ignored issues
show
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
91
            $bucketsPerRoute = array_get($bucketsPerRouteConfig, $routeName);
0 ignored issues
show
Deprecated Code introduced by
The function array_get() has been deprecated: Arr::get() should be used directly instead. Will be removed in Laravel 6.0. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

91
            $bucketsPerRoute = /** @scrutinizer ignore-deprecated */ array_get($bucketsPerRouteConfig, $routeName);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
92
        }
93
        
94
        $this->prometheusExporter->setHistogram(
95
            'requests_latency_milliseconds',
96
            'duration of requests',
97
            $duration,
98
            config('prometheus_exporter.namespace_http'),
99
            [
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
100
                'route',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 13 spaces, but found 16.
Loading history...
101
                'method',
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 13 spaces, but found 16.
Loading history...
102
                'status_code'
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
103
            ],
104
            [
0 ignored issues
show
Coding Style introduced by
Short array syntax is not allowed
Loading history...
105
                $routeName,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 13 spaces, but found 16.
Loading history...
106
                $method,
0 ignored issues
show
Coding Style introduced by
This array value does not seem to be aligned correcty; expected 13 spaces, but found 16.
Loading history...
107
                $status
0 ignored issues
show
Coding Style introduced by
There should be a trailing comma after the last value of an array declaration.
Loading history...
108
            ],
109
            $bucketsPerRoute
110
        );
111
    }
0 ignored issues
show
Coding Style introduced by
Expected 2 blank lines after function; 0 found
Loading history...
Coding Style introduced by
Expected 1 blank line before closing function brace; 0 found
Loading history...
Coding Style introduced by
Expected //end requestLatencyMetric()
Loading history...
112
}
0 ignored issues
show
Coding Style introduced by
Expected //end class
Loading history...
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
113