Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
28 | class Turbolinks |
||
29 | { |
||
30 | /** |
||
31 | * Request header used for origin validation. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | const ORIGIN_REQUEST_HEADER = 'Turbolinks-Referrer'; |
||
36 | |||
37 | /** |
||
38 | * Response header used for origin validation. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | const ORIGIN_RESPONSE_HEADER = 'Location'; |
||
43 | |||
44 | /** |
||
45 | * Redirect header inserted in the response. |
||
46 | * |
||
47 | * @var string |
||
48 | */ |
||
49 | const REDIRECT_RESPONSE_HEADER = 'Turbolinks-Location'; |
||
50 | |||
51 | /** |
||
52 | * Session attribute name for the redirect location. |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | const LOCATION_SESSION_ATTR_NAME = 'helthe_turbolinks_location'; |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | public static $turbolinksOptionsMap = array( |
||
62 | // Handles normal redirection with Turbolinks, if not set to `false`. |
||
63 | // Possible values: `null`, `'replace'`, `'advance'` or `false` |
||
64 | 'turbolinks' => 'X-Turbolinks', |
||
65 | ); |
||
66 | |||
67 | /** |
||
68 | * Modifies the HTTP headers and status code of the Response so that it can be |
||
69 | * properly handled by the Turbolinks javascript. |
||
70 | * |
||
71 | * @param Request $request |
||
72 | * @param Response $response |
||
73 | */ |
||
74 | public function decorateResponse(Request $request, Response $response) |
||
88 | |||
89 | /** |
||
90 | * @param Request $request |
||
91 | * @param Response $response |
||
92 | * @return \Symfony\Component\HttpFoundation\Response |
||
93 | */ |
||
94 | public function redirectTo($request, $response) |
||
111 | |||
112 | /** |
||
113 | * Checks if the request can handle a Turbolink redirect. You need to have a |
||
114 | * session and a XHR request header to handle a redirect. |
||
115 | * |
||
116 | * @param Request $request |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | private function canHandleRedirect(Request $request) |
||
125 | |||
126 | /** |
||
127 | * Parse the given url into an origin array with the scheme, host and port. |
||
128 | * |
||
129 | * @param string $url |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | private function getUrlOrigin($url) |
||
141 | |||
142 | /** |
||
143 | * Checks if the request and the response have the same origin. |
||
144 | * |
||
145 | * @param Request $request |
||
146 | * @param Response $response |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | private function haveSameOrigin(Request $request, Response $response) |
||
157 | |||
158 | /** |
||
159 | * Modifies the response status code. Checks for cross domain redirects and |
||
160 | * blocks them. |
||
161 | * |
||
162 | * @param Request $request |
||
163 | * @param Response $response |
||
164 | */ |
||
165 | private function modifyStatusCode(Request $request, Response $response) |
||
174 | |||
175 | /** |
||
176 | * @param ResponseHeaderBag $headers |
||
177 | * @return mixed |
||
178 | */ |
||
179 | private function extractTurbolinksOptions($headers) |
||
194 | |||
195 | private function visitLocationWithTurbolinks($location, $action) |
||
207 | |||
208 | /** |
||
209 | * @param Request $request |
||
210 | * @param Response $response |
||
211 | */ |
||
212 | View Code Duplication | private function storeTurbolinksLocationInSession(Request $request, Response $response) |
|
225 | |||
226 | /** |
||
227 | * @param Request $request |
||
228 | * @param Response $response |
||
229 | * @param string $body Content of the response |
||
230 | */ |
||
231 | private function performTurbolinksResponse(Request $request, Response $response, $body) |
||
237 | |||
238 | /** |
||
239 | * @param Request $request |
||
240 | * @param Response $response |
||
241 | */ |
||
242 | View Code Duplication | private function setTurbolinksLocationHeaderFromSession(Request $request, Response $response) |
|
253 | |||
254 | /** |
||
255 | * @param ResponseHeaderBag $headers |
||
256 | * |
||
257 | * @return array Turbolinks options |
||
258 | */ |
||
259 | public function extractTurbolinksHeaders($headers) |
||
277 | |||
278 | /** |
||
279 | * Return HTTP headers equivalent of the given Turbolinks options. |
||
280 | * E.G. `['turbolinks' => 'advance']` becomes `['X-Turbolinks' => 'advance']` |
||
281 | * |
||
282 | * @param array $options |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | public function convertTurbolinksOptions($options = array()) |
||
299 | } |
||
300 |
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.