StdoutWriter   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 76.19%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 43
ccs 16
cts 21
cp 0.7619
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A flush() 0 6 1
A echo() 0 3 1
A responseCode() 0 4 1
A header() 0 18 5
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
}