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 |
||
8 | View Code Duplication | class CreateViewModel extends ViewModel implements CreateViewModelInterface |
|
|
|||
9 | { |
||
10 | /** |
||
11 | * @var EntityInterface |
||
12 | */ |
||
13 | private $entity; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | private $errors; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | private $inputData; |
||
24 | |||
25 | /** |
||
26 | * @return EntityInterface |
||
27 | */ |
||
28 | public function getEntity() |
||
32 | |||
33 | /** |
||
34 | * @param EntityInterface $entity |
||
35 | */ |
||
36 | public function setEntity(EntityInterface $entity) |
||
40 | |||
41 | /** |
||
42 | * @return array |
||
43 | */ |
||
44 | public function getErrors() |
||
48 | |||
49 | /** |
||
50 | * @param array $errors |
||
51 | */ |
||
52 | public function setErrors(array $errors) |
||
56 | |||
57 | /** |
||
58 | * @return array |
||
59 | */ |
||
60 | public function getInputData() |
||
64 | |||
65 | /** |
||
66 | * @param array $inputData |
||
67 | */ |
||
68 | public function setInputData(array $inputData) |
||
72 | } |
||
73 |
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.