Issues (15)

src/Writer/StdoutWriter.php (1 issue)

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
}