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 = '') |
||
| 222 | |||
| 223 | /* |
||
| 224 | * @public function getPhpCodeTernaryOperator |
||
| 225 | * @param $return |
||
| 226 | * @param $condition |
||
| 227 | * @param $one |
||
| 228 | * @param $two |
||
| 229 | * @param $t - Indentation |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getPhpCodeTernaryOperator($return, $condition, $one, $two, $t = '') |
||
| 237 | |||
| 238 | /* |
||
| 239 | * @public function getPhpCodeClass |
||
| 240 | * @param $name |
||
| 241 | * @param $content |
||
| 242 | * @param $extends |
||
| 243 | * @param $type |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getPhpCodeClass($name = null, $content = null, $extends = null, $type = null) |
||
| 258 | |||
| 259 | /* |
||
| 260 | * @public function getPhpCodeClass |
||
| 261 | * @param $type |
||
| 262 | * @param $name |
||
| 263 | * @param $assign |
||
| 264 | * @param $t - Indentation |
||
| 265 | * |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getPhpCodeVariableClass($type = 'private', $name = null, $assign = 'null', $t = '') |
||
| 272 | |||
| 273 | /* |
||
| 274 | * @public function getPhpCodeFunction |
||
| 275 | * @param $name |
||
| 276 | * @param $params |
||
| 277 | * @param $content |
||
| 278 | * @param $method |
||
| 279 | * @param $t - Indentation |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public function getPhpCodeFunction($name = null, $params = null, $content = null, $method = null, $isRef = false, $t = '') |
||
| 294 | |||
| 295 | /* |
||
| 296 | * @public function getPhpCodeConditions |
||
| 297 | * @param string $condition |
||
| 298 | * @param string $operator |
||
| 299 | * @param string $type |
||
| 300 | * @param string $contentIf |
||
| 301 | * @param mixed $contentElse |
||
| 302 | * @param $t - Indentation |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function getPhpCodeConditions($condition = null, $operator = null, $type = null, $contentIf = null, $contentElse = false, $t = '') |
||
| 322 | |||
| 323 | /* |
||
| 324 | * @public function getPhpCodeForeach |
||
| 325 | * @param string $array |
||
| 326 | * @param string $arrayKey |
||
| 327 | * @param string $key |
||
| 328 | * @param string $value |
||
| 329 | * @param string $content |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = null, $t = '') |
||
| 350 | |||
| 351 | /* |
||
| 352 | * @public function getPhpCodeFor |
||
| 353 | * @param $var |
||
| 354 | * @param $content |
||
| 355 | * @param $value |
||
| 356 | * @param $initVal |
||
| 357 | * @param $operator |
||
| 358 | * |
||
| 359 | * @return string |
||
| 360 | */ |
||
| 361 | public function getPhpCodeFor($var = null, $content = null, $value = null, $initVal = null, $operator = null, $t = '') |
||
| 369 | |||
| 370 | /* |
||
| 371 | * @public function getPhpCodeWhile |
||
| 372 | * @param $var |
||
| 373 | * @param $content |
||
| 374 | * @param $value |
||
| 375 | * @param $operator |
||
| 376 | * @param $t |
||
| 377 | * |
||
| 378 | * @return string |
||
| 379 | */ |
||
| 380 | public function getPhpCodeWhile($var = null, $content = null, $value = null, $operator = null, $t = '') |
||
| 388 | |||
| 389 | /** |
||
| 390 | * @public function getPhpCodeSwitch |
||
| 391 | * |
||
| 392 | * @param $op |
||
| 393 | * @param $content |
||
| 394 | * @param $t |
||
| 395 | * |
||
| 396 | * @return string |
||
| 397 | */ |
||
| 398 | public function getPhpCodeSwitch($op = null, $content = null, $t = '') |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @public function getPhpCodeCaseSwitch |
||
| 409 | * |
||
| 410 | * @param $cases |
||
| 411 | * @param $defaultAfterCase |
||
| 412 | * @param $default |
||
| 413 | * @param $t |
||
| 414 | * |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = '') |
||
| 445 | |||
| 446 | /* |
||
| 447 | * @public function getPhpCodeIsset |
||
| 448 | * @param $var |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public function getPhpCodeIsset($var) |
||
| 455 | |||
| 456 | /* |
||
| 457 | * @public function getPhpCodeUnset |
||
| 458 | * @param $var |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | public function getPhpCodeUnset($var = '', $t = '') |
||
| 465 | |||
| 466 | /* |
||
| 467 | * @public function getPhpCodeIsDir |
||
| 468 | * @param $var |
||
| 469 | * @return string |
||
| 470 | */ |
||
| 471 | public function getPhpCodeIsDir($var) |
||
| 475 | |||
| 476 | /* |
||
| 477 | * @public function getPhpCodeImplode |
||
| 478 | * @param $left |
||
| 479 | * @param $right |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | public function getPhpCodeImplode($left, $right) |
||
| 486 | |||
| 487 | /* |
||
| 488 | * @public function getPhpCodeExplode |
||
| 489 | * @param $left |
||
| 490 | * @param $right |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | public function getPhpCodeExplode($left, $right) |
||
| 497 | |||
| 498 | /* |
||
| 499 | * @public function getPhpCodeChmod |
||
| 500 | * @param $var |
||
| 501 | * @param $perm |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | public function getPhpCodeChmod($var, $perm = '0777', $t = '') |
||
| 508 | |||
| 509 | /* |
||
| 510 | * @public function getPhpCodeMkdir |
||
| 511 | * @param $var |
||
| 512 | * @param $perm |
||
| 513 | * @return string |
||
| 514 | */ |
||
| 515 | public function getPhpCodeMkdir($var, $perm = '0777', $t = '') |
||
| 519 | |||
| 520 | /* |
||
| 521 | * @public function getPhpCodeCopy |
||
| 522 | * @param $file |
||
| 523 | * @param $newfile |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | public function getPhpCodeCopy($file, $newfile = '', $t = '') |
||
| 530 | |||
| 531 | /* |
||
| 532 | * @public function getPhpCodeArray |
||
| 533 | * @param $var |
||
| 534 | * @param $array |
||
| 535 | * @param $isParam |
||
| 536 | * |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | public function getPhpCodeArray($var, $array = null, $isParam = false, $t = "\t\t") |
||
| 564 | |||
| 565 | /* |
||
| 566 | * @public function getPhpCodeArrayType |
||
| 567 | * @param $var |
||
| 568 | * @param $type |
||
| 569 | * @param $left |
||
| 570 | * @param $right |
||
| 571 | * @param $isParam |
||
| 572 | * |
||
| 573 | * @return string |
||
| 574 | */ |
||
| 575 | public function getPhpCodeArrayType($var, $type, $left, $right = null, $isParam = false, $t = "\t\t") |
||
| 586 | |||
| 587 | /* |
||
| 588 | * @public function getPhpCodeSprintf |
||
| 589 | * @param $left |
||
| 590 | * @param $right |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | public function getPhpCodeSprintf($left, $right) |
||
| 597 | |||
| 598 | /* |
||
| 599 | * @public function getPhpCodeEmpty |
||
| 600 | * @param $var |
||
| 601 | * @return string |
||
| 602 | */ |
||
| 603 | public function getPhpCodeEmpty($var) |
||
| 607 | |||
| 608 | /* |
||
| 609 | * @public function getPhpCodeHeader |
||
| 610 | * @param $var |
||
| 611 | * @return string |
||
| 612 | */ |
||
| 613 | public function getPhpCodeHeader($var) |
||
| 617 | |||
| 618 | /* |
||
| 619 | * @public function getPhpCodeRawurlencode |
||
| 620 | * @param $var |
||
| 621 | * @return string |
||
| 622 | */ |
||
| 623 | public function getPhpCodeRawurlencode($var) |
||
| 627 | |||
| 628 | /* |
||
| 629 | * @public function getPhpCodePregFunzions |
||
| 630 | * @param $return |
||
| 631 | * @param $exp |
||
| 632 | * @param $str |
||
| 633 | * @param $val |
||
| 634 | * @param $type |
||
| 635 | * @param $isParam |
||
| 636 | * |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | public function getPhpCodePregFunzions($return, $exp = null, $str, $val, $type = 'match', $isParam = false, $t = "\t") |
||
| 650 | |||
| 651 | /* |
||
| 652 | * @public function getPhpCodeStrType |
||
| 653 | * @param $left |
||
| 654 | * @param $var |
||
| 655 | * @param $str |
||
| 656 | * @param $value |
||
| 657 | * @param $type |
||
| 658 | * @param $isParam |
||
| 659 | * |
||
| 660 | * @return string |
||
| 661 | */ |
||
| 662 | public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false, $t = "\t") |
||
| 673 | |||
| 674 | /* |
||
| 675 | * @public function getPhpCodeStripTags |
||
| 676 | * @param $left |
||
| 677 | * @param $value |
||
| 678 | * @param $isParam |
||
| 679 | * |
||
| 680 | * @return string |
||
| 681 | */ |
||
| 682 | public function getPhpCodeStripTags($left = null, $value, $isParam = false, $t = '') |
||
| 692 | |||
| 693 | /* |
||
| 694 | * @public function getPhpCodeHtmlentities |
||
| 695 | * @param $entitiesVar |
||
| 696 | * @param $entitiesQuote |
||
| 697 | * @return string |
||
| 698 | */ |
||
| 699 | public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false) |
||
| 706 | |||
| 707 | /* |
||
| 708 | * @public function getPhpCodeHtmlspecialchars |
||
| 709 | * @param $specialVar |
||
| 710 | * @param $specialQuote |
||
| 711 | * @return string |
||
| 712 | */ |
||
| 713 | public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false) |
||
| 720 | } |
||
| 721 |
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.