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 | * @var string |
||
| 32 | */ |
||
| 33 | protected $phpcode; |
||
| 34 | |||
| 35 | /* |
||
| 36 | * @public function constructor |
||
| 37 | * @param null |
||
| 38 | */ |
||
| 39 | /** |
||
| 40 | * |
||
| 41 | */ |
||
| 42 | public function __construct() |
||
| 45 | |||
| 46 | /* |
||
| 47 | * @static function &getInstance |
||
| 48 | * @param null |
||
| 49 | */ |
||
| 50 | /** |
||
| 51 | * @return TDMCreatePhpCode |
||
| 52 | */ |
||
| 53 | public static function &getInstance() |
||
| 62 | |||
| 63 | /* |
||
| 64 | * @public function getPhpCodeCommentLine |
||
| 65 | * @param $comment |
||
| 66 | * @param $var |
||
| 67 | * @return string |
||
| 68 | */ |
||
| 69 | public function getPhpCodeCommentLine($comment = '', $var = '') |
||
| 76 | |||
| 77 | /* |
||
| 78 | * @public function getPhpCodeCommentMultiLine |
||
| 79 | * @param $multiLine |
||
| 80 | * |
||
| 81 | * @return string |
||
| 82 | */ |
||
| 83 | public function getPhpCodeCommentMultiLine($multiLine = array(), $t = '') |
||
| 98 | |||
| 99 | /* |
||
| 100 | * @public function getPhpCodeDefine |
||
| 101 | * @param $left |
||
| 102 | * @param $right |
||
| 103 | * |
||
| 104 | * @return string |
||
| 105 | */ |
||
| 106 | public function getPhpCodeDefine($left, $right) |
||
| 110 | |||
| 111 | /* |
||
| 112 | * @public function getPhpCodeDefine |
||
| 113 | * @param $left |
||
| 114 | * @param $right |
||
| 115 | * |
||
| 116 | * @return string |
||
| 117 | */ |
||
| 118 | public function getPhpCodeDefined($left = 'XOOPS_ROOT_PATH', $right = 'Restricted access') |
||
| 122 | |||
| 123 | /* |
||
| 124 | * @public function getPhpCodeGlobalsVariables |
||
| 125 | * @param $var |
||
| 126 | * @param $type |
||
| 127 | * |
||
| 128 | * @return string |
||
| 129 | */ |
||
| 130 | public function getPhpCodeGlobalsVariables($var = '', $type = 'REQUEST') |
||
| 159 | |||
| 160 | /* |
||
| 161 | * @public function getPhpCodeRemoveCarriageReturn |
||
| 162 | * @param $string |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | public function getPhpCodeRemoveCarriageReturn($string) |
||
| 170 | |||
| 171 | /* |
||
| 172 | * @public function getPhpCodeFileExists |
||
| 173 | * @param $filename |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function getPhpCodeFileExists($filename) |
||
| 181 | |||
| 182 | /* |
||
| 183 | * @public function getPhpCodeIncludeDir |
||
| 184 | * @param $directory |
||
| 185 | * @param $filename |
||
| 186 | * @param $once |
||
| 187 | * @param $isPath |
||
| 188 | * |
||
| 189 | * @return string |
||
| 190 | */ |
||
| 191 | public function getPhpCodeIncludeDir($directory = '', $filename = '', $once = false, $isPath = false, $type = 'include') |
||
| 209 | |||
| 210 | /* |
||
| 211 | * @public function getPhpCodeTernaryOperator |
||
| 212 | * @param $return |
||
| 213 | * @param $condition |
||
| 214 | * @param $one |
||
| 215 | * @param $two |
||
| 216 | * |
||
| 217 | * @return string |
||
| 218 | */ |
||
| 219 | public function getPhpCodeTernaryOperator($return, $condition, $one, $two) |
||
| 223 | |||
| 224 | /* |
||
| 225 | * @public function getPhpCodeClass |
||
| 226 | * @param $name |
||
| 227 | * @param $content |
||
| 228 | * @param $extends |
||
| 229 | * @param $type |
||
| 230 | * |
||
| 231 | * @return string |
||
| 232 | */ |
||
| 233 | public function getPhpCodeClass($name = '', $content = '', $extends = null, $type = null) |
||
| 243 | |||
| 244 | /* |
||
| 245 | * @public function getPhpCodeClass |
||
| 246 | * @param $type |
||
| 247 | * @param $name |
||
| 248 | * @param $assign |
||
| 249 | * |
||
| 250 | * @return string |
||
| 251 | */ |
||
| 252 | public function getPhpCodeVariableClass($type = 'private', $name = '', $assign = 'null') |
||
| 256 | |||
| 257 | /* |
||
| 258 | * @public function getPhpCodeFunction |
||
| 259 | * @param $name |
||
| 260 | * @param $params |
||
| 261 | * @param $content |
||
| 262 | * @param $method |
||
| 263 | * @param $t - Indentation |
||
| 264 | * |
||
| 265 | * @return string |
||
| 266 | */ |
||
| 267 | public function getPhpCodeFunction($name = '', $params = '', $content = '', $method = null, $t = '') |
||
| 277 | |||
| 278 | /* |
||
| 279 | * @public function getPhpCodeConditions |
||
| 280 | * @param string $condition |
||
| 281 | * @param string $operator |
||
| 282 | * @param string $type |
||
| 283 | * @param string $contentIf |
||
| 284 | * @param mixed $contentElse |
||
| 285 | * @param $t - Indentation |
||
| 286 | * |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $t = '') |
||
| 305 | |||
| 306 | /* |
||
| 307 | * @public function getPhpCodeForeach |
||
| 308 | * @param string $array |
||
| 309 | * @param string $arrayKey |
||
| 310 | * @param string $key |
||
| 311 | * @param string $value |
||
| 312 | * @param string $content |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = '', $t = '') |
||
| 333 | |||
| 334 | /* |
||
| 335 | * @public function getPhpCodeFor |
||
| 336 | * @param $var |
||
| 337 | * @param $content |
||
| 338 | * @param $value |
||
| 339 | * @param $initVal |
||
| 340 | * @param $operator |
||
| 341 | * |
||
| 342 | * @return string |
||
| 343 | */ |
||
| 344 | public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '', $t = '') |
||
| 352 | |||
| 353 | /* |
||
| 354 | * @public function getPhpCodeWhile |
||
| 355 | * @param $var |
||
| 356 | * @param $content |
||
| 357 | * @param $value |
||
| 358 | * @param $operator |
||
| 359 | * @param $t |
||
| 360 | * |
||
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '', $t = '') |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @public function getPhpCodeSwitch |
||
| 374 | * |
||
| 375 | * @param $op |
||
| 376 | * @param $content |
||
| 377 | * @param $t |
||
| 378 | * |
||
| 379 | * @return string |
||
| 380 | */ |
||
| 381 | public function getPhpCodeSwitch($op = '', $content = '', $t = '') |
||
| 389 | |||
| 390 | /** |
||
| 391 | * @public function getPhpCodeCaseSwitch |
||
| 392 | * |
||
| 393 | * @param $cases |
||
| 394 | * @param $defaultAfterCase |
||
| 395 | * @param $default |
||
| 396 | * @param $t |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = "\t") |
||
| 428 | |||
| 429 | /* |
||
| 430 | * @public function getPhpCodeIsset |
||
| 431 | * @param $var |
||
| 432 | * @return string |
||
| 433 | */ |
||
| 434 | public function getPhpCodeIsset($var) |
||
| 438 | |||
| 439 | /* |
||
| 440 | * @public function getPhpCodeUnset |
||
| 441 | * @param $var |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | public function getPhpCodeUnset($var = '') |
||
| 448 | |||
| 449 | /* |
||
| 450 | * @public function getPhpCodeImplode |
||
| 451 | * @param $left |
||
| 452 | * @param $right |
||
| 453 | * @return string |
||
| 454 | */ |
||
| 455 | public function getPhpCodeImplode($left, $right) |
||
| 459 | |||
| 460 | /* |
||
| 461 | * @public function getPhpCodeExplode |
||
| 462 | * @param $left |
||
| 463 | * @param $right |
||
| 464 | * @return string |
||
| 465 | */ |
||
| 466 | public function getPhpCodeExplode($left, $right) |
||
| 470 | |||
| 471 | /* |
||
| 472 | * @public function getPhpCodeArray |
||
| 473 | * @param $var |
||
| 474 | * @param $left |
||
| 475 | * @param $right |
||
| 476 | * @param $key |
||
| 477 | * @param $isParam |
||
| 478 | * |
||
| 479 | * @return string |
||
| 480 | */ |
||
| 481 | public function getPhpCodeArray($var, $left = null, $right = null, $key = false, $isParam = false) |
||
| 495 | |||
| 496 | /* |
||
| 497 | * @public function getPhpCodeArrayType |
||
| 498 | * @param $var |
||
| 499 | * @param $type |
||
| 500 | * @param $left |
||
| 501 | * @param $right |
||
| 502 | * @param $isParam |
||
| 503 | * |
||
| 504 | * @return string |
||
| 505 | */ |
||
| 506 | public function getPhpCodeArrayType($var, $type, $left, $right, $isParam = false) |
||
| 516 | |||
| 517 | /* |
||
| 518 | * @public function getPhpCodeSprintf |
||
| 519 | * @param $left |
||
| 520 | * @param $right |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | public function getPhpCodeSprintf($left, $right) |
||
| 527 | |||
| 528 | /* |
||
| 529 | * @public function getPhpCodeEmpty |
||
| 530 | * @param $var |
||
| 531 | * @return string |
||
| 532 | */ |
||
| 533 | public function getPhpCodeEmpty($var) |
||
| 537 | |||
| 538 | /* |
||
| 539 | * @public function getPhpCodeHeader |
||
| 540 | * @param $var |
||
| 541 | * @return string |
||
| 542 | */ |
||
| 543 | public function getPhpCodeHeader($var) |
||
| 547 | |||
| 548 | /* |
||
| 549 | * @public function getPhpCodeRawurlencode |
||
| 550 | * @param $var |
||
| 551 | * @return string |
||
| 552 | */ |
||
| 553 | public function getPhpCodeRawurlencode($var) |
||
| 557 | |||
| 558 | /* |
||
| 559 | * @public function getPhpCodePregFunzions |
||
| 560 | * @param $return |
||
| 561 | * @param $exp |
||
| 562 | * @param $str |
||
| 563 | * @param $val |
||
| 564 | * @param $type |
||
| 565 | * @param $isParam |
||
| 566 | * |
||
| 567 | * @return string |
||
| 568 | */ |
||
| 569 | public function getPhpCodePregFunzions($return, $exp = '', $str, $val, $type = 'match', $isParam = false) |
||
| 580 | |||
| 581 | /* |
||
| 582 | * @public function getPhpCodeStrType |
||
| 583 | * @param $left |
||
| 584 | * @param $var |
||
| 585 | * @param $str |
||
| 586 | * @param $value |
||
| 587 | * @param $type |
||
| 588 | * @param $isParam |
||
| 589 | * |
||
| 590 | * @return string |
||
| 591 | */ |
||
| 592 | public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false) |
||
| 603 | |||
| 604 | /* |
||
| 605 | * @public function getPhpCodeStripTags |
||
| 606 | * @param $left |
||
| 607 | * @param $value |
||
| 608 | * @param $isParam |
||
| 609 | * |
||
| 610 | * @return string |
||
| 611 | */ |
||
| 612 | public function getPhpCodeStripTags($left = '', $value, $isParam = false) |
||
| 622 | |||
| 623 | /* |
||
| 624 | * @public function getPhpCodeHtmlentities |
||
| 625 | * @param $entitiesVar |
||
| 626 | * @param $entitiesQuote |
||
| 627 | * @return string |
||
| 628 | */ |
||
| 629 | public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false) |
||
| 636 | |||
| 637 | /* |
||
| 638 | * @public function getPhpCodeHtmlspecialchars |
||
| 639 | * @param $specialVar |
||
| 640 | * @param $specialQuote |
||
| 641 | * @return string |
||
| 642 | */ |
||
| 643 | public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false) |
||
| 650 | } |
||
| 651 |
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.