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 |
||
11 | class ContactDetail extends Model |
||
12 | { |
||
13 | |||
14 | const TYPE_PHONE = 'phone'; |
||
15 | |||
16 | const TYPE_EMAIL = 'email'; |
||
17 | |||
18 | const TYPE_SKYPE = 'skype'; |
||
19 | |||
20 | const TYPE_ICQ = 'icq'; |
||
21 | |||
22 | const TYPE_FACEBOOK = 'facebook'; |
||
23 | |||
24 | const TYPE_VK = 'vk'; |
||
25 | |||
26 | /** |
||
27 | * @var integer |
||
28 | */ |
||
29 | protected $id; |
||
30 | |||
31 | /** |
||
32 | * @var integer |
||
33 | */ |
||
34 | protected $contactId; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | protected $type; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $data; |
||
45 | |||
46 | |||
47 | /** |
||
48 | * ContactDetail constructor. |
||
49 | * |
||
50 | * @param array $detailData |
||
51 | */ |
||
52 | public function __construct(array $detailData = []) |
||
67 | |||
68 | |||
69 | /** |
||
70 | * @return bool |
||
71 | * @throws LPTrackerSDKException |
||
72 | */ |
||
73 | public function validate() |
||
88 | |||
89 | |||
90 | /** |
||
91 | * @return array |
||
92 | */ |
||
93 | public static function getAllTypes() |
||
104 | |||
105 | |||
106 | /** |
||
107 | * @return array |
||
108 | */ |
||
109 | View Code Duplication | public function toArray() |
|
122 | |||
123 | |||
124 | /** |
||
125 | * @return int |
||
126 | */ |
||
127 | public function getId() |
||
131 | |||
132 | |||
133 | /** |
||
134 | * @return int |
||
135 | */ |
||
136 | public function getContactId() |
||
140 | |||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getType() |
||
149 | |||
150 | |||
151 | /** |
||
152 | * @param string $type |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setType($type) |
||
162 | |||
163 | |||
164 | /** |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getData() |
||
171 | |||
172 | |||
173 | /** |
||
174 | * @param string $data |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function setData($data) |
||
184 | } |
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.