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 |
||
11 | class TemplateDispatcher implements DispatcherInterface |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var Router |
||
16 | */ |
||
17 | private $router; |
||
18 | |||
19 | /** |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $types = [ |
||
23 | 'json' => 'application/json', |
||
24 | 'xml' => 'application/xml', |
||
25 | 'txt' => 'text/plain', |
||
26 | 'html' => 'text/html' |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * @param Router $router |
||
31 | */ |
||
32 | public function __construct(Router $router) |
||
36 | |||
37 | /** |
||
38 | * @description call template file |
||
39 | */ |
||
40 | public function call() |
||
56 | |||
57 | /** |
||
58 | * @param $extension |
||
59 | */ |
||
60 | public function setContentType($extension) |
||
66 | |||
67 | } |
||
68 |
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.