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 |
||
9 | final class ModelReference implements ModelReferenceInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var string|null |
||
13 | */ |
||
14 | private $id; |
||
15 | |||
16 | /** |
||
17 | * @var ModelInterface|null |
||
18 | */ |
||
19 | private $model; |
||
20 | |||
21 | /** |
||
22 | * @param ModelInterface|null $model |
||
23 | * @return ModelReferenceInterface |
||
24 | */ |
||
25 | 1 | View Code Duplication | public function setModel(ModelInterface $model = null): ModelReferenceInterface |
32 | |||
33 | /** |
||
34 | * @return ModelInterface|null |
||
35 | */ |
||
36 | 1 | public function getModel() |
|
40 | |||
41 | /** |
||
42 | * @return null|string |
||
43 | */ |
||
44 | 2 | public function getId() |
|
48 | |||
49 | /** |
||
50 | * @return ModelInterface|null |
||
51 | */ |
||
52 | 1 | public function getInitialModel() |
|
56 | |||
57 | /** |
||
58 | * @return array|null |
||
59 | */ |
||
60 | 2 | public function jsonSerialize() |
|
70 | |||
71 | /** |
||
72 | * @throws \LogicException |
||
73 | */ |
||
74 | View Code Duplication | private function jsonSerializableOrException() |
|
82 | } |
||
83 |
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.