Total Complexity | 8 |
Total Lines | 43 |
Duplicated Lines | 0 % |
Coverage | 76.19% |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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++) { |
|
|
|||
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() |
||
48 | } |
||
49 | } |
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: