Conditions | 41 |
Paths | 77 |
Total Lines | 93 |
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 |
||
47 | function http_response_code($code = NULL) { |
||
48 | if ($code !== NULL) { |
||
49 | switch ($code) { |
||
50 | case 100: $text = 'Continue'; |
||
51 | break; |
||
52 | case 101: $text = 'Switching Protocols'; |
||
53 | break; |
||
54 | case 200: $text = 'OK'; |
||
55 | break; |
||
56 | case 201: $text = 'Created'; |
||
57 | break; |
||
58 | case 202: $text = 'Accepted'; |
||
59 | break; |
||
60 | case 203: $text = 'Non-Authoritative Information'; |
||
61 | break; |
||
62 | case 204: $text = 'No Content'; |
||
63 | break; |
||
64 | case 205: $text = 'Reset Content'; |
||
65 | break; |
||
66 | case 206: $text = 'Partial Content'; |
||
67 | break; |
||
68 | case 300: $text = 'Multiple Choices'; |
||
69 | break; |
||
70 | case 301: $text = 'Moved Permanently'; |
||
71 | break; |
||
72 | case 302: $text = 'Moved Temporarily'; |
||
73 | break; |
||
74 | case 303: $text = 'See Other'; |
||
75 | break; |
||
76 | case 304: $text = 'Not Modified'; |
||
77 | break; |
||
78 | case 305: $text = 'Use Proxy'; |
||
79 | break; |
||
80 | case 400: $text = 'Bad Request'; |
||
81 | break; |
||
82 | case 401: $text = 'Unauthorized'; |
||
83 | break; |
||
84 | case 402: $text = 'Payment Required'; |
||
85 | break; |
||
86 | case 403: $text = 'Forbidden'; |
||
87 | break; |
||
88 | case 404: $text = 'Not Found'; |
||
89 | break; |
||
90 | case 405: $text = 'Method Not Allowed'; |
||
91 | break; |
||
92 | case 406: $text = 'Not Acceptable'; |
||
93 | break; |
||
94 | case 407: $text = 'Proxy Authentication Required'; |
||
95 | break; |
||
96 | case 408: $text = 'Request Time-out'; |
||
97 | break; |
||
98 | case 409: $text = 'Conflict'; |
||
99 | break; |
||
100 | case 410: $text = 'Gone'; |
||
101 | break; |
||
102 | case 411: $text = 'Length Required'; |
||
103 | break; |
||
104 | case 412: $text = 'Precondition Failed'; |
||
105 | break; |
||
106 | case 413: $text = 'Request Entity Too Large'; |
||
107 | break; |
||
108 | case 414: $text = 'Request-URI Too Large'; |
||
109 | break; |
||
110 | case 415: $text = 'Unsupported Media Type'; |
||
111 | break; |
||
112 | case 500: $text = 'Internal Server Error'; |
||
113 | break; |
||
114 | case 501: $text = 'Not Implemented'; |
||
115 | break; |
||
116 | case 502: $text = 'Bad Gateway'; |
||
117 | break; |
||
118 | case 503: $text = 'Service Unavailable'; |
||
119 | break; |
||
120 | case 504: $text = 'Gateway Time-out'; |
||
121 | break; |
||
122 | case 505: $text = 'HTTP Version not supported'; |
||
123 | break; |
||
124 | default: |
||
125 | exit('Unknown http status code "' . htmlentities($code) . '"'); |
||
126 | break; |
||
127 | } |
||
128 | |||
129 | $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); |
||
130 | |||
131 | header($protocol . ' ' . $code . ' ' . $text); |
||
132 | |||
133 | $GLOBALS['http_response_code'] = $code; |
||
134 | } else { |
||
135 | $code = (isset($GLOBALS['http_response_code']) ? $GLOBALS['http_response_code'] : 200); |
||
136 | } |
||
137 | |||
138 | return $code; |
||
139 | } |
||
140 | } |
||
184 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.