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