Test Failed
Push — master ( 3e9160...6b5548 )
by Jairo
02:47
created

AbstractHttpParser::get()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 3
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
/**
3
 * Author: jairo.rodriguez <[email protected]>
4
 */
5
6
namespace BFunky\HttpParser;
7
8
use BFunky\HttpParser\Entity\HttpDataValidation;
9
use BFunky\HttpParser\Entity\HttpField;
10
use BFunky\HttpParser\Entity\HttpFieldCollection;
11
use BFunky\HttpParser\Entity\HttpHeaderInterface;
12
use BFunky\HttpParser\Exception\HttpFieldNotFoundOnCollection;
13
use BFunky\HttpParser\Exception\HttpParserBadFormatException;
14
15
abstract class AbstractHttpParser implements HttpParserInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $httpRaw;
21
22
    /**
23
     * @var HttpHeaderInterface
24
     */
25
    protected $httpHeader;
26
27
    /**
28
     * @var HttpFieldCollection
29
     */
30
    protected $httpFieldCollection;
31
32
    /**
33
     * HttpParser constructor.
34
     * @param HttpFieldCollection $httpFieldCollection
35
     */
36 2
    public function __construct(HttpFieldCollection $httpFieldCollection = null)
37
    {
38 2
        $this->httpFieldCollection = $httpFieldCollection ?? HttpFieldCollection::fromHttpFieldArray([]);
0 ignored issues
show
Documentation Bug introduced by
It seems like $httpFieldCollection ?? ...HttpFieldArray(array()) can also be of type object<self>. However, the property $httpFieldCollection is declared as type object<BFunky\HttpParser...ty\HttpFieldCollection>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
39 2
    }
40
41
    /**
42
     * @param string $rawHttp
43
     */
44 2
    public function parse(string $rawHttp): void
45
    {
46 2
        $this->process($rawHttp);
47
    }
48
49
    /**
50
     * @param string $headerFieldName
51
     * @return string
52
     * @throws HttpFieldNotFoundOnCollection
53
     */
54
    public function get(string $headerFieldName): string
55
    {
56
        $httpField = $this->httpFieldCollection->get($headerFieldName);
57
        return $httpField->getValue();
58
    }
59
60
    /**
61
     * @return HttpHeaderInterface
62
     */
63
    public function getHeader(): HttpHeaderInterface
64
    {
65
        return $this->httpHeader;
66
    }
67
68
    /**
69
     * @param string $rawHttp
70
     * @throws HttpParserBadFormatException
71
     */
72 2
    protected function process(string $rawHttp): void
73
    {
74 2
        $this->setHttpRaw($rawHttp);
75 2
        $this->extract();
76
    }
77
78
    /**
79
     * Split the http string
80
     * @throws HttpParserBadFormatException
81
     */
82 2
    protected function extract(): void
83
    {
84 2
        $headers = explode("\n", $this->httpRaw);
85 2
        foreach ($headers as $i => $headerLine) {
86 2
            if (trim($headerLine) === '') {
87
                continue;
88
            }
89 2
            if (HttpDataValidation::isField($headerLine)) {
90
                $this->addField($headerLine);
91
            } else {
92 2
                $this->addHeader($headerLine);
93
            }
94
        }
95
    }
96
97
    /**
98
     * @param string $headerLine
99
     * @throws HttpParserBadFormatException
100
     */
101 2
    protected function addHeader(string $headerLine): void
102
    {
103 2
        $data = preg_split('/ /', $headerLine);
104 2
        $data = array_merge($data, ['', '', '']);
105 2
        HttpDataValidation::checkHeaderOrRaiseError($data[0], $data[1], $data[2]);
106
        $this->setHttpHeader($data[0], $data[1], $data[2]);
107
    }
108
109
    /**
110
     * @param string $headerLine
111
     */
112
    protected function addField(string $headerLine): void
113
    {
114
        list($fieldKey, $fieldValue) = $this->splitRawLine($headerLine);
115
        $this->httpFieldCollection->add(HttpField::fromKeyAndValue($fieldKey, $fieldValue));
116
    }
117
118
    /**
119
     * @param string $method
120
     * @param string $path
121
     * @param string $protocol
122
     */
123
    abstract protected function setHttpHeader(string $method, string $path, string $protocol): void;
124
125
    /**
126
     * @param string $line
127
     * @return array
128
     */
129
    protected function splitRawLine(string $line): array
130
    {
131
        $parts = [];
132
        if (strpos($line, ': ') !== false) {
133
            $parts = explode(': ', $line);
134
        } else {
135
            if (strpos($line, ':') !== false) {
136
                $parts = explode(':', $line);
137
            }
138
        }
139
        return $parts;
140
    }
141
142
    /**
143
     * @param string $httpRaw
144
     * @return HttpParserInterface
145
     */
146 2
    protected function setHttpRaw(string $httpRaw): HttpParserInterface
147
    {
148 2
        $this->httpRaw = $httpRaw;
149 2
        return $this;
150
    }
151
}