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