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 MyXoopsStory 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 MyXoopsStory, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class MyXoopsStory |
||
29 | { |
||
30 | public $table; |
||
31 | public $storyid; |
||
32 | public $topicid; |
||
33 | public $uid; |
||
34 | public $title; |
||
35 | public $hometext; |
||
36 | public $bodytext = ''; |
||
37 | public $counter; |
||
38 | public $created; |
||
39 | public $published; |
||
40 | public $expired; |
||
41 | public $hostname; |
||
42 | public $nohtml = 0; |
||
43 | public $nosmiley = 0; |
||
44 | public $ihome = 0; |
||
45 | public $notifypub = 0; |
||
46 | public $type; |
||
47 | public $approved; |
||
48 | public $topicdisplay; |
||
49 | public $topicalign; |
||
50 | public $db; |
||
51 | public $topicstable; |
||
52 | public $comments; |
||
53 | |||
54 | /** |
||
55 | * @param $storyid |
||
56 | */ |
||
57 | public function Story($storyid = -1) |
||
68 | |||
69 | /** |
||
70 | * @param $value |
||
71 | */ |
||
72 | public function setStoryId($value) |
||
76 | |||
77 | /** |
||
78 | * @param $value |
||
79 | */ |
||
80 | public function setTopicId($value) |
||
84 | |||
85 | /** |
||
86 | * @param $value |
||
87 | */ |
||
88 | public function setUid($value) |
||
92 | |||
93 | /** |
||
94 | * @param $value |
||
95 | */ |
||
96 | public function setTitle($value) |
||
100 | |||
101 | /** |
||
102 | * @param $value |
||
103 | */ |
||
104 | public function setHometext($value) |
||
108 | |||
109 | /** |
||
110 | * @param $value |
||
111 | */ |
||
112 | public function setBodytext($value) |
||
116 | |||
117 | /** |
||
118 | * @param $value |
||
119 | */ |
||
120 | public function setPublished($value) |
||
124 | |||
125 | /** |
||
126 | * @param $value |
||
127 | */ |
||
128 | public function setExpired($value) |
||
132 | |||
133 | /** |
||
134 | * @param $value |
||
135 | */ |
||
136 | public function setHostname($value) |
||
140 | |||
141 | /** |
||
142 | * @param int $value |
||
143 | */ |
||
144 | public function setNohtml($value = 0) |
||
148 | |||
149 | /** |
||
150 | * @param int $value |
||
151 | */ |
||
152 | public function setNosmiley($value = 0) |
||
156 | |||
157 | /** |
||
158 | * @param $value |
||
159 | */ |
||
160 | public function setIhome($value) |
||
164 | |||
165 | /** |
||
166 | * @param $value |
||
167 | */ |
||
168 | public function setNotifyPub($value) |
||
172 | |||
173 | /** |
||
174 | * @param $value |
||
175 | */ |
||
176 | public function setType($value) |
||
180 | |||
181 | /** |
||
182 | * @param $value |
||
183 | */ |
||
184 | public function setApproved($value) |
||
188 | |||
189 | /** |
||
190 | * @param $value |
||
191 | */ |
||
192 | public function setTopicdisplay($value) |
||
196 | |||
197 | /** |
||
198 | * @param $value |
||
199 | */ |
||
200 | public function setTopicalign($value) |
||
204 | |||
205 | /** |
||
206 | * @param $value |
||
207 | */ |
||
208 | public function setComments($value) |
||
212 | |||
213 | /** |
||
214 | * @param bool $approved |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function store($approved = false) |
||
275 | |||
276 | /** |
||
277 | * @param $storyid |
||
278 | */ |
||
279 | View Code Duplication | public function getStory($storyid) |
|
286 | |||
287 | /** |
||
288 | * @param $array |
||
289 | */ |
||
290 | public function makeStory($array) |
||
296 | |||
297 | /** |
||
298 | * @return bool |
||
299 | */ |
||
300 | View Code Duplication | public function delete() |
|
309 | |||
310 | /** |
||
311 | * @return bool |
||
312 | */ |
||
313 | View Code Duplication | public function updateCounter() |
|
322 | |||
323 | /** |
||
324 | * @param $total |
||
325 | * |
||
326 | * @return bool |
||
327 | */ |
||
328 | View Code Duplication | public function updateComments($total) |
|
337 | |||
338 | public function topicid() |
||
342 | |||
343 | /** |
||
344 | * @return MyXoopsTopic |
||
345 | */ |
||
346 | public function topic() |
||
350 | |||
351 | public function uid() |
||
355 | |||
356 | /** |
||
357 | * @return string |
||
358 | */ |
||
359 | public function uname() |
||
363 | |||
364 | /** |
||
365 | * @param string $format |
||
366 | * |
||
367 | * @return mixed |
||
368 | */ |
||
369 | View Code Duplication | public function title($format = 'Show') |
|
389 | |||
390 | /** |
||
391 | * @param string $format |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | View Code Duplication | public function hometext($format = 'Show') |
|
424 | |||
425 | /** |
||
426 | * @param string $format |
||
427 | * |
||
428 | * @return string |
||
429 | */ |
||
430 | View Code Duplication | public function bodytext($format = 'Show') |
|
459 | |||
460 | public function counter() |
||
464 | |||
465 | public function created() |
||
469 | |||
470 | public function published() |
||
474 | |||
475 | public function expired() |
||
479 | |||
480 | public function hostname() |
||
484 | |||
485 | public function storyid() |
||
489 | |||
490 | /** |
||
491 | * @return int |
||
492 | */ |
||
493 | public function nohtml() |
||
497 | |||
498 | /** |
||
499 | * @return int |
||
500 | */ |
||
501 | public function nosmiley() |
||
505 | |||
506 | /** |
||
507 | * @return int |
||
508 | */ |
||
509 | public function notifypub() |
||
513 | |||
514 | public function type() |
||
518 | |||
519 | /** |
||
520 | * @return int |
||
521 | */ |
||
522 | public function ihome() |
||
526 | |||
527 | public function topicdisplay() |
||
531 | |||
532 | /** |
||
533 | * @param bool $astext |
||
534 | * |
||
535 | * @return string |
||
536 | */ |
||
537 | public function topicalign($astext = true) |
||
551 | |||
552 | public function comments() |
||
556 | } |
||
557 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.