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 |
||
7 | trait HttpErrors |
||
8 | { |
||
9 | |||
10 | public function handle404($data, string $type = 'db', array $page = []) |
||
22 | |||
23 | View Code Duplication | public function error404() |
|
31 | |||
32 | View Code Duplication | public function error503() |
|
40 | |||
41 | View Code Duplication | public function error500() |
|
49 | |||
50 | /** |
||
51 | * @param null $target |
||
52 | * |
||
53 | * @return mixed |
||
54 | */ |
||
55 | public abstract function redirect($target = null); |
||
56 | |||
57 | /** |
||
58 | * @param string $template |
||
59 | * @param array $params |
||
60 | */ |
||
61 | public abstract function twig(string $template, array $params = []): void; |
||
62 | |||
63 | /** |
||
64 | * @param string $key |
||
65 | * |
||
66 | * @return string|array |
||
67 | */ |
||
68 | public abstract function data(string $key = null); |
||
69 | } |
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.