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 | 9 | View Code Duplication | public function __construct(&$params, &$response, &$module) |
| 29 | { |
||
| 30 | 9 | parent::__construct($params, $response, $module); |
|
| 31 | 9 | $this->_setXoopsTagMap('storyid', 'postid'); |
|
| 32 | 9 | $this->_setXoopsTagMap('published', 'dateCreated'); |
|
| 33 | 9 | $this->_setXoopsTagMap('uid', 'userid'); |
|
| 34 | 9 | } |
|
| 35 | |||
| 36 | 1 | public function newPost() |
|
| 37 | { |
||
| 38 | 1 | if (!$this->_checkUser($this->params[2], $this->params[3])) { |
|
| 39 | $this->response->add(new XoopsXmlRpcFault(104)); |
||
| 40 | } else { |
||
| 41 | 1 | if (!$fields =& $this->_getPostFields(null, $this->params[1])) { |
|
| 42 | $this->response->add(new XoopsXmlRpcFault(106)); |
||
| 43 | View Code Duplication | } else { |
|
| 44 | 1 | $missing = array(); |
|
| 45 | 1 | $post = array(); |
|
| 46 | 1 | foreach ($fields as $tag => $detail) { |
|
| 47 | 1 | $maptag = $this->_getXoopsTagMap($tag); |
|
| 48 | 1 | $data = $this->_getTagCdata($this->params[4], $maptag, true); |
|
| 49 | 1 | if (trim($data) == ''){ |
|
| 50 | if ($detail['required']) { |
||
| 51 | $missing[] = $maptag; |
||
| 52 | } |
||
| 53 | } else { |
||
| 54 | 1 | $post[$tag] = $data; |
|
| 55 | } |
||
| 56 | } |
||
| 57 | 1 | 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 | 1 | $newparams = array(); |
|
| 65 | // Xoops Api ignores App key |
||
| 66 | 1 | $newparams[0] = $this->params[1]; |
|
| 67 | 1 | $newparams[1] = $this->params[2]; |
|
| 68 | 1 | $newparams[2] = $this->params[3]; |
|
| 69 | 1 | foreach ($post as $key => $value) { |
|
| 70 | 1 | $newparams[3][$key] = $value; |
|
| 71 | 1 | unset($value); |
|
| 72 | } |
||
| 73 | 1 | $newparams[3]['xoops_text'] = $this->params[4]; |
|
| 74 | 1 | $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 | 1 | public function editPost() |
|
| 84 | { |
||
| 85 | 1 | if (!$this->_checkUser($this->params[2], $this->params[3])) { |
|
| 86 | $this->response->add(new XoopsXmlRpcFault(104)); |
||
| 87 | View Code Duplication | } else { |
|
| 88 | 1 | if (!$fields = $this->_getPostFields($this->params[1])) { |
|
| 89 | } else { |
||
| 90 | 1 | $missing = array(); |
|
| 91 | 1 | $post = array(); |
|
| 92 | 1 | foreach ($fields as $tag => $detail) { |
|
| 93 | 1 | $data = $this->_getTagCdata($this->params[4], $tag, true); |
|
| 94 | 1 | if (trim($data) == ''){ |
|
| 95 | if ($detail['required']) { |
||
| 96 | $missing[] = $tag; |
||
| 97 | } |
||
| 98 | } else { |
||
| 99 | 1 | $post[$tag] = $data; |
|
| 100 | } |
||
| 101 | } |
||
| 102 | 1 | 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 | 1 | $newparams = array(); |
|
| 110 | // XOOPS API ignores App key (index 0 of params) |
||
| 111 | 1 | $newparams[0] = $this->params[1]; |
|
| 112 | 1 | $newparams[1] = $this->params[2]; |
|
| 113 | 1 | $newparams[2] = $this->params[3]; |
|
| 114 | 1 | foreach ($post as $key => $value) { |
|
| 115 | 1 | $newparams[3][$key] = $value; |
|
| 116 | 1 | unset($value); |
|
| 117 | } |
||
| 118 | 1 | $newparams[3]['xoops_text'] = $this->params[4]; |
|
| 119 | 1 | $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 | 1 | public function getPost() |
|
| 142 | { |
||
| 143 | 1 | if (!$this->_checkUser($this->params[2], $this->params[3])) { |
|
| 144 | $this->response->add(new XoopsXmlRpcFault(104)); |
||
| 145 | } else { |
||
| 146 | // XOOPS API ignores App key (index 0 of params) |
||
| 147 | 1 | array_shift($this->params); |
|
| 148 | 1 | $xoopsapi = $this->_getXoopsApi($this->params); |
|
| 149 | 1 | $xoopsapi->_setUser($this->user, $this->isadmin); |
|
| 150 | 1 | $ret = $xoopsapi->getPost(false); |
|
| 151 | if (is_array($ret)) { |
||
| 152 | $struct = new XoopsXmlRpcStruct(); |
||
| 153 | $content = ''; |
||
| 154 | View Code Duplication | foreach ($ret as $key => $value) { |
|
| 155 | $maptag = $this->_getXoopsTagMap($key); |
||
| 156 | switch($maptag) { |
||
| 157 | case 'userid': |
||
| 158 | $struct->add('userid', new XoopsXmlRpcString($value)); |
||
| 159 | break; |
||
| 160 | case 'dateCreated': |
||
| 161 | $struct->add('dateCreated', new XoopsXmlRpcDatetime($value)); |
||
| 162 | break; |
||
| 163 | case 'postid': |
||
| 164 | $struct->add('postid', new XoopsXmlRpcString($value)); |
||
| 165 | break; |
||
| 166 | default : |
||
| 167 | $content .= '<'.$key.'>'.trim($value).'</'.$key.'>'; |
||
| 168 | break; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | $struct->add('content', new XoopsXmlRpcString($content)); |
||
| 172 | $this->response->add($struct); |
||
| 173 | } else { |
||
| 174 | $this->response->add(new XoopsXmlRpcFault(106)); |
||
| 175 | } |
||
| 176 | } |
||
| 177 | } |
||
| 178 | |||
| 179 | 1 | public function getRecentPosts() |
|
| 180 | { |
||
| 181 | 1 | if (!$this->_checkUser($this->params[2], $this->params[3])) { |
|
| 182 | $this->response->add(new XoopsXmlRpcFault(104)); |
||
| 183 | } else { |
||
| 184 | // XOOPS API ignores App key (index 0 of params) |
||
| 185 | 1 | array_shift($this->params); |
|
| 186 | 1 | $xoopsapi = $this->_getXoopsApi($this->params); |
|
| 187 | 1 | $xoopsapi->_setUser($this->user, $this->isadmin); |
|
| 188 | 1 | $ret = $xoopsapi->getRecentPosts(false); |
|
| 189 | if (is_array($ret)) { |
||
| 190 | $arr = new XoopsXmlRpcArray(); |
||
| 191 | $count = count($ret); |
||
| 192 | if ($count == 0) { |
||
| 193 | $this->response->add(new XoopsXmlRpcFault(106, 'Found 0 Entries')); |
||
| 194 | } else { |
||
| 195 | for ($i = 0; $i < $count; ++$i) { |
||
| 196 | $struct = new XoopsXmlRpcStruct(); |
||
| 197 | $content = ''; |
||
| 198 | View Code Duplication | foreach($ret[$i] as $key => $value) { |
|
| 199 | $maptag = $this->_getXoopsTagMap($key); |
||
| 200 | switch($maptag) { |
||
| 201 | case 'userid': |
||
| 202 | $struct->add('userid', new XoopsXmlRpcString($value)); |
||
| 203 | break; |
||
| 204 | case 'dateCreated': |
||
| 205 | $struct->add('dateCreated', new XoopsXmlRpcDatetime($value)); |
||
| 206 | break; |
||
| 207 | case 'postid': |
||
| 208 | $struct->add('postid', new XoopsXmlRpcString($value)); |
||
| 209 | break; |
||
| 210 | default : |
||
| 211 | $content .= '<'.$key.'>'.trim($value).'</'.$key.'>'; |
||
| 212 | break; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | $struct->add('content', new XoopsXmlRpcString($content)); |
||
| 216 | $arr->add($struct); |
||
| 217 | unset($struct); |
||
| 218 | } |
||
| 219 | $this->response->add($arr); |
||
| 220 | } |
||
| 221 | } else { |
||
| 222 | $this->response->add(new XoopsXmlRpcFault(106)); |
||
| 223 | } |
||
| 224 | } |
||
| 225 | } |
||
| 226 | |||
| 227 | 1 | public function getUsersBlogs() |
|
| 242 | |||
| 243 | 1 | public function getUserInfo() |
|
| 258 | |||
| 259 | 1 | public function getTemplate() |
|
| 277 | |||
| 278 | 1 | public function setTemplate() |
|
| 286 | } |
||
| 287 |