Passed
Push — master ( 53af6f...4b20bd )
by Alexander
02:25 queued 10s
created

HTTPFunctions   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 16

8 Methods

Rating   Name   Duplication   Size   Complexity  
A hasHeader() 0 3 1
A http_response_code() 0 6 2
A header() 0 11 5
A headers_list() 0 9 3
A header_remove() 0 6 2
A set_headers_sent() 0 5 1
A headers_sent() 0 5 1
A reset() 0 7 1
1
<?php
2
/**
3
 * This class used to override some header*() functions and http_response_code()
4
 *
5
 * We put these into the Yii namespace, so that Yiisoft\Yii\Web\Emitter will use these versions of header*() and
6
 * http_response_code() when we test its output.
7
 */
8
9
declare(strict_types=1);
10
11
namespace Yiisoft\Yii\Web\Tests\Emitter\Support;
12
13
class HTTPFunctions
14
{
15
    /** @var string[][] */
16
    private static $headers = [];
17
    /** @var int */
18
    private static $responseCode = 200;
19
    private static $headersSent = false;
20
    private static string $headersSentFile = '';
21
    private static int $headersSentLine = 0;
22
23
    /**
24
     * Reset state
25
     */
26
    public static function reset(): void
27
    {
28
        self::$headers = [];
29
        self::$responseCode = 200;
30
        self::$headersSent = false;
31
        self::$headersSentFile = '';
32
        self::$headersSentLine = 0;
33
    }
34
35
    /**
36
     * Set header_sent() state
37
     */
38
    public static function set_headers_sent(bool $value = false, string $file = '', int $line = 0): void
39
    {
40
        static::$headersSent = $value;
0 ignored issues
show
Bug introduced by
Since $headersSent is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $headersSent to at least protected.
Loading history...
41
        static::$headersSentFile = $file;
0 ignored issues
show
Bug introduced by
Since $headersSentFile is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $headersSentFile to at least protected.
Loading history...
42
        static::$headersSentLine = $line;
0 ignored issues
show
Bug introduced by
Since $headersSentLine is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $headersSentLine to at least protected.
Loading history...
43
    }
44
45
    /**
46
     * Check if headers have been sent
47
     */
48
    public static function headers_sent(&$file = null, &$line = null): bool
49
    {
50
        $file = static::$headersSentFile;
0 ignored issues
show
Bug introduced by
Since $headersSentFile is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $headersSentFile to at least protected.
Loading history...
51
        $line = static::$headersSentLine;
0 ignored issues
show
Bug introduced by
Since $headersSentLine is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $headersSentLine to at least protected.
Loading history...
52
        return static::$headersSent;
0 ignored issues
show
Bug introduced by
Since $headersSent is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $headersSent to at least protected.
Loading history...
53
    }
54
55
    /**
56
     * Send a raw HTTP header
57
     */
58
    public static function header(string $string, bool $replace = true, ?int $http_response_code = null): void
59
    {
60
        if (strpos($string, 'HTTP/') !== 0) {
61
            $header = strtolower(explode(':', $string, 2)[0]);
62
            if ($replace || !array_key_exists($header, self::$headers)) {
63
                self::$headers[$header] = [];
64
            }
65
            self::$headers[$header][] = $string;
66
        }
67
        if ($http_response_code !== null) {
68
            self::$responseCode = $http_response_code;
69
        }
70
    }
71
72
    /**
73
     * Remove previously set headers
74
     */
75
    public static function header_remove(?string $header = null): void
76
    {
77
        if ($header === null) {
78
            self::$headers = [];
79
        } else {
80
            unset(self::$headers[strtolower($header)]);
81
        }
82
    }
83
84
    /**
85
     * Returns a list of response headers sent
86
     *
87
     * @return string[]
88
     */
89
    public static function headers_list(): array
90
    {
91
        $result = [];
92
        foreach (self::$headers as $values) {
93
            foreach ($values as $header) {
94
                $result[] = $header;
95
            }
96
        }
97
        return $result;
98
    }
99
100
    /**
101
     * Get or Set the HTTP response code
102
     */
103
    public static function http_response_code(?int $response_code = null): int
104
    {
105
        if ($response_code !== null) {
106
            self::$responseCode = $response_code;
107
        }
108
        return self::$responseCode;
109
    }
110
111
    /**
112
     * Check header is exists
113
     */
114
    public static function hasHeader(string $header): bool
115
    {
116
        return key_exists(strtolower($header), self::$headers);
117
    }
118
}
119