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:
Complex classes like Comment often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Comment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
31 | class Comment implements IComment { |
||
32 | |||
33 | protected $data = [ |
||
34 | 'id' => '', |
||
35 | 'parentId' => '0', |
||
36 | 'topmostParentId' => '0', |
||
37 | 'childrenCount' => '0', |
||
38 | 'message' => '', |
||
39 | 'verb' => '', |
||
40 | 'actorType' => '', |
||
41 | 'actorId' => '', |
||
42 | 'objectType' => '', |
||
43 | 'objectId' => '', |
||
44 | 'creationDT' => null, |
||
45 | 'latestChildDT' => null, |
||
46 | ]; |
||
47 | |||
48 | /** |
||
49 | * Comment constructor. |
||
50 | * |
||
51 | * @param array $data optional, array with keys according to column names from |
||
52 | * the comments database scheme |
||
53 | */ |
||
54 | public function __construct(array $data = null) { |
||
59 | |||
60 | /** |
||
61 | * returns the ID of the comment |
||
62 | * |
||
63 | * It may return an empty string, if the comment was not stored. |
||
64 | * It is expected that the concrete Comment implementation gives an ID |
||
65 | * by itself (e.g. after saving). |
||
66 | * |
||
67 | * @return string |
||
68 | * @since 9.0.0 |
||
69 | */ |
||
70 | public function getId() { |
||
73 | |||
74 | /** |
||
75 | * sets the ID of the comment and returns itself |
||
76 | * |
||
77 | * It is only allowed to set the ID only, if the current id is an empty |
||
78 | * string (which means it is not stored in a database, storage or whatever |
||
79 | * the concrete implementation does), or vice versa. Changing a given ID is |
||
80 | * not permitted and must result in an IllegalIDChangeException. |
||
81 | * |
||
82 | * @param string $id |
||
83 | * @return IComment |
||
84 | * @throws IllegalIDChangeException |
||
85 | * @since 9.0.0 |
||
86 | */ |
||
87 | public function setId($id) { |
||
100 | |||
101 | /** |
||
102 | * returns the parent ID of the comment |
||
103 | * |
||
104 | * @return string |
||
105 | * @since 9.0.0 |
||
106 | */ |
||
107 | public function getParentId() { |
||
110 | |||
111 | /** |
||
112 | * sets the parent ID and returns itself |
||
113 | * |
||
114 | * @param string $parentId |
||
115 | * @return IComment |
||
116 | * @since 9.0.0 |
||
117 | */ |
||
118 | public function setParentId($parentId) { |
||
125 | |||
126 | /** |
||
127 | * returns the topmost parent ID of the comment |
||
128 | * |
||
129 | * @return string |
||
130 | * @since 9.0.0 |
||
131 | */ |
||
132 | public function getTopmostParentId() { |
||
135 | |||
136 | |||
137 | /** |
||
138 | * sets the topmost parent ID and returns itself |
||
139 | * |
||
140 | * @param string $id |
||
141 | * @return IComment |
||
142 | * @since 9.0.0 |
||
143 | */ |
||
144 | View Code Duplication | public function setTopmostParentId($id) { |
|
151 | |||
152 | /** |
||
153 | * returns the number of children |
||
154 | * |
||
155 | * @return int |
||
156 | * @since 9.0.0 |
||
157 | */ |
||
158 | public function getChildrenCount() { |
||
161 | |||
162 | /** |
||
163 | * sets the number of children |
||
164 | * |
||
165 | * @param int $count |
||
166 | * @return IComment |
||
167 | * @since 9.0.0 |
||
168 | */ |
||
169 | public function setChildrenCount($count) { |
||
176 | |||
177 | /** |
||
178 | * returns the message of the comment |
||
179 | * |
||
180 | * @return string |
||
181 | * @since 9.0.0 |
||
182 | */ |
||
183 | public function getMessage() { |
||
186 | |||
187 | /** |
||
188 | * sets the message of the comment and returns itself |
||
189 | * |
||
190 | * @param string $message |
||
191 | * @return IComment |
||
192 | * @throws MessageTooLongException |
||
193 | * @since 9.0.0 |
||
194 | */ |
||
195 | public function setMessage($message) { |
||
206 | |||
207 | /** |
||
208 | * returns an array containing mentions that are included in the comment |
||
209 | * |
||
210 | * @return array each mention provides a 'type' and an 'id', see example below |
||
211 | * @since 11.0.0 |
||
212 | * |
||
213 | * The return array looks like: |
||
214 | * [ |
||
215 | * [ |
||
216 | * 'type' => 'user', |
||
217 | * 'id' => 'citizen4' |
||
218 | * ], |
||
219 | * [ |
||
220 | * 'type' => 'group', |
||
221 | * 'id' => 'media' |
||
222 | * ], |
||
223 | * … |
||
224 | * ] |
||
225 | * |
||
226 | */ |
||
227 | public function getMentions() { |
||
239 | |||
240 | /** |
||
241 | * returns the verb of the comment |
||
242 | * |
||
243 | * @return string |
||
244 | * @since 9.0.0 |
||
245 | */ |
||
246 | public function getVerb() { |
||
249 | |||
250 | /** |
||
251 | * sets the verb of the comment, e.g. 'comment' or 'like' |
||
252 | * |
||
253 | * @param string $verb |
||
254 | * @return IComment |
||
255 | * @since 9.0.0 |
||
256 | */ |
||
257 | View Code Duplication | public function setVerb($verb) { |
|
264 | |||
265 | /** |
||
266 | * returns the actor type |
||
267 | * |
||
268 | * @return string |
||
269 | * @since 9.0.0 |
||
270 | */ |
||
271 | public function getActorType() { |
||
274 | |||
275 | /** |
||
276 | * returns the actor ID |
||
277 | * |
||
278 | * @return string |
||
279 | * @since 9.0.0 |
||
280 | */ |
||
281 | public function getActorId() { |
||
284 | |||
285 | /** |
||
286 | * sets (overwrites) the actor type and id |
||
287 | * |
||
288 | * @param string $actorType e.g. 'users' |
||
289 | * @param string $actorId e.g. 'zombie234' |
||
290 | * @return IComment |
||
291 | * @since 9.0.0 |
||
292 | */ |
||
293 | public function setActor($actorType, $actorId) { |
||
304 | |||
305 | /** |
||
306 | * returns the creation date of the comment. |
||
307 | * |
||
308 | * If not explicitly set, it shall default to the time of initialization. |
||
309 | * |
||
310 | * @return \DateTime |
||
311 | * @since 9.0.0 |
||
312 | */ |
||
313 | public function getCreationDateTime() { |
||
316 | |||
317 | /** |
||
318 | * sets the creation date of the comment and returns itself |
||
319 | * |
||
320 | * @param \DateTime $timestamp |
||
321 | * @return IComment |
||
322 | * @since 9.0.0 |
||
323 | */ |
||
324 | public function setCreationDateTime(\DateTime $timestamp) { |
||
328 | |||
329 | /** |
||
330 | * returns the DateTime of the most recent child, if set, otherwise null |
||
331 | * |
||
332 | * @return \DateTime|null |
||
333 | * @since 9.0.0 |
||
334 | */ |
||
335 | public function getLatestChildDateTime() { |
||
338 | |||
339 | /** |
||
340 | * sets the date of the most recent child |
||
341 | * |
||
342 | * @param \DateTime $dateTime |
||
343 | * @return IComment |
||
344 | * @since 9.0.0 |
||
345 | */ |
||
346 | public function setLatestChildDateTime(\DateTime $dateTime = null) { |
||
350 | |||
351 | /** |
||
352 | * returns the object type the comment is attached to |
||
353 | * |
||
354 | * @return string |
||
355 | * @since 9.0.0 |
||
356 | */ |
||
357 | public function getObjectType() { |
||
360 | |||
361 | /** |
||
362 | * returns the object id the comment is attached to |
||
363 | * |
||
364 | * @return string |
||
365 | * @since 9.0.0 |
||
366 | */ |
||
367 | public function getObjectId() { |
||
370 | |||
371 | /** |
||
372 | * sets (overwrites) the object of the comment |
||
373 | * |
||
374 | * @param string $objectType e.g. 'files' |
||
375 | * @param string $objectId e.g. '16435' |
||
376 | * @return IComment |
||
377 | * @since 9.0.0 |
||
378 | */ |
||
379 | public function setObject($objectType, $objectId) { |
||
390 | |||
391 | /** |
||
392 | * sets the comment data based on an array with keys as taken from the |
||
393 | * database. |
||
394 | * |
||
395 | * @param array $data |
||
396 | * @return IComment |
||
397 | */ |
||
398 | protected function fromArray($data) { |
||
418 | } |
||
419 |