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 BloggerApi 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 BloggerApi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class BloggerApi extends XoopsXmlRpcApi |
||
22 | { |
||
23 | /** |
||
24 | * @param $params |
||
25 | * @param $response |
||
26 | * @param $module |
||
27 | */ |
||
28 | 5 | View Code Duplication | public function __construct(&$params, &$response, &$module) |
29 | { |
||
30 | 5 | parent::__construct($params, $response, $module); |
|
31 | 5 | $this->_setXoopsTagMap('storyid', 'postid'); |
|
32 | 5 | $this->_setXoopsTagMap('published', 'dateCreated'); |
|
33 | 5 | $this->_setXoopsTagMap('uid', 'userid'); |
|
34 | 5 | } |
|
35 | |||
36 | public function newPost() |
||
37 | { |
||
38 | if (!$this->_checkUser($this->params[2], $this->params[3])) { |
||
39 | $this->response->add(new XoopsXmlRpcFault(104)); |
||
40 | } else { |
||
41 | if (!$fields =& $this->_getPostFields(null, $this->params[1])) { |
||
42 | $this->response->add(new XoopsXmlRpcFault(106)); |
||
43 | View Code Duplication | } else { |
|
44 | $missing = array(); |
||
45 | $post = array(); |
||
46 | foreach ($fields as $tag => $detail) { |
||
47 | $maptag = $this->_getXoopsTagMap($tag); |
||
48 | $data = $this->_getTagCdata($this->params[4], $maptag, true); |
||
49 | if (trim($data) == ''){ |
||
50 | if ($detail['required']) { |
||
51 | $missing[] = $maptag; |
||
52 | } |
||
53 | } else { |
||
54 | $post[$tag] = $data; |
||
55 | } |
||
56 | } |
||
57 | if (count($missing) > 0) { |
||
58 | $msg = ''; |
||
59 | foreach ($missing as $m) { |
||
60 | $msg .= '<' . $m . '> '; |
||
61 | } |
||
62 | $this->response->add(new XoopsXmlRpcFault(109, $msg)); |
||
63 | } else { |
||
64 | $newparams = array(); |
||
65 | // Xoops Api ignores App key |
||
66 | $newparams[0] = $this->params[1]; |
||
67 | $newparams[1] = $this->params[2]; |
||
68 | $newparams[2] = $this->params[3]; |
||
69 | foreach ($post as $key => $value) { |
||
70 | $newparams[3][$key] = $value; |
||
71 | unset($value); |
||
72 | } |
||
73 | $newparams[3]['xoops_text'] = $this->params[4]; |
||
74 | $newparams[4] = $this->params[5]; |
||
75 | $xoopsapi = $this->_getXoopsApi($newparams); |
||
76 | $xoopsapi->_setUser($this->user, $this->isadmin); |
||
77 | $xoopsapi->newPost(); |
||
78 | } |
||
79 | } |
||
80 | } |
||
81 | } |
||
82 | |||
83 | public function editPost() |
||
84 | { |
||
85 | if (!$this->_checkUser($this->params[2], $this->params[3])) { |
||
86 | $this->response->add(new XoopsXmlRpcFault(104)); |
||
87 | View Code Duplication | } else { |
|
88 | if (!$fields = $this->_getPostFields($this->params[1])) { |
||
89 | } else { |
||
90 | $missing = array(); |
||
91 | $post = array(); |
||
92 | foreach ($fields as $tag => $detail) { |
||
93 | $data = $this->_getTagCdata($this->params[4], $tag, true); |
||
94 | if (trim($data) == ''){ |
||
95 | if ($detail['required']) { |
||
96 | $missing[] = $tag; |
||
97 | } |
||
98 | } else { |
||
99 | $post[$tag] = $data; |
||
100 | } |
||
101 | } |
||
102 | if (count($missing) > 0) { |
||
103 | $msg = ''; |
||
104 | foreach ($missing as $m) { |
||
105 | $msg .= '<' . $m . '> '; |
||
106 | } |
||
107 | $this->response->add(new XoopsXmlRpcFault(109, $msg)); |
||
108 | } else { |
||
109 | $newparams = array(); |
||
110 | // XOOPS API ignores App key (index 0 of params) |
||
111 | $newparams[0] = $this->params[1]; |
||
112 | $newparams[1] = $this->params[2]; |
||
113 | $newparams[2] = $this->params[3]; |
||
114 | foreach ($post as $key => $value) { |
||
115 | $newparams[3][$key] = $value; |
||
116 | unset($value); |
||
117 | } |
||
118 | $newparams[3]['xoops_text'] = $this->params[4]; |
||
119 | $newparams[4] = $this->params[5]; |
||
120 | $xoopsapi = $this->_getXoopsApi($newparams); |
||
121 | $xoopsapi->_setUser($this->user, $this->isadmin); |
||
122 | $xoopsapi->editPost(); |
||
123 | } |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | public function deletePost() |
||
140 | |||
141 | public function getPost() |
||
178 | |||
179 | public function getRecentPosts() |
||
226 | |||
227 | 1 | public function getUsersBlogs() |
|
242 | |||
243 | 1 | public function getUserInfo() |
|
258 | |||
259 | 1 | public function getTemplate() |
|
277 | |||
278 | 1 | public function setTemplate() |
|
279 | { |
||
280 | 1 | if (!$this->_checkUser($this->params[2], $this->params[3])) { |
|
286 | } |
||
287 |