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 = '') |
||
| 84 | { |
||
| 85 | $values = !empty($multiLine) ? $multiLine : array(); |
||
| 86 | $ret = "{$t}/**\n"; |
||
| 87 | foreach ($values as $string => $value) { |
||
| 88 | $ret .= "{$t} * {$string} {$value}\n {$t}"; |
||
| 89 | } |
||
| 90 | $ret .= "{$t}*/\n"; |
||
| 91 | |||
| 92 | return $ret; |
||
| 93 | } |
||
| 94 | |||
| 95 | /* |
||
| 96 | * @public function getPhpCodeDefine |
||
| 97 | * @param $left |
||
| 98 | * @param $right |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getPhpCodeDefine($left, $right) |
||
| 106 | |||
| 107 | /* |
||
| 108 | * @public function getPhpCodeDefine |
||
| 109 | * @param $left |
||
| 110 | * @param $right |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function getPhpCodeDefined($left = 'XOOPS_ROOT_PATH', $right = 'Restricted access') |
||
| 118 | |||
| 119 | /* |
||
| 120 | * @public function getPhpCodeGlobalsVariables |
||
| 121 | * @param $var |
||
| 122 | * @param $type |
||
| 123 | * |
||
| 124 | * @return string |
||
| 125 | */ |
||
| 126 | public function getPhpCodeGlobalsVariables($var = '', $type = 'REQUEST') |
||
| 155 | |||
| 156 | /* |
||
| 157 | * @public function getPhpCodeRemoveCarriageReturn |
||
| 158 | * @param $string |
||
| 159 | * |
||
| 160 | * @return string |
||
| 161 | */ |
||
| 162 | public function getPhpCodeRemoveCarriageReturn($string) |
||
| 166 | |||
| 167 | /* |
||
| 168 | * @public function getPhpCodeFileExists |
||
| 169 | * @param $filename |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | public function getPhpCodeFileExists($filename) |
||
| 177 | |||
| 178 | /* |
||
| 179 | * @public function getPhpCodeIncludeDir |
||
| 180 | * @param $directory |
||
| 181 | * @param $filename |
||
| 182 | * @param $once |
||
| 183 | * @param $isPath |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getPhpCodeIncludeDir($directory = '', $filename = '', $once = false, $isPath = false, $type = 'include') |
||
| 188 | { |
||
| 189 | if ($once == false) { |
||
| 190 | if ($isPath === false) { |
||
| 191 | $ret = "{$type} {$directory} .'/{$filename}.php';\n"; |
||
| 192 | } else { |
||
| 193 | $ret = "{$type} {$directory};\n"; |
||
| 194 | } |
||
| 195 | } else { |
||
| 196 | if ($isPath === false) { |
||
| 197 | $ret = "{$type}_once {$directory} .'/{$filename}.php';\n"; |
||
| 198 | } else { |
||
| 199 | $ret = "{$type}_once {$directory};\n"; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | return $ret; |
||
| 204 | } |
||
| 205 | |||
| 206 | /* |
||
| 207 | * @public function getPhpCodeTernaryOperator |
||
| 208 | * @param $return |
||
| 209 | * @param $condition |
||
| 210 | * @param $one |
||
| 211 | * @param $two |
||
| 212 | * |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | public function getPhpCodeTernaryOperator($return, $condition, $one, $two) |
||
| 219 | |||
| 220 | /* |
||
| 221 | * @public function getPhpCodeClass |
||
| 222 | * @param $name |
||
| 223 | * @param $content |
||
| 224 | * @param $extends |
||
| 225 | * @param $type |
||
| 226 | * |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | public function getPhpCodeClass($name = '', $content = '', $extends = null, $type = null) |
||
| 239 | |||
| 240 | /* |
||
| 241 | * @public function getPhpCodeClass |
||
| 242 | * @param $type |
||
| 243 | * @param $name |
||
| 244 | * @param $assign |
||
| 245 | * |
||
| 246 | * @return string |
||
| 247 | */ |
||
| 248 | public function getPhpCodeVariableClass($type = 'private', $name = '', $assign = 'null') |
||
| 252 | |||
| 253 | /* |
||
| 254 | * @public function getPhpCodeFunction |
||
| 255 | * @param $name |
||
| 256 | * @param $params |
||
| 257 | * @param $content |
||
| 258 | * @param $method |
||
| 259 | * @param $t - Indentation |
||
| 260 | * |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | public function getPhpCodeFunction($name = '', $params = '', $content = '', $method = null, $t = '') |
||
| 273 | |||
| 274 | /* |
||
| 275 | * @public function getPhpCodeConditions |
||
| 276 | * @param string $condition |
||
| 277 | * @param string $operator |
||
| 278 | * @param string $type |
||
| 279 | * @param string $contentIf |
||
| 280 | * @param mixed $contentElse |
||
| 281 | * @param $t - Indentation |
||
| 282 | * |
||
| 283 | * @return string |
||
| 284 | */ |
||
| 285 | public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $t = '') |
||
| 301 | |||
| 302 | /* |
||
| 303 | * @public function getPhpCodeForeach |
||
| 304 | * @param string $array |
||
| 305 | * @param string $arrayKey |
||
| 306 | * @param string $key |
||
| 307 | * @param string $value |
||
| 308 | * @param string $content |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function getPhpCodeForeach($array, $arrayKey = false, $key = false, $value = false, $content = '', $t = '') |
||
| 329 | |||
| 330 | /* |
||
| 331 | * @public function getPhpCodeFor |
||
| 332 | * @param $var |
||
| 333 | * @param $content |
||
| 334 | * @param $value |
||
| 335 | * @param $initVal |
||
| 336 | * @param $operator |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '', $t = '') |
||
| 348 | |||
| 349 | /* |
||
| 350 | * @public function getPhpCodeWhile |
||
| 351 | * @param $var |
||
| 352 | * @param $content |
||
| 353 | * @param $value |
||
| 354 | * @param $operator |
||
| 355 | * @param $t |
||
| 356 | * |
||
| 357 | * @return string |
||
| 358 | */ |
||
| 359 | public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '', $t = '') |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @public function getPhpCodeSwitch |
||
| 370 | * |
||
| 371 | * @param $op |
||
| 372 | * @param $content |
||
| 373 | * @param $t |
||
| 374 | * |
||
| 375 | * @return string |
||
| 376 | */ |
||
| 377 | public function getPhpCodeSwitch($op = '', $content = '', $t = '') |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @public function getPhpCodeCaseSwitch |
||
| 388 | * |
||
| 389 | * @param $cases |
||
| 390 | * @param $defaultAfterCase |
||
| 391 | * @param $default |
||
| 392 | * @param $t |
||
| 393 | * |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = "\t") |
||
| 424 | |||
| 425 | /* |
||
| 426 | * @public function getPhpCodeIsset |
||
| 427 | * @param $var |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | public function getPhpCodeIsset($var) |
||
| 434 | |||
| 435 | /* |
||
| 436 | * @public function getPhpCodeUnset |
||
| 437 | * @param $var |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | public function getPhpCodeUnset($var = '') |
||
| 444 | |||
| 445 | /* |
||
| 446 | * @public function getPhpCodeImplode |
||
| 447 | * @param $left |
||
| 448 | * @param $right |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | public function getPhpCodeImplode($left, $right) |
||
| 455 | |||
| 456 | /* |
||
| 457 | * @public function getPhpCodeExplode |
||
| 458 | * @param $left |
||
| 459 | * @param $right |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function getPhpCodeExplode($left, $right) |
||
| 466 | |||
| 467 | /* |
||
| 468 | * @public function getPhpCodeArray |
||
| 469 | * @param $var |
||
| 470 | * @param $left |
||
| 471 | * @param $right |
||
| 472 | * @param $key |
||
| 473 | * @param $isParam |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function getPhpCodeArray($var, $left = null, $right = null, $key = false, $isParam = false) |
||
| 491 | |||
| 492 | /* |
||
| 493 | * @public function getPhpCodeArrayType |
||
| 494 | * @param $var |
||
| 495 | * @param $type |
||
| 496 | * @param $left |
||
| 497 | * @param $right |
||
| 498 | * @param $isParam |
||
| 499 | * |
||
| 500 | * @return string |
||
| 501 | */ |
||
| 502 | public function getPhpCodeArrayType($var, $type, $left, $right, $isParam = false) |
||
| 512 | |||
| 513 | /* |
||
| 514 | * @public function getPhpCodeSprintf |
||
| 515 | * @param $left |
||
| 516 | * @param $right |
||
| 517 | * @return string |
||
| 518 | */ |
||
| 519 | public function getPhpCodeSprintf($left, $right) |
||
| 523 | |||
| 524 | /* |
||
| 525 | * @public function getPhpCodeEmpty |
||
| 526 | * @param $var |
||
| 527 | * @return string |
||
| 528 | */ |
||
| 529 | public function getPhpCodeEmpty($var) |
||
| 533 | |||
| 534 | /* |
||
| 535 | * @public function getPhpCodeHeader |
||
| 536 | * @param $var |
||
| 537 | * @return string |
||
| 538 | */ |
||
| 539 | public function getPhpCodeHeader($var) |
||
| 543 | |||
| 544 | /* |
||
| 545 | * @public function getPhpCodeRawurlencode |
||
| 546 | * @param $var |
||
| 547 | * @return string |
||
| 548 | */ |
||
| 549 | public function getPhpCodeRawurlencode($var) |
||
| 553 | |||
| 554 | /* |
||
| 555 | * @public function getPhpCodePregFunzions |
||
| 556 | * @param $return |
||
| 557 | * @param $exp |
||
| 558 | * @param $str |
||
| 559 | * @param $val |
||
| 560 | * @param $type |
||
| 561 | * @param $isParam |
||
| 562 | * |
||
| 563 | * @return string |
||
| 564 | */ |
||
| 565 | public function getPhpCodePregFunzions($return, $exp = '', $str, $val, $type = 'match', $isParam = false) |
||
| 576 | |||
| 577 | /* |
||
| 578 | * @public function getPhpCodeStrType |
||
| 579 | * @param $left |
||
| 580 | * @param $var |
||
| 581 | * @param $str |
||
| 582 | * @param $value |
||
| 583 | * @param $type |
||
| 584 | * @param $isParam |
||
| 585 | * |
||
| 586 | * @return string |
||
| 587 | */ |
||
| 588 | public function getPhpCodeStrType($left, $var, $str, $value, $type = 'replace', $isParam = false) |
||
| 599 | |||
| 600 | /* |
||
| 601 | * @public function getPhpCodeStripTags |
||
| 602 | * @param $left |
||
| 603 | * @param $value |
||
| 604 | * @param $isParam |
||
| 605 | * |
||
| 606 | * @return string |
||
| 607 | */ |
||
| 608 | public function getPhpCodeStripTags($left = '', $value, $isParam = false) |
||
| 618 | |||
| 619 | /* |
||
| 620 | * @public function getPhpCodeHtmlentities |
||
| 621 | * @param $entitiesVar |
||
| 622 | * @param $entitiesQuote |
||
| 623 | * @return string |
||
| 624 | */ |
||
| 625 | public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false) |
||
| 632 | |||
| 633 | /* |
||
| 634 | * @public function getPhpCodeHtmlspecialchars |
||
| 635 | * @param $specialVar |
||
| 636 | * @param $specialQuote |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false) |
||
| 646 | } |
||
| 647 |
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.