status_codes()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 77
Code Lines 75

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 75
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 77
rs 8.5454

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
// Define polyfills in local scope.
4
// @see http://php.net/manual/en/migration54.functions.php
5
6
namespace Ahc;
7
8
function hex2bin($hexString)
9
{
10
    // Based on http://php.net/manual/en/function.hex2bin.php#105601
11
12
    if (\strlen($hexString) % 2) {
13
        \trigger_error('hex2bin(): Hexadecimal input string must have an even length', E_USER_WARNING);
14
15
        return false;
16
    }
17
18
    if (!\preg_match('/^[a-f0-9]+$/i', $hexString)) {
19
        \trigger_error('hex2bin(): Input string must be hexadecimal string', E_USER_WARNING);
20
21
        return false;
22
    }
23
24
    return \pack('H*', $hexString);
25
}
26
27
/** @internal */
28
function status_codes()
29
{
30
    return [
31
        100 => 'Continue',
32
        101 => 'Switching Protocols',
33
        102 => 'Processing',
34
        200 => 'OK',
35
        201 => 'Created',
36
        202 => 'Accepted',
37
        203 => 'Non-Authoritative Information',
38
        204 => 'No Content',
39
        205 => 'Reset Content',
40
        206 => 'Partial Content',
41
        207 => 'Multi-Status',
42
        208 => 'Already Reported',
43
        226 => 'IM Used',
44
        300 => 'Multiple Choices',
45
        301 => 'Moved Permanently',
46
        302 => 'Found',
47
        303 => 'See Other',
48
        304 => 'Not Modified',
49
        305 => 'Use Proxy',
50
        306 => 'Switch Proxy',
51
        307 => 'Temporary Redirect',
52
        308 => 'Permanent Redirect',
53
        400 => 'Bad Request',
54
        401 => 'Unauthorized',
55
        402 => 'Payment Required',
56
        403 => 'Forbidden',
57
        404 => 'Not Found',
58
        405 => 'Method Not Allowed',
59
        406 => 'Not Acceptable',
60
        407 => 'Proxy Authentication Required',
61
        408 => 'Request Timeout',
62
        409 => 'Conflict',
63
        410 => 'Gone',
64
        411 => 'Length Required',
65
        412 => 'Precondition Failed',
66
        413 => 'Request Entity Too Large',
67
        414 => 'Request-URI Too Long',
68
        415 => 'Unsupported Media Type',
69
        416 => 'Requested Range Not Satisfiable',
70
        417 => 'Expectation Failed',
71
        418 => 'I\'m a teapot',
72
        419 => 'Authentication Timeout',
73
        420 => 'Enhance Your Calm',
74
        422 => 'Unprocessable Entity',
75
        423 => 'Locked',
76
        424 => 'Failed Dependency',
77
        425 => 'Unordered Collection',
78
        426 => 'Upgrade Required',
79
        428 => 'Precondition Required',
80
        429 => 'Too Many Requests',
81
        431 => 'Request Header Fields Too Large',
82
        444 => 'No Response',
83
        449 => 'Retry With',
84
        450 => 'Blocked by Windows Parental Controls',
85
        451 => 'Unavailable For Legal Reasons',
86
        494 => 'Request Header Too Large',
87
        495 => 'Cert Error',
88
        496 => 'No Cert',
89
        497 => 'HTTP to HTTPS',
90
        499 => 'Client Closed Request',
91
        500 => 'Internal Server Error',
92
        501 => 'Not Implemented',
93
        502 => 'Bad Gateway',
94
        503 => 'Service Unavailable',
95
        504 => 'Gateway Timeout',
96
        505 => 'HTTP Version Not Supported',
97
        506 => 'Variant Also Negotiates',
98
        507 => 'Insufficient Storage',
99
        508 => 'Loop Detected',
100
        509 => 'Bandwidth Limit Exceeded',
101
        510 => 'Not Extended',
102
        511 => 'Network Authentication Required',
103
        598 => 'Network read timeout error',
104
        599 => 'Network connect timeout error',
105
    ];
106
}
107
108
function http_response_code($responseCode = null)
109
{
110
    // Based on http://php.net/manual/en/function.http-response-code.php#107261
111
112
    static $recentCode = null;
113
114
    if (null !== $responseCode && !\is_int($responseCode)) {
115
        \trigger_error('http_response_code() expects parameter 1 to be integer', E_USER_WARNING);
116
117
        return null;
118
    }
119
120
    if (PHP_SAPI === 'cli') {
121
        if (!$responseCode) {
122
            return $recentCode ? $recentCode : false;
123
        }
124
125
        $toReturn   = $recentCode;
126
        $recentCode = $responseCode;
127
128
        if (!$toReturn) {
129
            return true;
130
        }
131
132
        return $toReturn;
133
    }
134
135
    if (!$responseCode) {
136
        return $recentCode ? $recentCode : 200;
137
    }
138
139
    $statusCodes   = status_codes();
140
    $protocol      = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';
141
    $toReturn      = $recentCode;
142
    $statusMessage = isset($statusCodes[$responseCode]) ? ' ' . $statusCodes[$responseCode] : '';
143
144
    \header("{$protocol} {$responseCode}{$statusMessage}");
145
    $recentCode    = $responseCode;
146
147
    return $toReturn ? $toReturn : 200;
148
}
149