Conditions | 19 |
Paths | 19 |
Total Lines | 142 |
Code Lines | 73 |
Lines | 21 |
Ratio | 14.79 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 namespace Comodojo\Dispatcher\Output; |
||
75 | public function compose() { |
||
76 | |||
77 | $status = $this->response->status()->get(); |
||
78 | |||
79 | $content = $this->response->content(); |
||
80 | |||
81 | $cookies = $this->response->cookies(); |
||
82 | |||
83 | $headers = $this->response->headers(); |
||
84 | |||
85 | $location = $this->response->location(); |
||
86 | |||
87 | // return value, just in case... |
||
88 | $return = $content->get(); |
||
89 | |||
90 | switch ($status) { |
||
91 | |||
92 | case 200: //OK |
||
93 | |||
94 | header('Content-Length: '.$content->length()); |
||
95 | |||
96 | break; |
||
97 | |||
98 | View Code Duplication | case 202: //Accepted |
|
99 | |||
100 | //PLEASE NOTE: according to HTTP/1.1, 202 header SHOULD HAVE status description in body... just in case |
||
101 | header($_SERVER["SERVER_PROTOCOL"].' 202 Accepted'); |
||
102 | header('Status: 202 Accepted'); |
||
103 | header('Content-Length: '.$content->length()); |
||
104 | |||
105 | break; |
||
106 | |||
107 | case 204: //OK - No Content |
||
108 | |||
109 | header($_SERVER["SERVER_PROTOCOL"].' 204 No Content'); |
||
110 | header('Status: 204 No Content'); |
||
111 | header('Content-Length: 0',true); |
||
112 | |||
113 | $return = null; |
||
114 | |||
115 | break; |
||
116 | |||
117 | case 201: //Created |
||
118 | case 301: //Moved Permanent |
||
119 | case 302: //Found |
||
120 | case 303: //See Other |
||
121 | case 307: //Temporary Redirect |
||
122 | |||
123 | header("Location: ".$location->get(),true,$status); |
||
124 | |||
125 | break; |
||
126 | |||
127 | case 304: //Not Modified |
||
128 | |||
129 | $last_modified = $headers->get('Last-Modified'); |
||
130 | |||
131 | if ( is_null($last_modified) ) { |
||
132 | |||
133 | header($_SERVER["SERVER_PROTOCOL"].' 304 Not Modified'); |
||
134 | |||
135 | } else if ( is_int($last_modified) ) { |
||
136 | |||
137 | header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_modified).' GMT', true, 304); |
||
138 | |||
139 | } else { |
||
140 | |||
141 | header('Last-Modified: '.$last_modified, true, 304); |
||
142 | |||
143 | } |
||
144 | |||
145 | header('Content-Length: '.$content->length()); |
||
146 | $headers->remove('Last-Modified'); |
||
147 | |||
148 | break; |
||
149 | |||
150 | View Code Duplication | case 400: //Bad Request |
|
151 | |||
152 | header($_SERVER["SERVER_PROTOCOL"].' 400 Bad Request', true, 400); |
||
153 | header('Content-Length: '.$content->length()); |
||
154 | |||
155 | break; |
||
156 | |||
157 | case 403: |
||
158 | |||
159 | header('Origin not allowed', true, 403); //Not originated from allowed source |
||
160 | |||
161 | break; |
||
162 | |||
163 | View Code Duplication | case 404: //Not Found |
|
164 | |||
165 | header($_SERVER["SERVER_PROTOCOL"].' 404 Not Found'); |
||
166 | header('Status: 404 Not Found'); |
||
167 | header('Content-Length: '.$content->length()); |
||
168 | |||
169 | break; |
||
170 | |||
171 | case 405: //Not allowed |
||
172 | |||
173 | header('Allow: ' . $value, true, 405); |
||
174 | |||
175 | break; |
||
176 | |||
177 | case 500: //Internal Server Error |
||
178 | |||
179 | header('500 Internal Server Error', true, 500); |
||
180 | header('Content-Length: '.$content->length()); |
||
181 | |||
182 | break; |
||
183 | |||
184 | case 501: //Not implemented |
||
185 | |||
186 | header('Allow: ' . $value, true, 501); |
||
187 | |||
188 | break; |
||
189 | |||
190 | case 503: //Service Unavailable |
||
191 | |||
192 | header($_SERVER["SERVER_PROTOCOL"].' 503 Service Temporarily Unavailable'); |
||
193 | header('Status: 503 Service Temporarily Unavailable'); |
||
194 | |||
195 | // if ( $value !== false AND @is_int($value) ) { |
||
196 | // header('Retry-After: '.$value); |
||
197 | // } |
||
198 | |||
199 | break; |
||
200 | |||
201 | default: |
||
202 | |||
203 | header($_SERVER["SERVER_PROTOCOL"].' '.$this->response->status(), true, $status); |
||
204 | header('Content-Length: '.$content->length()); |
||
205 | |||
206 | break; |
||
207 | |||
208 | } |
||
209 | |||
210 | $headers->send(); |
||
211 | |||
212 | $cookies->save(); |
||
213 | |||
214 | return $content; |
||
215 | |||
216 | } |
||
217 | |||
228 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.