Conditions | 18 |
Paths | 147 |
Total Lines | 140 |
Code Lines | 110 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
11 | function print_error_page() |
||
12 | { |
||
13 | $status_reason = [ |
||
14 | 100 => 'Continue', |
||
15 | 101 => 'Switching Protocols', |
||
16 | 102 => 'Processing', |
||
17 | 200 => 'OK', |
||
18 | 201 => 'Created', |
||
19 | 202 => 'Accepted', |
||
20 | 203 => 'Non-Authoritative Information', |
||
21 | 204 => 'No Content', |
||
22 | 205 => 'Reset Content', |
||
23 | 206 => 'Partial Content', |
||
24 | 207 => 'Multi-Status', |
||
25 | 226 => 'IM Used', |
||
26 | 300 => 'Multiple Choices', |
||
27 | 301 => 'Moved Permanently', |
||
28 | 302 => 'Found', |
||
29 | 303 => 'See Other', |
||
30 | 304 => 'Not Modified', |
||
31 | 305 => 'Use Proxy', |
||
32 | 306 => 'Reserved', |
||
33 | 307 => 'Temporary Redirect', |
||
34 | 400 => 'Bad Request', |
||
35 | 401 => 'Unauthorized', |
||
36 | 402 => 'Payment Required', |
||
37 | 403 => 'Forbidden', |
||
38 | 404 => 'Not Found', |
||
39 | 405 => 'Method Not Allowed', |
||
40 | 406 => 'Not Acceptable', |
||
41 | 407 => 'Proxy Authentication Required', |
||
42 | 408 => 'Request Timeout', |
||
43 | 409 => 'Conflict', |
||
44 | 410 => 'Gone', |
||
45 | 411 => 'Length Required', |
||
46 | 412 => 'Precondition Failed', |
||
47 | 413 => 'Request Entity Too Large', |
||
48 | 414 => 'Request-URI Too Long', |
||
49 | 415 => 'Unsupported Media Type', |
||
50 | 416 => 'Requested Range Not Satisfiable', |
||
51 | 417 => 'Expectation Failed', |
||
52 | 422 => 'Unprocessable Entity', |
||
53 | 423 => 'Locked', |
||
54 | 424 => 'Failed Dependency', |
||
55 | 426 => 'Upgrade Required', |
||
56 | 500 => 'Internal Server Error', |
||
57 | 501 => 'Not Implemented', |
||
58 | 502 => 'Bad Gateway', |
||
59 | 503 => 'Service Unavailable', |
||
60 | 504 => 'Gateway Timeout', |
||
61 | 505 => 'HTTP Version Not Supported', |
||
62 | 506 => 'Variant Also Negotiates', |
||
63 | 507 => 'Insufficient Storage', |
||
64 | 510 => 'Not Extended', |
||
65 | ]; |
||
66 | |||
67 | $status_msg = [ |
||
68 | 400 => 'Your browser sent a request that this server could not understand.', |
||
69 | 401 => 'This server could not verify that you are authorized to access the document requested.', |
||
70 | 402 => 'The server encountered an internal error or misconfiguration and was unable to complete your request.', |
||
71 | 403 => "You don't have permission to access %U% on this server.", |
||
72 | 404 => "We couldn't find <acronym title='%U%'>that uri</acronym> on our server, though it's most certainly not your fault.", |
||
73 | 405 => 'The requested method is not allowed for the URL %U%.', |
||
74 | 406 => 'An appropriate representation of the requested resource %U% could not be found on this server.', |
||
75 | 407 => 'An appropriate representation of the requested resource %U% could not be found on this server.', |
||
76 | 408 => 'Server timeout waiting for the HTTP request from the client.', |
||
77 | 409 => 'The server encountered an internal error or misconfiguration and was unable to complete your request.', |
||
78 | 410 => 'The requested resource %U% is no longer available on this server and there is no forwarding address. Please remove all references to this resource.', |
||
79 | 411 => 'A request of the requested method GET requires a valid Content-length.', |
||
80 | 412 => 'The precondition on the request for the URL %U% evaluated to false.', |
||
81 | 413 => 'The requested resource %U% does not allow request data with GET requests, or the amount of data provided in the request exceeds the capacity limit.', |
||
82 | 414 => "The requested URL's length exceeds the capacity limit for this server.", |
||
83 | 415 => 'The supplied request data is not in a format acceptable for processing by this resource.', |
||
84 | 416 => 'Requested Range Not Satisfiable', |
||
85 | 417 => 'The expectation given in the Expect request-header field could not be met by this server. The client sent <code>Expect:</code>', |
||
86 | 422 => 'The server understands the media type of the request entity, but was unable to process the contained instructions.', |
||
87 | 423 => 'The requested resource is currently locked. The lock must be released or proper identification given before the method can be applied.', |
||
88 | 424 => 'The method could not be performed on the resource because the requested action depended on another action and that other action failed.', |
||
89 | 425 => 'The server encountered an internal error or misconfiguration and was unable to complete your request.', |
||
90 | 426 => 'The requested resource can only be retrieved using SSL. Either upgrade your client, or try requesting the page using https://', |
||
91 | 500 => 'The server encountered an internal error or misconfiguration and was unable to complete your request.', |
||
92 | 501 => 'This type of request method to %U% is not supported.', |
||
93 | 502 => 'The proxy server received an invalid response from an upstream server.', |
||
94 | 503 => 'The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.', |
||
95 | 504 => 'The proxy server did not receive a timely response from the upstream server.', |
||
96 | 505 => 'The server encountered an internal error or misconfiguration and was unable to complete your request.', |
||
97 | 506 => 'A variant for the requested resource <code>%U%</code> is itself a negotiable resource. This indicates a configuration error.', |
||
98 | 507 => 'The method could not be performed. There is insufficient free space left in your storage allocation.', |
||
99 | 510 => 'A mandatory extension policy in the request is not accepted by the server for this resource.', |
||
100 | ]; |
||
101 | |||
102 | // Get the Status Code |
||
103 | if (Request::hasVar('REDIRECT_STATUS', 'SERVER') && (200 != $_SERVER['REDIRECT_STATUS'])) { |
||
104 | $sc = $_SERVER['REDIRECT_STATUS']; |
||
|
|||
105 | } elseif (Request::hasVar('REDIRECT_REDIRECT_STATUS', 'SERVER') && (200 != $_SERVER['REDIRECT_REDIRECT_STATUS'])) { |
||
106 | $sc = $_SERVER['REDIRECT_REDIRECT_STATUS']; |
||
107 | } |
||
108 | $sc = ($_GET['error'] ?? 404); |
||
109 | |||
110 | $sc = abs($sc); |
||
111 | |||
112 | // Redirect to server home if called directly or if status is under 400 |
||
113 | if (((isset($_SERVER['REDIRECT_STATUS']) && 200 == $_SERVER['REDIRECT_STATUS']) && (3 == floor($sc / 100))) |
||
114 | || (!isset($_GET['error']) && 200 == $_SERVER['REDIRECT_STATUS'])) { |
||
115 | @header("Location: http://{$_SERVER['SERVER_NAME']}", 1, 302); |
||
116 | exit(); |
||
117 | } |
||
118 | |||
119 | // Check range of code or issue 500 |
||
120 | if (($sc < 200) || ($sc > 599)) { |
||
121 | $sc = 500; |
||
122 | } |
||
123 | |||
124 | // Check for valid protocols or else issue 505 |
||
125 | if (!in_array($_SERVER['SERVER_PROTOCOL'], ['HTTP/1.0', 'HTTP/1.1', 'HTTP/0.9'])) { |
||
126 | $sc = 505; |
||
127 | } |
||
128 | |||
129 | // Get the status reason |
||
130 | $reason = ($status_reason[$sc] ?? ''); |
||
131 | |||
132 | // Get the status message |
||
133 | $msg = (isset($status_msg[$sc]) ? str_replace('%U%', htmlspecialchars(strip_tags(stripslashes($_SERVER['REQUEST_URI'])), ENT_QUOTES | ENT_HTML5), $status_msg[$sc]) : 'Error'); |
||
134 | |||
135 | // issue optimized headers (optimized for your server) |
||
136 | @header("{$_SERVER['SERVER_PROTOCOL']} {$sc} {$reason}", 1, $sc); |
||
137 | if ('cgi-fcgi' !== @php_sapi_name()) { |
||
138 | @header("Status: {$sc} {$reason}", 1, $sc); |
||
139 | } |
||
140 | |||
141 | // A very small footprint for certain types of 4xx class errors and all 5xx class errors |
||
142 | if (in_array($sc, [400, 403, 405]) || (5 == floor($sc / 100))) { |
||
143 | @header('Connection: close', 1); |
||
144 | if (405 == $sc) { |
||
145 | @header('Allow: GET,HEAD,POST,OPTIONS', 1, 405); |
||
146 | } |
||
147 | } |
||
148 | |||
149 | echo "<!DOCTYPE HTML>\n<html>"; |
||
150 | echo "<head>\n<title>{$sc} {$reason}</title>\n<h1>{$reason}</h1>\n<p>{$msg}<br>\n</p>\n"; |
||
151 | } |
||
219 |