StdoutWriter::header()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 10
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 5
rs 9.6111
1
<?php
2
3
namespace ByJG\RestServer\Writer;
4
5
class StdoutWriter implements WriterInterface
6
{
7
    protected $headerList = [];
8
    protected $data = '';
9
    protected $statusCode = 0;
10
11 16
    public function header($header, $replace = true)
12
    {
13 16
        if (preg_match("~^HTTP/~", $header) === 1) {
14 15
            array_unshift($this->headerList, $header);
15 15
            return;
16
        }
17
18 12
        if ($replace) {
19 12
            $headerParts = explode(':', $header);
20 12
            for ($i=0; $i<count($this->headerList); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
21 8
                if (preg_match("~^" . $headerParts[0] . "~", $this->headerList[$i]) === 1) {
22 7
                    $this->headerList[$i] = $header;
23 7
                    return;
24
                }
25
            }
26
        }
27
28 12
        $this->headerList[] = $header;
29
    }
30
31 15
    public function responseCode($responseCode, $description)
32
    {
33 15
        $this->header("HTTP/1.1 $responseCode $description");
34 15
        $this->statusCode = $responseCode;
35
    }
36
37 15
    public function echo($data)
38
    {
39 15
        $this->data .= $data;
40
    }
41
42
    public function flush()
43
    {
44
        echo implode("\r\n", $this->headerList);
45
        echo "\r\n";
46
        echo "\r\n";
47
        echo $this->data;
48
    }
49
}