Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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()) |
||
| 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 getPhpCodeGlobalsVariables |
||
| 109 | * @param $type |
||
| 110 | * @param $var |
||
| 111 | * |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function getPhpCodeGlobalsVariables($type = 'REQUEST', $var = '') |
||
| 143 | |||
| 144 | /* |
||
| 145 | * @public function getPhpCodeRemoveCarriageReturn |
||
| 146 | * @param $string |
||
| 147 | * |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getPhpCodeRemoveCarriageReturn($string) |
||
| 154 | |||
| 155 | /* |
||
| 156 | * @public function getPhpCodeFileExists |
||
| 157 | * @param $filename |
||
| 158 | * |
||
| 159 | * @return string |
||
| 160 | */ |
||
| 161 | public function getPhpCodeFileExists($filename) |
||
| 165 | |||
| 166 | /* |
||
| 167 | * @public function getPhpCodeIncludeDir |
||
| 168 | * @param $directory |
||
| 169 | * @param $filename |
||
| 170 | * @param $once |
||
| 171 | * @param $isPath |
||
| 172 | * |
||
| 173 | * @return string |
||
| 174 | */ |
||
| 175 | public function getPhpCodeIncludeDir($directory = '', $filename = '', $once = false, $isPath = false, $type = 'include') |
||
| 193 | |||
| 194 | /* |
||
| 195 | * @public function getPhpCodeFunction |
||
| 196 | * @param $name |
||
| 197 | * @param $param |
||
| 198 | * @param $content |
||
| 199 | * @param $type |
||
| 200 | * @param $t |
||
| 201 | * |
||
| 202 | * @return string |
||
| 203 | */ |
||
| 204 | public function getPhpCodeFunction($name = '', $param = '', $content = '', $type = 'public ', $t = "\t") |
||
| 216 | |||
| 217 | /* |
||
| 218 | * @public function getPhpCodeConditions |
||
| 219 | * @param string $condition |
||
| 220 | * @param string $operator |
||
| 221 | * @param string $type |
||
| 222 | * @param string $contentIf |
||
| 223 | * @param mixed $contentElse |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | public function getPhpCodeConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $t = '') |
||
| 247 | |||
| 248 | /* |
||
| 249 | * @public function getPhpCodeForeach |
||
| 250 | * @param string $array |
||
| 251 | * @param string $arrayKey |
||
| 252 | * @param string $key |
||
| 253 | * @param string $value |
||
| 254 | * @param string $content |
||
| 255 | * |
||
| 256 | * @return string |
||
| 257 | */ |
||
| 258 | public function getPhpCodeForeach($array = '', $arrayKey = false, $key = false, $value = false, $content = '', $t = '') |
||
| 277 | |||
| 278 | /* |
||
| 279 | * @public function getPhpCodeFor |
||
| 280 | * @param $var |
||
| 281 | * @param $content |
||
| 282 | * @param $value |
||
| 283 | * @param $initVal |
||
| 284 | * @param $operator |
||
| 285 | * |
||
| 286 | * @return string |
||
| 287 | */ |
||
| 288 | public function getPhpCodeFor($var = '', $content = '', $value = '', $initVal = '', $operator = '', $t = '') |
||
| 298 | |||
| 299 | /* |
||
| 300 | * @public function getPhpCodeWhile |
||
| 301 | * @param $var |
||
| 302 | * @param $content |
||
| 303 | * @param $value |
||
| 304 | * @param $operator |
||
| 305 | * @param $t |
||
| 306 | * |
||
| 307 | * @return string |
||
| 308 | */ |
||
| 309 | public function getPhpCodeWhile($var = '', $content = '', $value = '', $operator = '', $t = '') |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @public function getPhpCodeSwitch |
||
| 322 | * |
||
| 323 | * @param $op |
||
| 324 | * @param $content |
||
| 325 | * @param $t |
||
| 326 | * |
||
| 327 | * @return string |
||
| 328 | */ |
||
| 329 | public function getPhpCodeSwitch($op = '', $content = '', $t = '') |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @public function getPhpCodeCaseSwitch |
||
| 343 | * |
||
| 344 | * @param $cases |
||
| 345 | * @param $defaultAfterCase |
||
| 346 | * @param $default |
||
| 347 | * @param $t |
||
| 348 | * |
||
| 349 | * @return string |
||
| 350 | */ |
||
| 351 | public function getPhpCodeCaseSwitch($cases = array(), $defaultAfterCase = false, $default = false, $t = "\t") |
||
| 379 | |||
| 380 | /* |
||
| 381 | * @public function getPhpCodeIsset |
||
| 382 | * @param $var |
||
| 383 | * @return string |
||
| 384 | */ |
||
| 385 | public function getPhpCodeIsset($var) |
||
| 389 | |||
| 390 | /* |
||
| 391 | * @public function getPhpCodeUnset |
||
| 392 | * @param $var |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | public function getPhpCodeUnset($var = '') |
||
| 399 | |||
| 400 | /* |
||
| 401 | * @public function getPhpCodeImplode |
||
| 402 | * @param $left |
||
| 403 | * @param $right |
||
| 404 | * @return string |
||
| 405 | */ |
||
| 406 | public function getPhpCodeImplode($left, $right) |
||
| 410 | |||
| 411 | /* |
||
| 412 | * @public function getPhpCodeExplode |
||
| 413 | * @param $left |
||
| 414 | * @param $right |
||
| 415 | * @return string |
||
| 416 | */ |
||
| 417 | public function getPhpCodeExplode($left, $right) |
||
| 421 | |||
| 422 | /* |
||
| 423 | * @public function getPhpCodeArray |
||
| 424 | * @param $return |
||
| 425 | * @param $left |
||
| 426 | * @param $right |
||
| 427 | * @param $isParam |
||
| 428 | * |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function getPhpCodeArray($return, $left, $right = '', $isParam = false) |
||
| 442 | |||
| 443 | /* |
||
| 444 | * @public function getPhpCodeArrayMerge |
||
| 445 | * @param $return |
||
| 446 | * @param $left |
||
| 447 | * @param $right |
||
| 448 | * @param $isParam |
||
| 449 | * |
||
| 450 | * @return string |
||
| 451 | */ |
||
| 452 | View Code Duplication | public function getPhpCodeArrayMerge($return, $left, $right, $isParam = false) |
|
| 462 | |||
| 463 | /* |
||
| 464 | * @public function getPhpCodeSprintf |
||
| 465 | * @param $left |
||
| 466 | * @param $right |
||
| 467 | * @return string |
||
| 468 | */ |
||
| 469 | public function getPhpCodeSprintf($left, $right) |
||
| 473 | |||
| 474 | /* |
||
| 475 | * @public function getPhpCodeEmpty |
||
| 476 | * @param $var |
||
| 477 | * @return string |
||
| 478 | */ |
||
| 479 | public function getPhpCodeEmpty($var) |
||
| 483 | |||
| 484 | /* |
||
| 485 | * @public function getPhpCodeHeader |
||
| 486 | * @param $var |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function getPhpCodeHeader($var) |
||
| 493 | |||
| 494 | /* |
||
| 495 | * @public function getPhpCodeRawurlencode |
||
| 496 | * @param $var |
||
| 497 | * @return string |
||
| 498 | */ |
||
| 499 | public function getPhpCodeRawurlencode($var) |
||
| 503 | |||
| 504 | /* |
||
| 505 | * @public function getPhpCodePregReplace |
||
| 506 | * @param $return |
||
| 507 | * @param $exp |
||
| 508 | * @param $str |
||
| 509 | * @param $val |
||
| 510 | * @param $isParam |
||
| 511 | * |
||
| 512 | * @return string |
||
| 513 | */ |
||
| 514 | View Code Duplication | public function getPhpCodePregReplace($return, $exp, $str, $val, $isParam = false) |
|
| 524 | |||
| 525 | /* |
||
| 526 | * @public function getPhpCodePregMatch |
||
| 527 | * @param $return |
||
| 528 | * @param $exp |
||
| 529 | * @param $str |
||
| 530 | * @param $val |
||
| 531 | * @param $isParam |
||
| 532 | * |
||
| 533 | * @return string |
||
| 534 | */ |
||
| 535 | View Code Duplication | public function getPhpCodePregMatch($return, $exp, $str, $val, $isParam = false) |
|
| 545 | |||
| 546 | /* |
||
| 547 | * @public function getPhpCodeStrReplace |
||
| 548 | * @param $left |
||
| 549 | * @param $var |
||
| 550 | * @param $str |
||
| 551 | * @param $value |
||
| 552 | * @param $isParam |
||
| 553 | * |
||
| 554 | * @return string |
||
| 555 | */ |
||
| 556 | public function getPhpCodeStrReplace($left, $var, $str, $value, $isParam = false) |
||
| 566 | |||
| 567 | /* |
||
| 568 | * @public function getPhpCodeStripTags |
||
| 569 | * @param $left |
||
| 570 | * @param $value |
||
| 571 | * @param $isParam |
||
| 572 | * |
||
| 573 | * @return string |
||
| 574 | */ |
||
| 575 | public function getPhpCodeStripTags($left = '', $value, $isParam = false) |
||
| 585 | |||
| 586 | /* |
||
| 587 | * @public function getPhpCodeHtmlentities |
||
| 588 | * @param $entitiesVar |
||
| 589 | * @param $entitiesQuote |
||
| 590 | * @return string |
||
| 591 | */ |
||
| 592 | public function getPhpCodeHtmlentities($entitiesVar, $entitiesQuote = false) |
||
| 599 | |||
| 600 | /* |
||
| 601 | * @public function getPhpCodeHtmlspecialchars |
||
| 602 | * @param $specialVar |
||
| 603 | * @param $specialQuote |
||
| 604 | * @return string |
||
| 605 | */ |
||
| 606 | public function getPhpCodeHtmlspecialchars($specialVar, $specialQuote = false) |
||
| 613 | } |
||
| 614 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.