Completed
Push — master ( 9cb483...15ad7c )
by Maik
03:24
created

HttpHeadersTrait::adjustHeaders()   C

Complexity

Conditions 11
Paths 48

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 11.0295

Importance

Changes 0
Metric Value
dl 0
loc 28
ccs 15
cts 16
cp 0.9375
rs 5.2653
c 0
b 0
f 0
cc 11
eloc 16
nc 48
nop 1
crap 11.0295

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
namespace Generics\Client;
3
4
use Generics\Util\Arrays;
5
6
trait HttpHeadersTrait
7
{
8
9
    /**
10
     * Headers
11
     *
12
     * @var array
13
     */
14
    private $headers;
15
16
    /**
17
     * The response status code
18
     *
19
     * @var int
20
     */
21
    private $responseCode;
22
    
23
    /**
24
     *
25
     * {@inheritdoc}
26
     * @see \Generics\Streams\HttpStream::setHeader()
27
     * @return HttpClient
28
     */
29 22
    public function setHeader($headerName, $headerValue)
30
    {
31 22
        $this->headers[$headerName] = $headerValue;
32 22
    }
33
34
    /**
35
     * Reset the headers
36
     */
37 23
    public function resetHeaders()
38
    {
39 23
        $this->headers = array();
40 23
    }
41
42
    /**
43
     *
44
     * {@inheritdoc}
45
     * @see \Generics\Streams\HttpStream::getHeaders()
46
     */
47 22
    public function getHeaders(): array
48
    {
49 22
        return $this->headers;
50
    }
51
    
52
    /**
53
     * Retrieve the response status code
54
     *
55
     * @return int
56
     */
57 16
    public function getResponseCode(): int
58
    {
59 16
        return $this->responseCode;
60
    }
61
62
    /**
63
     * Adjust the headers by injecting default values for missing keys.
64
     */
65 22
    private function adjustHeaders($requestType)
66
    {
67 22
        if (! array_key_exists('Accept', $this->headers) && $requestType != 'HEAD') {
68 18
            $this->setHeader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8');
69
        }
70
        
71 22
        if (! array_key_exists('Accept-Language', $this->headers) && $requestType != 'HEAD') {
72 18
            $this->setHeader('Accept-Language', 'en-US;q=0.7,en;q=0.3');
73
        }
74
        
75 22
        if (! array_key_exists('User-Agent', $this->headers) && $requestType != 'HEAD') {
76 18
            $this->setHeader('User-Agent', 'phpGenerics 1.0');
77
        }
78
        
79 22
        if (! array_key_exists('Connection', $this->headers) || strlen($this->headers['Connection']) == 0) {
80 18
            $this->adjustConnectionHeader($requestType);
81
        }
82
        
83 22
        if (! array_key_exists('Accept-Encoding', $this->headers)) {
84 22
            $encoding = "";
0 ignored issues
show
Unused Code introduced by
$encoding is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
85 22
            if (function_exists('gzinflate')) {
86 22
                $encoding = 'gzip, deflate';
87
            } else {
88
                $encoding = 'identity';
89
            }
90 22
            $this->setHeader('Accept-Encoding', $encoding);
91
        }
92 22
    }
93
94
    /**
95
     * Depending on request type the connection header is either
96
     * set to keep-alive or close
97
     *
98
     * @param string $requestType
99
     */
100 18
    private function adjustConnectionHeader($requestType)
101
    {
102 18
        if ($requestType == 'HEAD') {
103 2
            $this->setHeader('Connection', 'close');
104
        } else {
105 16
            $this->setHeader('Connection', 'keep-alive');
106
        }
107 18
    }
108
109
    /**
110
     * Try to parse line as header and add the results to local header list
111
     *
112
     * @param string $line
113
     */
114 18
    private function addParsedHeader($line)
115
    {
116 18
        if (strpos($line, ':') === false) {
117 18
            $this->responseCode = HttpStatus::parseStatus($line)->getCode();
118
        } else {
119 18
            $line = trim($line);
120 18
            list ($headerName, $headerValue) = explode(':', $line, 2);
121 18
            $this->headers[$headerName] = trim($headerValue);
122
        }
123 18
    }
124
125
    /**
126
     * Adjust number of bytes to read according content length header
127
     *
128
     * @param int $numBytes
129
     * @return int
130
     */
131 18
    private function adjustNumbytes($numBytes): int
132
    {
133 18
        if (isset($this->headers['Content-Length'])) {
134
            // Try to read the whole payload at once
135 18
            $numBytes = intval($this->headers['Content-Length']);
136
        }
137
        
138 18
        return $numBytes;
139
    }
140
141
    /**
142
     * Retrieve content type from headers
143
     * 
144
     * @return string
145
     */
146 18
    private function getContentEncoding(): string
147
    {
148 18
        return $this->getHeader('Content-Encoding');
149
    }
150
151
    /**
152
     * Retrieve an given header
153
     *
154
     * @param string $name
155
     * @return string
156
     */
157 18
    private function getHeader(string $name): string
158
    {
159 18
        $result = "";
160
        
161 18
        if (Arrays::hasElement($this->headers, $name)) {
162 18
            $result = $this->headers[$name];
163
        }
164
        
165 18
        return $result;
166
    }
167
}
168