Passed
Push — master ( a92d5c...8cd674 )
by Alexandre
02:54
created

CurlResponse::headers()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * php-guard/curl <https://github.com/php-guard/curl>
4
 * Copyright (C) ${YEAR} by Alexandre Le Borgne <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace PhpGuard\Curl;
21
22
class CurlResponse
23
{
24
    const JSON_PATTERN = '/^(?:application|text)\/(?:[a-z]+(?:[\.-][0-9a-z]+){0,}[\+\.]|x-)?json(?:-[a-z]+)?/i';
25
    const XML_PATTERN = '~^(?:text/|application/(?:atom\+|rss\+)?)xml~i';
26
27
    /**
28
     * @var string
29
     */
30
    private $rawResponse;
31
32
    /**
33
     * @var array
34
     */
35
    private $headers;
36
37
    /**
38
     * @var int
39
     */
40
    private $statusCode;
41
    /**
42
     * @var array
43
     */
44
    private $info;
45
46
    /**
47
     * CurlResponse constructor.
48
     *
49
     * @param int $statusCode
50
     * @param string $rawResponse
51
     * @param array $headers
52
     * @param array $info
53
     */
54 13
    public function __construct(int $statusCode, string $rawResponse, array $headers, array $info)
55
    {
56 13
        $this->statusCode = $statusCode;
57 13
        $this->rawResponse = $rawResponse;
58 13
        $this->headers = new Headers($headers);
0 ignored issues
show
Documentation Bug introduced by
It seems like new PhpGuard\Curl\Headers($headers) of type PhpGuard\Curl\Headers is incompatible with the declared type array of property $headers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
59 13
        $this->info = $info;
60 13
    }
61
62
    /**
63
     * @return int
64
     */
65 13
    public function statusCode(): int
66
    {
67 13
        return $this->statusCode;
68
    }
69
70 1
    public function isError(): bool
71
    {
72 1
        return $this->statusCode >= 300;
73
    }
74
75
    /**
76
     * @return Headers
77
     */
78
    public function headers(): Headers
79
    {
80
        return $this->headers;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->headers returns the type array which is incompatible with the type-hinted return PhpGuard\Curl\Headers.
Loading history...
81
    }
82
83 2
    public function raw()
84
    {
85 2
        return $this->rawResponse;
86
    }
87
88 12
    public function json()
89
    {
90 12
        if (!preg_match(self::JSON_PATTERN, $this->headers['Content-Type'])) {
91 1
            return false;
92
        }
93
94 11
        return json_decode($this->rawResponse, true);
95
    }
96
}
97