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 |
||
42 | class CommandHandler extends OfferCommandHandler implements LoggerAwareInterface |
||
43 | { |
||
44 | use LoggerAwareTrait; |
||
45 | |||
46 | /** |
||
47 | * @return string |
||
48 | */ |
||
49 | protected function getAddLabelClassName() |
||
50 | { |
||
51 | return AddLabel::class; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return string |
||
56 | */ |
||
57 | protected function getRemoveLabelClassName() |
||
58 | { |
||
59 | return RemoveLabel::class; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | protected function getAddImageClassName() |
||
66 | { |
||
67 | return AddImage::class; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * @return string |
||
72 | */ |
||
73 | protected function getUpdateImageClassName() |
||
74 | { |
||
75 | return UpdateImage::class; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @return string |
||
80 | */ |
||
81 | protected function getRemoveImageClassName() |
||
82 | { |
||
83 | return RemoveImage::class; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return string |
||
88 | */ |
||
89 | protected function getSelectMainImageClassName() |
||
90 | { |
||
91 | return SelectMainImage::class; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return string |
||
96 | */ |
||
97 | protected function getUpdateTitleClassName() |
||
98 | { |
||
99 | return UpdateTitle::class; |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | protected function getUpdateDescriptionClassName() |
||
109 | |||
110 | /** |
||
111 | * @inheritdoc |
||
112 | */ |
||
113 | protected function getUpdateCalendarClassName() |
||
114 | { |
||
115 | return UpdateCalendar::class; |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * @return string |
||
120 | */ |
||
121 | protected function getUpdateTypicalAgeRangeClassName() |
||
125 | |||
126 | /** |
||
127 | * @return string |
||
128 | */ |
||
129 | protected function getDeleteTypicalAgeRangeClassName() |
||
133 | |||
134 | /** |
||
135 | * @return string |
||
136 | */ |
||
137 | protected function getUpdateOrganizerClassName() |
||
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | protected function getDeleteOrganizerClassName() |
||
149 | |||
150 | /** |
||
151 | * @return string |
||
152 | */ |
||
153 | protected function getUpdateContactPointClassName() |
||
157 | |||
158 | /** |
||
159 | * @return string |
||
160 | */ |
||
161 | protected function getUpdateBookingInfoClassName() |
||
165 | |||
166 | /** |
||
167 | * @return string |
||
168 | */ |
||
169 | protected function getUpdatePriceInfoClassName() |
||
173 | |||
174 | /** |
||
175 | * @return string |
||
176 | */ |
||
177 | protected function getDeleteOfferClassName() |
||
181 | |||
182 | protected function getPublishClassName() |
||
186 | |||
187 | protected function getApproveClassName() |
||
191 | |||
192 | protected function getRejectClassName() |
||
196 | |||
197 | protected function getFlagAsDuplicateClassName() |
||
201 | |||
202 | protected function getFlagAsInappropriateClassName() |
||
206 | |||
207 | /** |
||
208 | * @param CreatePlace $command |
||
209 | */ |
||
210 | View Code Duplication | protected function handleCreatePlace(CreatePlace $command) |
|
225 | |||
226 | /** |
||
227 | * @param UpdateAddress $updateAddress |
||
228 | */ |
||
229 | protected function handleUpdateAddress(UpdateAddress $updateAddress) |
||
236 | |||
237 | /** |
||
238 | * Handle an update the major info command. |
||
239 | * @param UpdateMajorInfo $updateMajorInfo |
||
240 | */ |
||
241 | View Code Duplication | public function handleUpdateMajorInfo(UpdateMajorInfo $updateMajorInfo) |
|
258 | |||
259 | protected function getUpdateTypeClassName() |
||
263 | |||
264 | protected function getUpdateThemeClassName() |
||
268 | |||
269 | /** |
||
270 | * @inheritdoc |
||
271 | */ |
||
272 | protected function getUpdateFacilitiesClassName() |
||
276 | } |
||
277 |
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.