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 |
||
22 | class HttpErrorsController implements Http400HandlerInterface, Http404HandlerInterface, Http500HandlerInterface, Scopable |
||
23 | { |
||
24 | /** |
||
25 | * The template used by Splash for displaying error pages (HTTP 400, 404 and 500). |
||
26 | * |
||
27 | * @var TemplateInterface |
||
28 | */ |
||
29 | private $template; |
||
30 | |||
31 | /** |
||
32 | * The content block the template will be written into. |
||
33 | * |
||
34 | * @var HtmlBlock |
||
35 | */ |
||
36 | private $contentBlock; |
||
37 | |||
38 | /** |
||
39 | * Whether we should display exception stacktrace or not in HTTP 500. |
||
40 | * |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $debugMode = true; |
||
44 | |||
45 | /** |
||
46 | * Content block displayed in case of a 400 error. |
||
47 | * If not set, a default block will be used instead. |
||
48 | * |
||
49 | * @var HtmlElementInterface |
||
50 | */ |
||
51 | protected $contentFor400; |
||
52 | |||
53 | /** |
||
54 | * Content block displayed in case of a 404 error. |
||
55 | * If not set, a default block will be used instead. |
||
56 | * |
||
57 | * @var HtmlElementInterface |
||
58 | */ |
||
59 | protected $contentFor404; |
||
60 | |||
61 | /** |
||
62 | * Content block displayed in case of a 500 error. |
||
63 | * If not set, a default block will be used instead. |
||
64 | * |
||
65 | * @var HtmlElementInterface |
||
66 | */ |
||
67 | protected $contentFor500; |
||
68 | |||
69 | protected $exception; |
||
70 | |||
71 | /** |
||
72 | * @param TemplateInterface $template The template used by Splash for displaying error pages (HTTP 400, 404 and 500). |
||
73 | * @param HtmlBlock $contentBlock The content block the template will be written into. |
||
74 | * @param bool $debugMode Whether we should display exception stacktrace or not in HTTP 500. |
||
75 | */ |
||
76 | public function __construct(TemplateInterface $template, HtmlBlock $contentBlock, bool $debugMode = true) |
||
82 | |||
83 | /** |
||
84 | * Creates a default controller. |
||
85 | * |
||
86 | * @param bool $debugMode |
||
87 | * |
||
88 | * @return HttpErrorsController |
||
89 | */ |
||
90 | public static function createDefault(bool $debugMode = true) |
||
97 | |||
98 | /** |
||
99 | * This function is called when a HTTP 400 error is triggered by the user. |
||
100 | * |
||
101 | * @param ServerRequestInterface $request |
||
102 | * |
||
103 | * @return ResponseInterface |
||
104 | */ |
||
105 | View Code Duplication | public function badRequest(BadRequestException $exception, ServerRequestInterface $request) : ResponseInterface |
|
122 | |||
123 | /** |
||
124 | * This function is called when a HTTP 404 error is triggered by the user. |
||
125 | * |
||
126 | * @param ServerRequestInterface $request |
||
127 | * |
||
128 | * @return ResponseInterface |
||
129 | */ |
||
130 | View Code Duplication | public function pageNotFound(ServerRequestInterface $request) : ResponseInterface |
|
145 | |||
146 | /** |
||
147 | * (non-PHPdoc). |
||
148 | * |
||
149 | * @see Mouf\Mvc\Splash\Controllers.Http500HandlerInterface::serverError() |
||
150 | */ |
||
151 | public function serverError(\Throwable $exception, ServerRequestInterface $request) : ResponseInterface |
||
168 | |||
169 | /** |
||
170 | * Includes the file (useful to load a view inside the Controllers scope). |
||
171 | * |
||
172 | * @param string $file |
||
173 | */ |
||
174 | public function loadFile($file) |
||
178 | |||
179 | /** |
||
180 | * Content block displayed in case of a 400 error. |
||
181 | * If not set, a default block will be used instead. |
||
182 | * |
||
183 | * @param HtmlElementInterface $contentFor400 |
||
184 | * |
||
185 | * @return $this |
||
186 | */ |
||
187 | public function setContentFor400(HtmlElementInterface $contentFor400) |
||
193 | |||
194 | /** |
||
195 | * Content block displayed in case of a 404 error. |
||
196 | * If not set, a default block will be used instead. |
||
197 | * |
||
198 | * @param HtmlElementInterface $contentFor404 |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function setContentFor404(HtmlElementInterface $contentFor404) |
||
208 | |||
209 | /** |
||
210 | * Content block displayed in case of a 500 error. |
||
211 | * If not set, a default block will be used instead. |
||
212 | * |
||
213 | * @param HtmlElementInterface $contentFor500 |
||
214 | * |
||
215 | * @return $this |
||
216 | */ |
||
217 | public function setContentFor500(HtmlElementInterface $contentFor500) |
||
223 | } |
||
224 |
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.