Complex classes like TDMCreatePhpCode 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 TDMCreatePhpCode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | class TDMCreatePhpCode |
||
|
|
|||
| 29 | { |
||
| 30 | /* |
||
| 31 | * @public function constructor |
||
| 32 | * @param null |
||
| 33 | */ |
||
| 34 | /** |
||
| 35 | * |
||
| 36 | */ |
||
| 37 | public function __construct() |
||
| 40 | |||
| 41 | /* |
||
| 42 | * @static function &getInstance |
||
| 43 | * @param null |
||
| 44 | */ |
||
| 45 | /** |
||
| 46 | * @return TDMCreatePhpCode |
||
| 47 | */ |
||
| 48 | public static function &getInstance() |
||
| 57 | |||
| 58 | /* |
||
| 59 | * @public function getPhpCodeCommentLine |
||
| 60 | * @param $comment |
||
| 61 | * @param $var |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public function getPhpCodeCommentLine($comment = null, $var = null, $t = '') |
||
| 65 | { |
||
| 66 | $value = !empty($var) ? ' '.$var : ''; |
||
| 67 | $ret = "{$t}// {$comment}{$value}\n"; |
||
| 68 | |||
| 69 | return $ret; |
||
| 70 | } |
||
| 71 | |||
| 72 | /* |
||
| 73 | * @public function getPhpCodeCommentMultiLine |
||
| 74 | * @param $multiLine |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function getPhpCodeCommentMultiLine($multiLine = array(), $t = '') |
||
| 79 | { |
||
| 80 | $values = !empty($multiLine) ? $multiLine : array(); |
||
| 81 | $ret = "\n{$t}/**\n"; |
||
| 82 | foreach ($values as $string => $value) { |
||
| 83 | if ($string === '' && $value === '') { |
||
| 84 | $ret .= "{$t} *\n"; |
||
| 85 | } else { |
||
| 86 | $ret .= "{$t} * {$string} {$value}\n"; |
||
| 87 | } |
||
| 88 | } |
||
| 89 | $ret .= "{$t} */\n"; |
||
| 90 | |||
| 91 | return $ret; |
||
| 92 | } |
||
| 93 | |||
| 94 | /* |
||
| 95 | * @public function getPhpCodeDefine |
||
| 96 | * @param $left |
||
| 97 | * @param $right |
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | public function getPhpCodeDefine($left, $right) |
||
| 105 | |||
| 106 | /* |
||
| 107 | * @public function getPhpCodeDefine |
||
| 108 | * @param $left |
||
| 109 | * @param $right |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function getPhpCodeDefined($left = 'XOOPS_ROOT_PATH', $right = 'Restricted access') |
||
| 117 | |||
| 118 | /* |
||
| 119 | * @public function getPhpCodeGlobals |
||
| 120 | * @param $var |
||
| 121 | * @param $value |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getPhpCodeGlobals($var, $value = '') |
||
| 135 | |||
| 136 | /* |
||
| 137 | * @public function getPhpCodeGlobalsVariables |
||
| 138 | * @param $var |
||
| 139 | * @param $type |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | */ |
||
| 143 | public function getPhpCodeGlobalsVariables($var = null, $type = 'REQUEST') |
||
| 172 | |||
| 173 | /* |
||
| 174 | * @public function getPhpCodeRemoveCarriageReturn |
||
| 175 | * @param $string |
||
| 176 | * |
||
| 177 | * @return string |
||
| 178 | */ |
||
| 179 | public function getPhpCodeRemoveCarriageReturn($string, $n = "\n", $t = "\r") |
||
| 183 | |||
| 184 | /* |
||
| 185 | * @public function getPhpCodeFileExists |
||
| 186 | * @param $filename |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getPhpCodeFileExists($filename) |
||
| 194 | |||
| 195 | /* |
||
| 196 | * @public function getPhpCodeIncludeDir |
||
| 197 | * @param $directory |
||
| 198 | * @param $filename |
||
| 199 | * @param $once |
||
| 200 | * @param $isPath |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function getPhpCodeIncludeDir($directory = null, $filename = null, $once = false, $isPath = false, $type = 'include', $t = '') |
||
| 225 | |||
| 226 | /* |
||
| 227 | * @public function getPhpCodeTernaryOperator |
||
| 228 | * @param $return |
||
| 229 | * @param $condition |
||
| 230 | * @param $one |
||
| 231 | * @param $two |
||
| 232 | * @param $t - Indentation |
||
| 233 | * |
||
| 234 | * @return string |
||
| 235 | */ |
||
| 236 | public function getPhpCodeTernaryOperator($return, $condition, $one, $two, $t = '') |
||
| 240 | |||
| 241 | /* |
||
| 242 | * @public function getPhpCodeClass |
||
| 243 | * @param $name |
||
| 244 | * @param $content |
||
| 245 | * @param $extends |
||
| 246 | * @param $type |
||
| 247 | * |
||
| 248 | * @return string |
||
| 249 | */ |
||
| 250 | public function getPhpCodeClass($name = null, $content = null, $extends = null, $type = null) |
||
| 261 | |||
| 262 | /* |
||
| 263 | * @public function getPhpCodeClass |
||
| 264 | * @param $type |
||
| 265 | * @param $name |
||
| 266 | * @param $assign |
||
| 267 | * @param $t - Indentation |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function getPhpCodeVariableClass($type = 'private', $name = null, $assign = 'null', $t = '') |
||
| 275 | |||
| 276 | /* |
||
| 277 | * @public function getPhpCodeFunction |
||
| 278 | * @param $name |
||
| 279 | * @param $params |
||
| 280 | * @param $content |
||
| 281 | * @param $method |
||
| 282 | * @param $t - Indentation |
||
| 283 | * |
||
| 284 | * @return string |
||
| 285 | */ |
||
| 286 | public function getPhpCodeFunction($name = null, $params = null, $content = null, $method = null, $isRef = false, $t = '') |
||
| 297 | |||
| 298 | /* |
||
| 299 | * @public function getPhpCodeConditions |
||
| 300 | * @param string $condition |
||
| 301 | * @param string $operator |
||
| 302 | * @param string $type |
||
| 303 | * @param string $contentIf |
||
| 304 | * @param mixed $contentElse |
||
| 305 | * @param $t - Indentation |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function getPhpCodeConditions($condition = null, $operator = null, $type = null, $contentIf = null, $contentElse = false, $t = '') |
||
| 325 | |||
| 326 | /* |
||
| 327 | * @public function getPhpCodeForeach |
||
| 328 | * @param string $array |
||
| 329 | * @param string $arrayKey |
||
| 330 | * @param string $key |
||
| 331 | * @param string $value |
||
| 332 | * @param string $content |
||
| 333 | * |
||
| 334 | * @return string |
||
| 335 | */ |
||
| 336 | public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = null, $t = '') |
||
| 353 | |||
| 354 | /* |
||
| 355 | * @public function getPhpCodeFor |
||
| 356 | * @param $var |
||
| 357 | * @param $content |
||
| 358 | * @param $value |
||
| 359 | * @param $initVal |
||
| 360 | * @param $operator |
||
| 361 | * |
||
| 362 | * @return string |
||
| 363 | */ |
||
| 364 | public function getPhpCodeFor($var = null, $content = null, $value = null, $initVal = null, $operator = null, $t = '') |
||
| 372 | |||
| 373 | /* |
||
| 374 | * @public function getPhpCodeWhile |
||
| 375 | * @param $var |
||
| 376 | * @param $content |
||
| 377 | * @param $value |
||
| 378 | * @param $operator |
||
| 379 | * @param $t |
||
| 380 | * |
||
| 381 | * @return string |
||
| 382 | */ |
||
| 383 | public function getPhpCodeWhile($var = null, $content = null, $value = null, $operator = null, $t = '') |
||
| 391 | |||
| 392 | /** |
||
| 393 | * @public function getPhpCodeSwitch |
||
| 394 | * |
||
| 395 | * @param $op |
||
| 396 | * @param $content |
||
| 397 | * @param $t |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function getPhpCodeSwitch($op = null, $content = null, $t = '') |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @public function getPhpCodeCaseSwitch |
||
| 412 | * |
||
| 413 | * @param $cases |
||
| 414 | * @param $defaultAfterCase |
||
| 415 | * @param $default |
||
| 416 | * @param $t |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = '') |
||
| 448 | |||
| 449 | /* |
||
| 450 | * @public function getPhpCodeIsset |
||
| 451 | * @param $var |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | public function getPhpCodeIsset($var) |
||
| 458 | |||
| 459 | /* |
||
| 460 | * @public function getPhpCodeUnset |
||
| 461 | * @param $var |
||
| 462 | * @return string |
||
| 463 | */ |
||
| 464 | public function getPhpCodeUnset($var = '', $t = '') |
||
| 468 | |||
| 469 | /* |
||
| 470 | * @public function getPhpCodeIsDir |
||
| 471 | * @param $var |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | public function getPhpCodeIsDir($var) |
||
| 478 | |||
| 479 | /* |
||
| 480 | * @public function getPhpCodeImplode |
||
| 481 | * @param $left |
||
| 482 | * @param $right |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | public function getPhpCodeImplode($left, $right) |
||
| 489 | |||
| 490 | /* |
||
| 491 | * @public function getPhpCodeExplode |
||
| 492 | * @param $left |
||
| 493 | * @param $right |
||
| 494 | * @return string |
||
| 495 | */ |
||
| 496 | public function getPhpCodeExplode($left, $right) |
||
| 500 | |||
| 501 | /* |
||
| 502 | * @public function getPhpCodeChmod |
||
| 503 | * @param $var |
||
| 504 | * @param $perm |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | public function getPhpCodeChmod($var, $perm = '0777', $t = '') |
||
| 511 | |||
| 512 | /* |
||
| 513 | * @public function getPhpCodeMkdir |
||
| 514 | * @param $var |
||
| 515 | * @param $perm |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public function getPhpCodeMkdir($var, $perm = '0777', $t = '') |
||
| 522 | |||
| 523 | /* |
||
| 524 | * @public function getPhpCodeCopy |
||
| 525 | * @param $file |
||
| 526 | * @param $newfile |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | public function getPhpCodeCopy($file, $newfile = '', $t = '') |
||
| 533 | |||
| 534 | /* |
||
| 535 | * @public function getPhpCodeArray |
||
| 536 | * @param $var |
||
| 537 | * @param $array |
||
| 538 | * @param $isParam |
||
| 539 | * |
||
| 540 | * @return string |
||
| 541 | */ |
||
| 542 | public function getPhpCodeArray($var, $array = null, $isParam = false, $t = "\t\t") |
||
| 567 | |||
| 568 | /* |
||
| 569 | * @public function getPhpCodeArrayType |
||
| 570 | * @param $var |
||
| 571 | * @param $type |
||
| 572 | * @param $left |
||
| 573 | * @param $right |
||
| 574 | * @param $isParam |
||
| 575 | * |
||
| 576 | * @return string |
||
| 577 | */ |
||
| 578 | public function getPhpCodeArrayType($var, $type, $left, $right = null, $isParam = false, $t = "\t\t") |
||
| 589 | |||
| 590 | /* |
||
| 591 | * @public function getPhpCodeSprintf |
||
| 592 | * @param $left |
||
| 593 | * @param $right |
||
| 594 | * @return string |
||
| 595 | */ |
||
| 596 | public function getPhpCodeSprintf($left, $right) |
||
| 600 | |||
| 601 | /* |
||
| 602 | * @public function getPhpCodeEmpty |
||
| 603 | * @param $var |
||
| 604 | * @return string |
||
| 605 | */ |
||
| 606 | public function getPhpCodeEmpty($var) |
||
| 610 | |||
| 611 | /* |
||
| 612 | * @public function getPhpCodeHeader |
||
| 613 | * @param $var |
||
| 614 | * @return string |
||
| 615 | */ |
||
| 616 | public function getPhpCodeHeader($var) |
||
| 620 | |||
| 621 | /* |
||
| 622 | * @public function getPhpCodeRawurlencode |
||
| 623 | * @param $var |
||
| 624 | * @return string |
||
| 625 | */ |
||
| 626 | public function getPhpCodeRawurlencode($var) |
||
| 630 | |||
| 631 | /* |
||
| 632 | * @public function getPhpCodePregFunzions |
||
| 633 | * @param $var |
||
| 634 | * @param $exp |
||
| 635 | * @param $str |
||
| 636 | * @param $val |
||
| 637 | * @param $type |
||
| 638 | * @param $isParam |
||
| 639 | * |
||
| 640 | * @return string |
||
| 641 | */ |
||
| 642 | public function getPhpCodePregFunzions($var, $exp = null, $str, $val, $type = 'match', $isParam = false, $t = "\t") |
||
| 653 | |||
| 654 | /* |
||
| 655 | * @public function getPhpCodeStrType |
||
| 656 | * @param $left |
||
| 657 | * @param $var |
||
| 658 | * @param $str |
||
| 659 | * @param $value |
||
| 660 | * @param $type |
||
| 661 | * @param $isParam |
||
| 662 | * |
||
| 663 | * @return string |
||
| 664 | */ |
||
| 665 | public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false, $t = "\t") |
||
| 676 | |||
| 677 | /* |
||
| 678 | * @public function getPhpCodeStripTags |
||
| 679 | * @param $left |
||
| 680 | * @param $value |
||
| 681 | * @param $isParam |
||
| 682 | * |
||
| 683 | * @return string |
||
| 684 | */ |
||
| 685 | public function getPhpCodeStripTags($left = null, $value, $isParam = false, $t = '') |
||
| 695 | |||
| 696 | /* |
||
| 697 | * @public function getPhpCodeHtmlentities |
||
| 698 | * @param $entitiesVar |
||
| 699 | * @param $entitiesQuote |
||
| 700 | * @return string |
||
| 701 | */ |
||
| 702 | public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false) |
||
| 709 | |||
| 710 | /* |
||
| 711 | * @public function getPhpCodeHtmlspecialchars |
||
| 712 | * @param $specialVar |
||
| 713 | * @param $specialQuote |
||
| 714 | * @return string |
||
| 715 | */ |
||
| 716 | public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false) |
||
| 723 | } |
||
| 724 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.