| Conditions | 39 |
| Paths | 39 |
| Total Lines | 166 |
| Code Lines | 122 |
| 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 |
||
| 51 | function http_response_code($code = null) { |
||
| 52 | if ($code !== null) { |
||
| 53 | switch ($code) { |
||
| 54 | case 100: |
||
| 55 | $text = 'Continue'; |
||
| 56 | break; |
||
| 57 | |||
| 58 | case 101: |
||
| 59 | $text = 'Switching Protocols'; |
||
| 60 | break; |
||
| 61 | |||
| 62 | case 200: |
||
| 63 | $text = 'OK'; |
||
| 64 | break; |
||
| 65 | |||
| 66 | case 201: |
||
| 67 | $text = 'Created'; |
||
| 68 | break; |
||
| 69 | |||
| 70 | case 202: |
||
| 71 | $text = 'Accepted'; |
||
| 72 | break; |
||
| 73 | |||
| 74 | case 203: |
||
| 75 | $text = 'Non-Authoritative Information'; |
||
| 76 | break; |
||
| 77 | |||
| 78 | case 204: |
||
| 79 | $text = 'No Content'; |
||
| 80 | break; |
||
| 81 | |||
| 82 | case 205: |
||
| 83 | $text = 'Reset Content'; |
||
| 84 | break; |
||
| 85 | |||
| 86 | case 206: |
||
| 87 | $text = 'Partial Content'; |
||
| 88 | break; |
||
| 89 | |||
| 90 | case 300: |
||
| 91 | $text = 'Multiple Choices'; |
||
| 92 | break; |
||
| 93 | |||
| 94 | case 301: |
||
| 95 | $text = 'Moved Permanently'; |
||
| 96 | break; |
||
| 97 | |||
| 98 | case 302: |
||
| 99 | $text = 'Moved Temporarily'; |
||
| 100 | break; |
||
| 101 | |||
| 102 | case 303: |
||
| 103 | $text = 'See Other'; |
||
| 104 | break; |
||
| 105 | |||
| 106 | case 304: |
||
| 107 | $text = 'Not Modified'; |
||
| 108 | break; |
||
| 109 | |||
| 110 | case 305: |
||
| 111 | $text = 'Use Proxy'; |
||
| 112 | break; |
||
| 113 | |||
| 114 | case 400: |
||
| 115 | $text = 'Bad Request'; |
||
| 116 | break; |
||
| 117 | |||
| 118 | case 401: |
||
| 119 | $text = 'Unauthorized'; |
||
| 120 | break; |
||
| 121 | |||
| 122 | case 402: |
||
| 123 | $text = 'Payment Required'; |
||
| 124 | break; |
||
| 125 | |||
| 126 | case 403: |
||
| 127 | $text = 'Forbidden'; |
||
| 128 | break; |
||
| 129 | |||
| 130 | case 404: |
||
| 131 | $text = 'Not Found'; |
||
| 132 | break; |
||
| 133 | |||
| 134 | case 405: |
||
| 135 | $text = 'Method Not Allowed'; |
||
| 136 | break; |
||
| 137 | |||
| 138 | case 406: |
||
| 139 | $text = 'Not Acceptable'; |
||
| 140 | break; |
||
| 141 | |||
| 142 | case 407: |
||
| 143 | $text = 'Proxy Authentication Required'; |
||
| 144 | break; |
||
| 145 | |||
| 146 | case 408: |
||
| 147 | $text = 'Request Time-out'; |
||
| 148 | break; |
||
| 149 | |||
| 150 | case 409: |
||
| 151 | $text = 'Conflict'; |
||
| 152 | break; |
||
| 153 | |||
| 154 | case 410: |
||
| 155 | $text = 'Gone'; |
||
| 156 | break; |
||
| 157 | |||
| 158 | case 411: |
||
| 159 | $text = 'Length Required'; |
||
| 160 | break; |
||
| 161 | |||
| 162 | case 412: |
||
| 163 | $text = 'Precondition Failed'; |
||
| 164 | break; |
||
| 165 | |||
| 166 | case 413: |
||
| 167 | $text = 'Request Entity Too Large'; |
||
| 168 | break; |
||
| 169 | |||
| 170 | case 414: |
||
| 171 | $text = 'Request-URI Too Large'; |
||
| 172 | break; |
||
| 173 | |||
| 174 | case 415: |
||
| 175 | $text = 'Unsupported Media Type'; |
||
| 176 | break; |
||
| 177 | |||
| 178 | case 500: |
||
| 179 | $text = 'Internal Server Error'; |
||
| 180 | break; |
||
| 181 | |||
| 182 | case 501: |
||
| 183 | $text = 'Not Implemented'; |
||
| 184 | break; |
||
| 185 | |||
| 186 | case 502: |
||
| 187 | $text = 'Bad Gateway'; |
||
| 188 | break; |
||
| 189 | |||
| 190 | case 503: |
||
| 191 | $text = 'Service Unavailable'; |
||
| 192 | break; |
||
| 193 | |||
| 194 | case 504: |
||
| 195 | $text = 'Gateway Time-out'; |
||
| 196 | break; |
||
| 197 | |||
| 198 | case 505: |
||
| 199 | $text = 'HTTP Version not supported'; |
||
| 200 | break; |
||
| 201 | |||
| 202 | default: |
||
| 203 | exit('Unknown http status code "' . htmlentities($code) . '"'); |
||
|
|
|||
| 204 | break; |
||
| 205 | } |
||
| 206 | |||
| 207 | $protocol = ($_SERVER['SERVER_PROTOCOL'] ?? 'HTTP/1.0'); |
||
| 208 | header($protocol . ' ' . $code . ' ' . $text); |
||
| 209 | |||
| 210 | $GLOBALS['http_response_code'] = $code; |
||
| 211 | } |
||
| 212 | else { |
||
| 213 | $code = ($GLOBALS['http_response_code'] ?? 200); |
||
| 214 | } |
||
| 215 | |||
| 216 | return $code; |
||
| 217 | } |
||
| 242 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.