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 XoopsApi 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 XoopsApi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class XoopsApi extends XoopsXmlRpcApi |
||
24 | { |
||
25 | /** |
||
26 | * @param array $params |
||
27 | * @param XoopsXmlRpcResponse $response |
||
28 | * @param XoopsModule $module |
||
29 | */ |
||
30 | 4 | public function __construct(array &$params, XoopsXmlRpcResponse $response, XoopsModule $module) |
|
34 | |||
35 | /** |
||
36 | * @return void |
||
37 | */ |
||
38 | public function newPost() |
||
39 | { |
||
40 | if (!$this->_checkUser($this->params[1], $this->params[2])) { |
||
41 | $this->response->add(new XoopsXmlRpcFault(104)); |
||
42 | } else { |
||
43 | if (!$fields = $this->_getPostFields(null, $this->params[0])) { |
||
44 | $this->response->add(new XoopsXmlRpcFault(106)); |
||
45 | } else { |
||
46 | $missing = array(); |
||
47 | foreach ($fields as $tag => $detail) { |
||
48 | if (!isset($this->params[3][$tag])) { |
||
49 | $data = $this->_getTagCdata($this->params[3]['xoops_text'], $tag, true); |
||
50 | if (trim($data) == '') { |
||
51 | if ($detail['required']) { |
||
52 | $missing[] = $tag; |
||
53 | } |
||
54 | } else { |
||
55 | $post[$tag] = $data; |
||
56 | } |
||
57 | } else { |
||
58 | $post[$tag] = $this->params[3][$tag]; |
||
59 | } |
||
60 | } |
||
61 | if (count($missing) > 0) { |
||
62 | $msg = ''; |
||
63 | foreach ($missing as $m) { |
||
64 | $msg .= '<' . $m . '> '; |
||
65 | } |
||
66 | $this->response->add(new XoopsXmlRpcFault(109, $msg)); |
||
67 | } else { |
||
68 | // will be removed... don't worry if this looks bad |
||
69 | View Code Duplication | if (!XoopsLoad::loadFile(\XoopsBaseConfig::get('root-path').'/modules/news/class/class.newsstory.php', true)) { |
|
70 | $this->response->add(new XoopsXmlRpcFault(103)); |
||
71 | return; |
||
72 | } |
||
73 | $story = new NewsStory(); |
||
74 | $error = false; |
||
75 | if ((int)$this->params[4] > 0) { |
||
76 | if (!$this->_checkAdmin()) { |
||
77 | // non admin users cannot publish |
||
78 | $error = true; |
||
79 | $this->response->add(new XoopsXmlRpcFault(111)); |
||
80 | } else { |
||
81 | $story->setType('admin'); |
||
82 | $story->setApproved(true); |
||
83 | $story->setPublished(time()); |
||
84 | } |
||
85 | } else { |
||
86 | if (!$this->_checkAdmin()) { |
||
87 | $story->setType('user'); |
||
88 | } else { |
||
89 | $story->setType('admin'); |
||
90 | } |
||
91 | } |
||
92 | if (!$error) { |
||
93 | if (isset($post['categories']) && !empty($post['categories'][0])) { |
||
94 | $story->setTopicId((int)($post['categories'][0]['categoryId'])); |
||
95 | } else { |
||
96 | $story->setTopicId(1); |
||
97 | } |
||
98 | $story->setTitle(addslashes(trim($post['title']))); |
||
99 | if (isset($post['moretext'])) { |
||
100 | $story->setBodytext(addslashes(trim($post['moretext']))); |
||
101 | } |
||
102 | View Code Duplication | if (!isset($post['hometext'])) { |
|
103 | $story->setHometext(addslashes(trim($this->params[3]['xoops_text']))); |
||
104 | } else { |
||
105 | $story->setHometext(addslashes(trim($post['hometext']))); |
||
106 | } |
||
107 | $story->setUid($this->user->getVar('uid')); |
||
108 | $story->setHostname($_SERVER['REMOTE_ADDR']); |
||
109 | if (!$this->_checkAdmin()) { |
||
110 | $story->setNohtml(1); |
||
111 | } else { |
||
112 | $story->setNohtml(0); |
||
113 | } |
||
114 | $story->setNosmiley(0); |
||
115 | $story->setNotifyPub(1); |
||
116 | $story->setTopicalign('R'); |
||
117 | $ret = $story->store(); |
||
118 | if (!$ret) { |
||
119 | $this->response->add(new XoopsXmlRpcFault(106)); |
||
120 | } else { |
||
121 | $this->response->add(new XoopsXmlRpcString($ret)); |
||
122 | } |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | |||
129 | public function editPost() |
||
194 | |||
195 | public function deletePost() |
||
196 | { |
||
197 | if (!$this->_checkUser($this->params[1], $this->params[2])) { |
||
198 | $this->response->add(new XoopsXmlRpcFault(104)); |
||
199 | } else { |
||
200 | if (!$this->_checkAdmin()) { |
||
201 | $this->response->add(new XoopsXmlRpcFault(111)); |
||
202 | } else { |
||
203 | // will be removed... don't worry if this looks bad |
||
204 | View Code Duplication | if (!XoopsLoad::loadFile(\XoopsBaseConfig::get('root-path').'/modules/news/class/class.newsstory.php', true)) { |
|
205 | $this->response->add(new XoopsXmlRpcFault(103)); |
||
206 | return; |
||
207 | } |
||
208 | $story = new NewsStory($this->params[0]); |
||
209 | View Code Duplication | if (!$story->delete()) { |
|
210 | $this->response->add(new XoopsXmlRpcFault(106)); |
||
211 | } else { |
||
212 | $this->response->add(new XoopsXmlRpcBoolean(true)); |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 | } |
||
217 | |||
218 | // currently returns the same struct as in metaWeblogApi |
||
219 | /** |
||
220 | * @param bool $respond |
||
221 | * |
||
222 | * @return array |
||
223 | */ |
||
224 | 1 | public function &getPost($respond = true) |
|
225 | { |
||
226 | 1 | if (!$this->_checkUser($this->params[1], $this->params[2])) { |
|
227 | 1 | $this->response->add(new XoopsXmlRpcFault(104)); |
|
228 | } else { |
||
229 | // will be removed... don't worry if this looks bad |
||
230 | View Code Duplication | if (!XoopsLoad::loadFile(\XoopsBaseConfig::get('root-path').'/modules/news/class/class.newsstory.php', true)) { |
|
231 | $this->response->add(new XoopsXmlRpcFault(103)); |
||
232 | return; |
||
233 | } |
||
234 | $story = new NewsStory($this->params[0]); |
||
235 | $ret = array( |
||
236 | 'uid' => $story->uid(), |
||
237 | 'published' => $story->published(), |
||
238 | 'storyid' => $story->storyId(), |
||
239 | 'title' => $story->title('Edit'), |
||
240 | 'hometext' => $story->hometext('Edit'), |
||
241 | 'moretext' => $story->bodytext('Edit') |
||
242 | ); |
||
243 | if (!$respond) { |
||
244 | return $ret; |
||
245 | } else { |
||
246 | if (!$ret) { |
||
247 | $this->response->add(new XoopsXmlRpcFault(106)); |
||
248 | } else { |
||
249 | $struct = new XoopsXmlRpcStruct(); |
||
250 | $content = ''; |
||
251 | View Code Duplication | foreach ($ret as $key => $value) { |
|
252 | switch($key) { |
||
253 | case 'uid': |
||
254 | $struct->add('userid', new XoopsXmlRpcString($value)); |
||
255 | break; |
||
256 | case 'published': |
||
257 | $struct->add('dateCreated', new XoopsXmlRpcDatetime($value)); |
||
258 | break; |
||
259 | case 'storyid': |
||
260 | $struct->add('postid', new XoopsXmlRpcString($value)); |
||
261 | $struct->add('link', new XoopsXmlRpcString(\XoopsBaseConfig::get('url') . '/modules/news/article.php?item_id=' . $value)); |
||
262 | $struct->add('permaLink', new XoopsXmlRpcString(\XoopsBaseConfig::get('url') . '/modules/news/article.php?item_id=' . $value)); |
||
263 | break; |
||
264 | case 'title': |
||
265 | $struct->add('title', new XoopsXmlRpcString($value)); |
||
266 | break; |
||
267 | default : |
||
268 | $content .= '<' . $key . '>' . trim($value) . '</' . $key . '>'; |
||
269 | break; |
||
270 | } |
||
271 | } |
||
272 | $struct->add('description', new XoopsXmlRpcString($content)); |
||
273 | $this->response->add($struct); |
||
274 | } |
||
275 | } |
||
276 | } |
||
277 | 1 | return null; |
|
278 | } |
||
279 | |||
280 | /** |
||
281 | * @param bool $respond |
||
282 | * |
||
283 | * @return array |
||
284 | */ |
||
285 | 1 | public function &getRecentPosts($respond = true) |
|
286 | { |
||
287 | 1 | if (!$this->_checkUser($this->params[1], $this->params[2])) { |
|
288 | 1 | $this->response->add(new XoopsXmlRpcFault(104)); |
|
289 | } else { |
||
290 | View Code Duplication | if (!XoopsLoad::loadFile(\XoopsBaseConfig::get('root-path').'/modules/news/class/class.newsstory.php', true)) { |
|
291 | $this->response->add(new XoopsXmlRpcFault(103)); |
||
292 | return; |
||
293 | } |
||
294 | if (isset($this->params[4]) && (int)($this->params[4]) > 0) { |
||
295 | $stories = NewsStory::getAllPublished((int)($this->params[3]), 0, $this->params[4]); |
||
296 | } else { |
||
297 | $stories = NewsStory::getAllPublished((int)($this->params[3])); |
||
298 | } |
||
299 | $scount = count($stories); |
||
300 | $ret = array(); |
||
301 | for ($i = 0; $i < $scount; ++$i) { |
||
302 | $ret[] = array( |
||
303 | 'uid' => $stories[$i]->uid(), |
||
304 | 'published' => $stories[$i]->published(), |
||
305 | 'storyid' => $stories[$i]->storyId(), |
||
306 | 'title' => $stories[$i]->title('Edit'), |
||
307 | 'hometext' => $stories[$i]->hometext('Edit'), |
||
308 | 'moretext' => $stories[$i]->bodytext('Edit') |
||
309 | ); |
||
310 | } |
||
311 | if (!$respond) { |
||
312 | return $ret; |
||
313 | } else { |
||
314 | if (count($ret) == 0) { |
||
315 | $this->response->add(new XoopsXmlRpcFault(106, 'Found 0 Entries')); |
||
316 | } else { |
||
317 | $arr = new XoopsXmlRpcArray(); |
||
318 | $count = count($ret); |
||
319 | for ($i = 0; $i < $count; ++$i) { |
||
320 | $struct = new XoopsXmlRpcStruct(); |
||
321 | $content = ''; |
||
322 | View Code Duplication | foreach($ret[$i] as $key => $value) { |
|
323 | switch($key) { |
||
324 | case 'uid': |
||
325 | $struct->add('userid', new XoopsXmlRpcString($value)); |
||
326 | break; |
||
327 | case 'published': |
||
328 | $struct->add('dateCreated', new XoopsXmlRpcDatetime($value)); |
||
329 | break; |
||
330 | case 'storyid': |
||
331 | $struct->add('postid', new XoopsXmlRpcString($value)); |
||
332 | $struct->add('link', new XoopsXmlRpcString(\XoopsBaseConfig::get('url') . '/modules/news/article.php?item_id=' . $value)); |
||
333 | $struct->add('permaLink', new XoopsXmlRpcString(\XoopsBaseConfig::get('url') . '/modules/news/article.php?item_id=' . $value)); |
||
334 | break; |
||
335 | case 'title': |
||
336 | $struct->add('title', new XoopsXmlRpcString($value)); |
||
337 | break; |
||
338 | default : |
||
339 | $content .= '<' . $key . '>' . trim($value) . '</' . $key . '>'; |
||
340 | break; |
||
341 | } |
||
342 | } |
||
343 | $struct->add('description', new XoopsXmlRpcString($content)); |
||
344 | $arr->add($struct); |
||
345 | unset($struct); |
||
346 | } |
||
347 | $this->response->add($arr); |
||
348 | } |
||
349 | } |
||
350 | } |
||
351 | 1 | return null; |
|
352 | } |
||
353 | |||
354 | function getCategories($respond=true) |
||
388 | } |
||
389 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: