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 sFiles 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 sFiles, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
34 | class sFiles |
||
35 | { |
||
36 | public $db; |
||
37 | public $table; |
||
38 | public $fileid; |
||
39 | public $filerealname; |
||
40 | public $storyid; |
||
41 | public $date; |
||
42 | public $mimetype; |
||
43 | public $downloadname; |
||
44 | public $counter; |
||
45 | |||
46 | /** |
||
47 | * @param $fileid |
||
48 | */ |
||
49 | public function __construct($fileid = -1) |
||
50 | { |
||
51 | $this->db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
52 | $this->table = $this->db->prefix('news_stories_files'); |
||
53 | $this->storyid = 0; |
||
54 | $this->filerealname = ''; |
||
55 | $this->date = 0; |
||
56 | $this->mimetype = ''; |
||
57 | $this->downloadname = 'downloadfile'; |
||
58 | $this->counter = 0; |
||
59 | if (is_array($fileid)) { |
||
60 | $this->makeFile($fileid); |
||
61 | } elseif ($fileid != -1) { |
||
62 | $this->getFile((int)$fileid); |
||
63 | } |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * @param $folder |
||
68 | * @param $filename |
||
69 | * @param bool $trimname |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function createUploadName($folder, $filename, $trimname = false) |
||
74 | { |
||
75 | $workingfolder = $folder; |
||
76 | if (xoops_substr($workingfolder, strlen($workingfolder) - 1, 1) !== '/') { |
||
77 | $workingfolder .= '/'; |
||
78 | } |
||
79 | $ext = basename($filename); |
||
80 | $ext = explode('.', $ext); |
||
81 | $ext = '.' . $ext[count($ext) - 1]; |
||
82 | $true = true; |
||
83 | while ($true) { |
||
84 | $ipbits = explode('.', $_SERVER['REMOTE_ADDR']); |
||
85 | list($usec, $sec) = explode(' ', microtime()); |
||
86 | |||
87 | $usec = (integer)($usec * 65536); |
||
88 | $sec = ((integer)$sec) & 0xFFFF; |
||
89 | |||
90 | if ($trimname) { |
||
91 | $uid = sprintf('%06x%04x%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec); |
||
92 | } else { |
||
93 | $uid = sprintf('%08x-%04x-%04x', ($ipbits[0] << 24) | ($ipbits[1] << 16) | ($ipbits[2] << 8) | $ipbits[3], $sec, $usec); |
||
94 | } |
||
95 | if (!file_exists($workingfolder . $uid . $ext)) { |
||
96 | $true = false; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | return $uid . $ext; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * @param string $filename |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function giveMimetype($filename = '') |
||
120 | |||
121 | /** |
||
122 | * @param $storyid |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | public function getAllbyStory($storyid) |
||
127 | { |
||
128 | $ret = array(); |
||
129 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE storyid=' . (int)$storyid; |
||
130 | $result = $this->db->query($sql); |
||
131 | while ($myrow = $this->db->fetchArray($result)) { |
||
132 | $ret[] = new sFiles($myrow); |
||
133 | } |
||
134 | |||
135 | return $ret; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param $id |
||
140 | */ |
||
141 | public function getFile($id) |
||
142 | { |
||
143 | $sql = 'SELECT * FROM ' . $this->table . ' WHERE fileid=' . (int)$id; |
||
144 | $array = $this->db->fetchArray($this->db->query($sql)); |
||
145 | $this->makeFile($array); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @param $array |
||
150 | */ |
||
151 | public function makeFile($array) |
||
157 | |||
158 | /** |
||
159 | * @return bool |
||
160 | */ |
||
161 | public function store() |
||
184 | |||
185 | /** |
||
186 | * @param string $workdir |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function delete($workdir = XOOPS_UPLOAD_PATH) |
||
202 | |||
203 | /** |
||
204 | * @return bool |
||
205 | */ |
||
206 | public function updateCounter() |
||
215 | |||
216 | // **************************************************************************************************************** |
||
217 | // All the Sets |
||
218 | // **************************************************************************************************************** |
||
219 | /** |
||
220 | * @param $filename |
||
221 | */ |
||
222 | public function setFileRealName($filename) |
||
226 | |||
227 | /** |
||
228 | * @param $id |
||
229 | */ |
||
230 | public function setStoryid($id) |
||
234 | |||
235 | /** |
||
236 | * @param $value |
||
237 | */ |
||
238 | public function setMimetype($value) |
||
242 | |||
243 | /** |
||
244 | * @param $value |
||
245 | */ |
||
246 | public function setDownloadname($value) |
||
250 | |||
251 | // **************************************************************************************************************** |
||
252 | // All the Gets |
||
253 | // **************************************************************************************************************** |
||
254 | /** |
||
255 | * @return int |
||
256 | */ |
||
257 | public function getFileid() |
||
261 | |||
262 | /** |
||
263 | * @return int |
||
264 | */ |
||
265 | public function getStoryid() |
||
269 | |||
270 | /** |
||
271 | * @return int |
||
272 | */ |
||
273 | public function getCounter() |
||
277 | |||
278 | /** |
||
279 | * @return int |
||
280 | */ |
||
281 | public function getDate() |
||
285 | |||
286 | /** |
||
287 | * @param string $format |
||
288 | * |
||
289 | * @return mixed |
||
290 | */ |
||
291 | View Code Duplication | public function getFileRealName($format = 'S') |
|
315 | |||
316 | /** |
||
317 | * @param string $format |
||
318 | * |
||
319 | * @return mixed |
||
320 | */ |
||
321 | View Code Duplication | public function getMimetype($format = 'S') |
|
345 | |||
346 | /** |
||
347 | * @param string $format |
||
348 | * |
||
349 | * @return mixed |
||
350 | */ |
||
351 | View Code Duplication | public function getDownloadname($format = 'S') |
|
375 | |||
376 | // Deprecated |
||
377 | /** |
||
378 | * @param $storyid |
||
379 | * |
||
380 | * @return mixed |
||
381 | */ |
||
382 | View Code Duplication | public function getCountbyStory($storyid) |
|
390 | |||
391 | /** |
||
392 | * @param $stories |
||
393 | * |
||
394 | * @return array |
||
395 | */ |
||
396 | public function getCountbyStories($stories) |
||
410 | } |
||
411 |
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.