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 |
||
30 | View Code Duplication | class Message |
|
|
|||
31 | { |
||
32 | /** |
||
33 | * Message Id |
||
34 | * |
||
35 | * @var int|string |
||
36 | */ |
||
37 | protected $id; |
||
38 | |||
39 | /** |
||
40 | * Message Body |
||
41 | * |
||
42 | * @var string|array |
||
43 | */ |
||
44 | protected $body; |
||
45 | |||
46 | /** |
||
47 | * Message Metadata |
||
48 | * |
||
49 | * @var ArrayCollection |
||
50 | */ |
||
51 | protected $metadata; |
||
52 | |||
53 | /** |
||
54 | * Constructor. |
||
55 | * |
||
56 | * Sets the Message Id, Message Body, and any Message Metadata |
||
57 | * |
||
58 | * @param int|string $id The Message Id |
||
59 | * @param string|array $body The Message Message |
||
60 | * @param array $metadata The Message Metadata |
||
61 | */ |
||
62 | 19 | public function __construct($id, $body, array $metadata) |
|
74 | |||
75 | /** |
||
76 | * Returns the Message Id |
||
77 | * |
||
78 | * @return int|string |
||
79 | */ |
||
80 | 5 | public function getId() |
|
84 | |||
85 | /** |
||
86 | * Returns the Message Body |
||
87 | * |
||
88 | * @return string|array |
||
89 | */ |
||
90 | 4 | public function getBody() |
|
94 | |||
95 | /** |
||
96 | * Returns the Message Metadata |
||
97 | * |
||
98 | * @return ArrayCollection |
||
99 | */ |
||
100 | 3 | public function getMetadata() |
|
104 | } |
||
105 |
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.