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 = '', $var = '') |
||
| 71 | |||
| 72 | /* |
||
| 73 | * @public function getPhpCodeCommentMultiLine |
||
| 74 | * @param $multiLine |
||
| 75 | * |
||
| 76 | * @return string |
||
| 77 | */ |
||
| 78 | public function getPhpCodeCommentMultiLine($multiLine = array(), $t = '') |
||
| 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 getPhpCodeGlobalsVariables |
||
| 120 | * @param $var |
||
| 121 | * @param $type |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getPhpCodeGlobalsVariables($var = '', $type = 'REQUEST') |
||
| 154 | |||
| 155 | /* |
||
| 156 | * @public function getPhpCodeRemoveCarriageReturn |
||
| 157 | * @param $string |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getPhpCodeRemoveCarriageReturn($string) |
||
| 165 | |||
| 166 | /* |
||
| 167 | * @public function getPhpCodeFileExists |
||
| 168 | * @param $filename |
||
| 169 | * |
||
| 170 | * @return string |
||
| 171 | */ |
||
| 172 | public function getPhpCodeFileExists($filename) |
||
| 176 | |||
| 177 | /* |
||
| 178 | * @public function getPhpCodeIncludeDir |
||
| 179 | * @param $directory |
||
| 180 | * @param $filename |
||
| 181 | * @param $once |
||
| 182 | * @param $isPath |
||
| 183 | * |
||
| 184 | * @return string |
||
| 185 | */ |
||
| 186 | public function getPhpCodeIncludeDir($directory = '', $filename = '', $once = false, $isPath = false, $type = 'include') |
||
| 204 | |||
| 205 | /* |
||
| 206 | * @public function getPhpCodeTernaryOperator |
||
| 207 | * @param $return |
||
| 208 | * @param $condition |
||
| 209 | * @param $one |
||
| 210 | * @param $two |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | public function getPhpCodeTernaryOperator($return, $condition, $one, $two) |
||
| 218 | |||
| 219 | /* |
||
| 220 | * @public function getPhpCodeClass |
||
| 221 | * @param $name |
||
| 222 | * @param $content |
||
| 223 | * @param $extends |
||
| 224 | * @param $type |
||
| 225 | * |
||
| 226 | * @return string |
||
| 227 | */ |
||
| 228 | public function getPhpCodeClass($name = '', $content = '', $extends = null, $type = null) |
||
| 238 | |||
| 239 | /* |
||
| 240 | * @public function getPhpCodeClass |
||
| 241 | * @param $type |
||
| 242 | * @param $name |
||
| 243 | * @param $assign |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | public function getPhpCodeVariableClass($type = 'private', $name = '', $assign = 'null') |
||
| 251 | |||
| 252 | /* |
||
| 253 | * @public function getPhpCodeFunction |
||
| 254 | * @param $name |
||
| 255 | * @param $params |
||
| 256 | * @param $content |
||
| 257 | * @param $method |
||
| 258 | * @param $t - Indentation |
||
| 259 | * |
||
| 260 | * @return string |
||
| 261 | */ |
||
| 262 | public function getPhpCodeFunction($name = '', $params = '', $content = '', $method = null, $t = '') |
||
| 272 | |||
| 273 | /* |
||
| 274 | * @public function getPhpCodeConditions |
||
| 275 | * @param string $condition |
||
| 276 | * @param string $operator |
||
| 277 | * @param string $type |
||
| 278 | * @param string $contentIf |
||
| 279 | * @param mixed $contentElse |
||
| 280 | * @param $t - Indentation |
||
| 281 | * |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $t = '') |
||
| 300 | |||
| 301 | /* |
||
| 302 | * @public function getPhpCodeForeach |
||
| 303 | * @param string $array |
||
| 304 | * @param string $arrayKey |
||
| 305 | * @param string $key |
||
| 306 | * @param string $value |
||
| 307 | * @param string $content |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = '', $t = '') |
||
| 328 | |||
| 329 | /* |
||
| 330 | * @public function getPhpCodeFor |
||
| 331 | * @param $var |
||
| 332 | * @param $content |
||
| 333 | * @param $value |
||
| 334 | * @param $initVal |
||
| 335 | * @param $operator |
||
| 336 | * |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '', $t = '') |
||
| 347 | |||
| 348 | /* |
||
| 349 | * @public function getPhpCodeWhile |
||
| 350 | * @param $var |
||
| 351 | * @param $content |
||
| 352 | * @param $value |
||
| 353 | * @param $operator |
||
| 354 | * @param $t |
||
| 355 | * |
||
| 356 | * @return string |
||
| 357 | */ |
||
| 358 | public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '', $t = '') |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @public function getPhpCodeSwitch |
||
| 369 | * |
||
| 370 | * @param $op |
||
| 371 | * @param $content |
||
| 372 | * @param $t |
||
| 373 | * |
||
| 374 | * @return string |
||
| 375 | */ |
||
| 376 | public function getPhpCodeSwitch($op = '', $content = '', $t = '') |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @public function getPhpCodeCaseSwitch |
||
| 387 | * |
||
| 388 | * @param $cases |
||
| 389 | * @param $defaultAfterCase |
||
| 390 | * @param $default |
||
| 391 | * @param $t |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = "\t") |
||
| 423 | |||
| 424 | /* |
||
| 425 | * @public function getPhpCodeIsset |
||
| 426 | * @param $var |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getPhpCodeIsset($var) |
||
| 433 | |||
| 434 | /* |
||
| 435 | * @public function getPhpCodeUnset |
||
| 436 | * @param $var |
||
| 437 | * @return string |
||
| 438 | */ |
||
| 439 | public function getPhpCodeUnset($var = '') |
||
| 443 | |||
| 444 | /* |
||
| 445 | * @public function getPhpCodeImplode |
||
| 446 | * @param $left |
||
| 447 | * @param $right |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | public function getPhpCodeImplode($left, $right) |
||
| 454 | |||
| 455 | /* |
||
| 456 | * @public function getPhpCodeExplode |
||
| 457 | * @param $left |
||
| 458 | * @param $right |
||
| 459 | * @return string |
||
| 460 | */ |
||
| 461 | public function getPhpCodeExplode($left, $right) |
||
| 465 | |||
| 466 | /* |
||
| 467 | * @public function getPhpCodeArray |
||
| 468 | * @param $var |
||
| 469 | * @param $left |
||
| 470 | * @param $right |
||
| 471 | * @param $key |
||
| 472 | * @param $isParam |
||
| 473 | * |
||
| 474 | * @return string |
||
| 475 | */ |
||
| 476 | public function getPhpCodeArray($var, $left = null, $right = null, $key = false, $isParam = false) |
||
| 490 | |||
| 491 | /* |
||
| 492 | * @public function getPhpCodeArrayType |
||
| 493 | * @param $var |
||
| 494 | * @param $type |
||
| 495 | * @param $left |
||
| 496 | * @param $right |
||
| 497 | * @param $isParam |
||
| 498 | * |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | public function getPhpCodeArrayType($var, $type, $left, $right, $isParam = false) |
||
| 511 | |||
| 512 | /* |
||
| 513 | * @public function getPhpCodeSprintf |
||
| 514 | * @param $left |
||
| 515 | * @param $right |
||
| 516 | * @return string |
||
| 517 | */ |
||
| 518 | public function getPhpCodeSprintf($left, $right) |
||
| 522 | |||
| 523 | /* |
||
| 524 | * @public function getPhpCodeEmpty |
||
| 525 | * @param $var |
||
| 526 | * @return string |
||
| 527 | */ |
||
| 528 | public function getPhpCodeEmpty($var) |
||
| 532 | |||
| 533 | /* |
||
| 534 | * @public function getPhpCodeHeader |
||
| 535 | * @param $var |
||
| 536 | * @return string |
||
| 537 | */ |
||
| 538 | public function getPhpCodeHeader($var) |
||
| 542 | |||
| 543 | /* |
||
| 544 | * @public function getPhpCodeRawurlencode |
||
| 545 | * @param $var |
||
| 546 | * @return string |
||
| 547 | */ |
||
| 548 | public function getPhpCodeRawurlencode($var) |
||
| 552 | |||
| 553 | /* |
||
| 554 | * @public function getPhpCodePregFunzions |
||
| 555 | * @param $return |
||
| 556 | * @param $exp |
||
| 557 | * @param $str |
||
| 558 | * @param $val |
||
| 559 | * @param $type |
||
| 560 | * @param $isParam |
||
| 561 | * |
||
| 562 | * @return string |
||
| 563 | */ |
||
| 564 | public function getPhpCodePregFunzions($return, $exp = '', $str, $val, $type = 'match', $isParam = false) |
||
| 575 | |||
| 576 | /* |
||
| 577 | * @public function getPhpCodeStrType |
||
| 578 | * @param $left |
||
| 579 | * @param $var |
||
| 580 | * @param $str |
||
| 581 | * @param $value |
||
| 582 | * @param $type |
||
| 583 | * @param $isParam |
||
| 584 | * |
||
| 585 | * @return string |
||
| 586 | */ |
||
| 587 | public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false) |
||
| 598 | |||
| 599 | /* |
||
| 600 | * @public function getPhpCodeStripTags |
||
| 601 | * @param $left |
||
| 602 | * @param $value |
||
| 603 | * @param $isParam |
||
| 604 | * |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | public function getPhpCodeStripTags($left = '', $value, $isParam = false) |
||
| 617 | |||
| 618 | /* |
||
| 619 | * @public function getPhpCodeHtmlentities |
||
| 620 | * @param $entitiesVar |
||
| 621 | * @param $entitiesQuote |
||
| 622 | * @return string |
||
| 623 | */ |
||
| 624 | public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false) |
||
| 631 | |||
| 632 | /* |
||
| 633 | * @public function getPhpCodeHtmlspecialchars |
||
| 634 | * @param $specialVar |
||
| 635 | * @param $specialQuote |
||
| 636 | * @return string |
||
| 637 | */ |
||
| 638 | public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false) |
||
| 645 | } |
||
| 646 |
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.