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 |
||
16 | View Code Duplication | class Timestamp implements QueryInterface |
|
|
|||
17 | { |
||
18 | use FileFieldsTrait; |
||
19 | |||
20 | /** @var string Possible values: pdf, adoc, mdoc */ |
||
21 | private $type; |
||
22 | |||
23 | /** @var string file path */ |
||
24 | private $path; |
||
25 | |||
26 | /** |
||
27 | * @param string $type Possible values: pdf, adoc, mdoc |
||
28 | * @param string $path |
||
29 | */ |
||
30 | 6 | public function __construct($type, $path) |
|
35 | |||
36 | /** |
||
37 | * API action name, part of full API request url |
||
38 | * @return string |
||
39 | */ |
||
40 | 1 | public function getAction() |
|
44 | |||
45 | /** |
||
46 | * Field and values association used in query |
||
47 | * @return array |
||
48 | */ |
||
49 | 2 | public function getFields() |
|
56 | |||
57 | /** |
||
58 | * Result object for this query result |
||
59 | * @return ResultInterface |
||
60 | */ |
||
61 | 1 | public function createResult() |
|
65 | |||
66 | /** |
||
67 | * Validation constraints for fields |
||
68 | * @return array |
||
69 | */ |
||
70 | 1 | public function getValidationConstraints() |
|
92 | |||
93 | /** |
||
94 | * HTTP method to use |
||
95 | * @return string |
||
96 | */ |
||
97 | 1 | public function getMethod() |
|
101 | } |
||
102 |
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.