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 xajaxResponse 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 xajaxResponse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 68 | class xajaxResponse |
||
| 69 | { |
||
| 70 | /**#@+ |
||
| 71 | * @access protected |
||
| 72 | */ |
||
| 73 | /** |
||
| 74 | * @var string internal XML storage |
||
| 75 | */ |
||
| 76 | var $xml; |
||
| 77 | /** |
||
| 78 | * @var string the encoding type to use |
||
| 79 | */ |
||
| 80 | var $sEncoding; |
||
| 81 | /** |
||
| 82 | * @var boolean if special characters in the XML should be converted to |
||
| 83 | * entities |
||
| 84 | */ |
||
| 85 | var $bOutputEntities; |
||
| 86 | |||
| 87 | /**#@-*/ |
||
| 88 | |||
| 89 | /** |
||
| 90 | * The constructor's main job is to set the character encoding for the |
||
| 91 | * response. |
||
| 92 | * |
||
| 93 | * <i>Note:</i> to change the character encoding for all of the |
||
| 94 | * responses, set the XAJAX_DEFAULT_ENCODING constant before you |
||
| 95 | * instantiate xajax. |
||
| 96 | * |
||
| 97 | * @param string contains the character encoding string to use |
||
| 98 | * @param boolean lets you set if you want special characters in the output |
||
| 99 | * converted to HTML entities |
||
| 100 | * |
||
| 101 | */ |
||
| 102 | public function __construct($sEncoding=XAJAX_DEFAULT_CHAR_ENCODING, $bOutputEntities=false) |
||
| 103 | { |
||
| 104 | $this->setCharEncoding($sEncoding); |
||
| 105 | $this->bOutputEntities = $bOutputEntities; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Sets the character encoding for the response based on $sEncoding, which |
||
| 110 | * is a string containing the character encoding to use. You don't need to |
||
| 111 | * use this method normally, since the character encoding for the response |
||
| 112 | * gets set automatically based on the XAJAX_DEFAULT_CHAR_ENCODING |
||
| 113 | * constant. |
||
| 114 | * |
||
| 115 | * @param string |
||
| 116 | */ |
||
| 117 | function setCharEncoding($sEncoding) |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Tells the response object to convert special characters to HTML entities |
||
| 124 | * automatically (only works if the mb_string extension is available). |
||
| 125 | */ |
||
| 126 | function outputEntitiesOn() |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Tells the response object to output special characters intact. (default |
||
| 133 | * behavior) |
||
| 134 | */ |
||
| 135 | function outputEntitiesOff() |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Adds a confirm commands command message to the XML response. |
||
| 142 | * |
||
| 143 | * <i>Usage:</i> <kbd>$objResponse->addConfirmCommands(1, "Do you want to preview the new data?");</kbd> |
||
| 144 | * |
||
| 145 | * @param integer the number of commands to skip if the user presses |
||
| 146 | * Cancel in the browsers's confirm dialog |
||
| 147 | * @param string the message to show in the browser's confirm dialog |
||
| 148 | */ |
||
| 149 | function addConfirmCommands($iCmdNumber, $sMessage) |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Adds an assign command message to the XML response. |
||
| 156 | * |
||
| 157 | * <i>Usage:</i> <kbd>$objResponse->addAssign("contentDiv", "innerHTML", "Some Text");</kbd> |
||
| 158 | * |
||
| 159 | * @param string contains the id of an HTML element |
||
| 160 | * @param string the part of the element you wish to modify ("innerHTML", |
||
| 161 | * "value", etc.) |
||
| 162 | * @param string the data you want to set the attribute to |
||
| 163 | */ |
||
| 164 | function addAssign($sTarget,$sAttribute,$sData) |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Adds an append command message to the XML response. |
||
| 171 | * |
||
| 172 | * <i>Usage:</i> <kbd>$objResponse->addAppend("contentDiv", "innerHTML", "Some New Text");</kbd> |
||
| 173 | * |
||
| 174 | * @param string contains the id of an HTML element |
||
| 175 | * @param string the part of the element you wish to modify ("innerHTML", |
||
| 176 | * "value", etc.) |
||
| 177 | * @param string the data you want to append to the end of the attribute |
||
| 178 | */ |
||
| 179 | function addAppend($sTarget,$sAttribute,$sData) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Adds an prepend command message to the XML response. |
||
| 186 | * |
||
| 187 | * <i>Usage:</i> <kbd>$objResponse->addPrepend("contentDiv", "innerHTML", "Some Starting Text");</kbd> |
||
| 188 | * |
||
| 189 | * @param string contains the id of an HTML element |
||
| 190 | * @param string the part of the element you wish to modify ("innerHTML", |
||
| 191 | * "value", etc.) |
||
| 192 | * @param string the data you want to prepend to the beginning of the |
||
| 193 | * attribute |
||
| 194 | */ |
||
| 195 | function addPrepend($sTarget,$sAttribute,$sData) |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Adds a replace command message to the XML response. |
||
| 202 | * |
||
| 203 | * <i>Usage:</i> <kbd>$objResponse->addReplace("contentDiv", "innerHTML", "text", "<b>text</b>");</kbd> |
||
| 204 | * |
||
| 205 | * @param string contains the id of an HTML element |
||
| 206 | * @param string the part of the element you wish to modify ("innerHTML", |
||
| 207 | * "value", etc.) |
||
| 208 | * @param string the string to search for |
||
| 209 | * @param string the string to replace the search string when found in the |
||
| 210 | * attribute |
||
| 211 | */ |
||
| 212 | function addReplace($sTarget,$sAttribute,$sSearch,$sData) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Adds a clear command message to the XML response. |
||
| 220 | * |
||
| 221 | * <i>Usage:</i> <kbd>$objResponse->addClear("contentDiv", "innerHTML");</kbd> |
||
| 222 | * |
||
| 223 | * @param string contains the id of an HTML element |
||
| 224 | * @param string the part of the element you wish to clear ("innerHTML", |
||
| 225 | * "value", etc.) |
||
| 226 | */ |
||
| 227 | function addClear($sTarget,$sAttribute) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Adds an alert command message to the XML response. |
||
| 234 | * |
||
| 235 | * <i>Usage:</i> <kbd>$objResponse->addAlert("This is important information");</kbd> |
||
| 236 | * |
||
| 237 | * @param string the text to be displayed in the Javascript alert box |
||
| 238 | */ |
||
| 239 | function addAlert($sMsg) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Uses the addScript() method to add a Javascript redirect to another URL. |
||
| 246 | * |
||
| 247 | * <i>Usage:</i> <kbd>$objResponse->addRedirect("http://www.xajaxproject.org");</kbd> |
||
| 248 | * |
||
| 249 | * @param string the URL to redirect the client browser to |
||
| 250 | */ |
||
| 251 | function addRedirect($sURL) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Adds a Javascript command message to the XML response. |
||
| 278 | * |
||
| 279 | * <i>Usage:</i> <kbd>$objResponse->addScript("var x = prompt('get some text');");</kbd> |
||
| 280 | * |
||
| 281 | * @param string contains Javascript code to be executed |
||
| 282 | */ |
||
| 283 | function addScript($sJS) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Adds a Javascript function call command message to the XML response. |
||
| 290 | * |
||
| 291 | * <i>Usage:</i> <kbd>$objResponse->addScriptCall("myJSFunction", "arg 1", "arg 2", 12345);</kbd> |
||
| 292 | * |
||
| 293 | * @param string $sFunc the name of a Javascript function |
||
| 294 | * @param mixed $args,... optional arguments to pass to the Javascript function |
||
| 295 | */ |
||
| 296 | function addScriptCall() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Adds a remove element command message to the XML response. |
||
| 305 | * |
||
| 306 | * <i>Usage:</i> <kbd>$objResponse->addRemove("Div2");</kbd> |
||
| 307 | * |
||
| 308 | * @param string contains the id of an HTML element to be removed |
||
| 309 | */ |
||
| 310 | function addRemove($sTarget) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Adds a create element command message to the XML response. |
||
| 317 | * |
||
| 318 | * <i>Usage:</i> <kbd>$objResponse->addCreate("parentDiv", "h3", "myid");</kbd> |
||
| 319 | * |
||
| 320 | * @param string contains the id of an HTML element to to which the new |
||
| 321 | * element will be appended. |
||
| 322 | * @param string the tag to be added |
||
| 323 | * @param string the id to be assigned to the new element |
||
| 324 | * @param string deprecated, use the addCreateInput() method instead |
||
| 325 | */ |
||
| 326 | function addCreate($sParent, $sTag, $sId, $sType="") |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Adds a insert element command message to the XML response. |
||
| 338 | * |
||
| 339 | * <i>Usage:</i> <kbd>$objResponse->addInsert("childDiv", "h3", "myid");</kbd> |
||
| 340 | * |
||
| 341 | * @param string contains the id of the child before which the new element |
||
| 342 | * will be inserted |
||
| 343 | * @param string the tag to be added |
||
| 344 | * @param string the id to be assigned to the new element |
||
| 345 | */ |
||
| 346 | function addInsert($sBefore, $sTag, $sId) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Adds a insert element command message to the XML response. |
||
| 353 | * |
||
| 354 | * <i>Usage:</i> <kbd>$objResponse->addInsertAfter("childDiv", "h3", "myid");</kbd> |
||
| 355 | * |
||
| 356 | * @param string contains the id of the child after which the new element |
||
| 357 | * will be inserted |
||
| 358 | * @param string the tag to be added |
||
| 359 | * @param string the id to be assigned to the new element |
||
| 360 | */ |
||
| 361 | function addInsertAfter($sAfter, $sTag, $sId) |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Adds a create input command message to the XML response. |
||
| 368 | * |
||
| 369 | * <i>Usage:</i> <kbd>$objResponse->addCreateInput("form1", "text", "username", "input1");</kbd> |
||
| 370 | * |
||
| 371 | * @param string contains the id of an HTML element to which the new input |
||
| 372 | * will be appended |
||
| 373 | * @param string the type of input to be created (text, radio, checkbox, |
||
| 374 | * etc.) |
||
| 375 | * @param string the name to be assigned to the new input and the variable |
||
| 376 | * name when it is submitted |
||
| 377 | * @param string the id to be assigned to the new input |
||
| 378 | */ |
||
| 379 | function addCreateInput($sParent, $sType, $sName, $sId) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Adds an insert input command message to the XML response. |
||
| 386 | * |
||
| 387 | * <i>Usage:</i> <kbd>$objResponse->addInsertInput("input5", "text", "username", "input1");</kbd> |
||
| 388 | * |
||
| 389 | * @param string contains the id of the child before which the new element |
||
| 390 | * will be inserted |
||
| 391 | * @param string the type of input to be created (text, radio, checkbox, |
||
| 392 | * etc.) |
||
| 393 | * @param string the name to be assigned to the new input and the variable |
||
| 394 | * name when it is submitted |
||
| 395 | * @param string the id to be assigned to the new input |
||
| 396 | */ |
||
| 397 | function addInsertInput($sBefore, $sType, $sName, $sId) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Adds an insert input command message to the XML response. |
||
| 404 | * |
||
| 405 | * <i>Usage:</i> <kbd>$objResponse->addInsertInputAfter("input7", "text", "email", "input2");</kbd> |
||
| 406 | * |
||
| 407 | * @param string contains the id of the child after which the new element |
||
| 408 | * will be inserted |
||
| 409 | * @param string the type of input to be created (text, radio, checkbox, |
||
| 410 | * etc.) |
||
| 411 | * @param string the name to be assigned to the new input and the variable |
||
| 412 | * name when it is submitted |
||
| 413 | * @param string the id to be assigned to the new input |
||
| 414 | */ |
||
| 415 | function addInsertInputAfter($sAfter, $sType, $sName, $sId) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Adds an event command message to the XML response. |
||
| 422 | * |
||
| 423 | * <i>Usage:</i> <kbd>$objResponse->addEvent("contentDiv", "onclick", "alert(\'Hello World\');");</kbd> |
||
| 424 | * |
||
| 425 | * @param string contains the id of an HTML element |
||
| 426 | * @param string the event you wish to set ("onclick", "onmouseover", etc.) |
||
| 427 | * @param string the Javascript string you want the event to invoke |
||
| 428 | */ |
||
| 429 | function addEvent($sTarget,$sEvent,$sScript) |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Adds a handler command message to the XML response. |
||
| 436 | * |
||
| 437 | * <i>Usage:</i> <kbd>$objResponse->addHandler("contentDiv", "onclick", "content_click");</kbd> |
||
| 438 | * |
||
| 439 | * @param string contains the id of an HTML element |
||
| 440 | * @param string the event you wish to set ("onclick", "onmouseover", etc.) |
||
| 441 | * @param string the name of a Javascript function that will handle the |
||
| 442 | * event. Multiple handlers can be added for the same event |
||
| 443 | */ |
||
| 444 | function addHandler($sTarget,$sEvent,$sHandler) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Adds a remove handler command message to the XML response. |
||
| 451 | * |
||
| 452 | * <i>Usage:</i> <kbd>$objResponse->addRemoveHandler("contentDiv", "onclick", "content_click");</kbd> |
||
| 453 | * |
||
| 454 | * @param string contains the id of an HTML element |
||
| 455 | * @param string the event you wish to remove ("onclick", "onmouseover", |
||
| 456 | * etc.) |
||
| 457 | * @param string the name of a Javascript handler function that you want to |
||
| 458 | * remove |
||
| 459 | */ |
||
| 460 | function addRemoveHandler($sTarget,$sEvent,$sHandler) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Adds an include script command message to the XML response. |
||
| 467 | * |
||
| 468 | * <i>Usage:</i> <kbd>$objResponse->addIncludeScript("functions.js");</kbd> |
||
| 469 | * |
||
| 470 | * @param string URL of the Javascript file to include |
||
| 471 | */ |
||
| 472 | function addIncludeScript($sFileName) |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Returns the XML to be returned from your function to the xajax processor |
||
| 479 | * on your page. Since xajax 0.2, you can also return an xajaxResponse |
||
| 480 | * object from your function directly, and xajax will automatically request |
||
| 481 | * the XML using this method call. |
||
| 482 | * |
||
| 483 | * <i>Usage:</i> <kbd>return $objResponse->getXML();</kbd> |
||
| 484 | * |
||
| 485 | * @return string response XML data |
||
| 486 | */ |
||
| 487 | function getXML() |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Adds the commands of the provided response XML output to this response |
||
| 499 | * object |
||
| 500 | * |
||
| 501 | * <i>Usage:</i> |
||
| 502 | * <code>$r1 = $objResponse1->getXML(); |
||
| 503 | * $objResponse2->loadXML($r1); |
||
| 504 | * return $objResponse2->getXML();</code> |
||
| 505 | * |
||
| 506 | * @param string the response XML (returned from a getXML() method) to add |
||
| 507 | * to the end of this response object |
||
| 508 | */ |
||
| 509 | function loadXML($mXML) |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Generates XML from command data |
||
| 524 | * |
||
| 525 | * @access private |
||
| 526 | * @param array associative array of attributes |
||
| 527 | * @param string data |
||
| 528 | * @return string XML command |
||
| 529 | */ |
||
| 530 | function _cmdXML($aAttributes, $sData) |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Recursively serializes a data structure in XML so it can be sent to |
||
| 561 | * the client. It could be thought of as the opposite of |
||
| 562 | * {@link xajax::_parseObjXml()}. |
||
| 563 | * |
||
| 564 | * @access private |
||
| 565 | * @param mixed data structure to serialize to XML |
||
| 566 | * @return string serialized XML |
||
| 567 | */ |
||
| 568 | function _buildObjXml($var) { |
||
| 585 | |||
| 586 | }// end class xajaxResponse |
||
| 587 | ?> |
||
| 588 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.