Profiler::getParameters()   C
last analyzed

Complexity

Conditions 14
Paths 25

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 14

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 32
cts 32
cp 1
rs 6.2666
c 0
b 0
f 0
cc 14
nc 25
nop 2
crap 14

How to fix   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
namespace GuzzleHttp\Profiling\Debugbar;
4
5
use DebugBar\DataCollector\TimeDataCollector;
6
use GuzzleHttp\Profiling\DescriptionMaker;
7
use GuzzleHttp\Profiling\Profiler as ProfilerContract;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
class Profiler implements ProfilerContract
12
{
13
    use DescriptionMaker;
14
15
    /**
16
     * @var \DebugBar\DataCollector\TimeDataCollector
17
     */
18
    protected $timeline;
19
20
    /**
21
     * @var array
22
     */
23
    protected $availableParameters = [
24
        'method', 'url', 'resource', 'request_version', 'response_version', 'host', 'hostname', 'status_code', 'phrase',
25
    ];
26
27
    /**
28
     * @var array
29
     */
30
    protected $context = ['host', 'method', 'url', 'status_code', 'phrase'];
31
32
    /**
33
     * Public constructor.
34
     *
35
     * @param \DebugBar\DataCollector\TimeDataCollector $timeline
36
     */
37 2
    public function __construct(TimeDataCollector $timeline)
38
    {
39 2
        $this->timeline = $timeline;
40 2
    }
41
42
    /**
43
     * @param float $start
44
     * @param float $end
45
     * @param \Psr\Http\Message\RequestInterface $request
46
     * @param \Psr\Http\Message\ResponseInterface $response
47
     */
48 2
    public function add(float $start, float $end, RequestInterface $request, ResponseInterface $response = null): void
49
    {
50 2
        $description = $this->describe($request, $response);
51 2
        $params = $this->getParameters($request, $response);
52
53 2
        $this->timeline->addMeasure($description, $start, $end, $params, 'guzzle');
54 2
    }
55
56
    /**
57
     * Get context fields to add to the time-line entry.
58
     *
59
     * @param \Psr\Http\Message\RequestInterface $request
60
     * @param \Psr\Http\Message\ResponseInterface $response
61
     *
62
     * @return array
63
     */
64 2
    protected function getParameters(RequestInterface $request, ResponseInterface $response = null): array
65
    {
66 2
        $params = [];
67 2
        $result = '';
68
69 2
        $keys = array_intersect($this->context, $this->availableParameters);
70
71 2
        foreach ($keys as $key) {
72 2
            switch ($key) {
73 2
                case 'method':
74 2
                    $result = $request->getMethod();
75 2
                    break;
76 2
                case 'url':
77 2
                    $result = $request->getUri()->__toString();
78 2
                    break;
79 2
                case 'request_version':
80 1
                    $result = $request->getProtocolVersion();
81 1
                    break;
82 2
                case 'response_version':
83 1
                    $result = $response ? $response->getProtocolVersion() : 'NULL';
84 1
                    break;
85 2
                case 'host':
86 2
                    $result = $request->getUri()->getHost();
87 2
                    break;
88 2
                case 'hostname':
89 1
                    $result = gethostname();
90 1
                    break;
91 2
                case 'status_code':
92 2
                    $result = $response ? $response->getStatusCode() : 'NULL';
93 2
                    break;
94 2
                case 'phrase':
95 2
                    $result = $response ? $response->getReasonPhrase() : 'NULL';
96 2
                    break;
97
            }
98
99 2
            $params[$key] = (string) $result ?: '';
100
        }
101
102 2
        return $params;
103
    }
104
}
105