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 TDMCreateHtmlSmartyCodes 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 TDMCreateHtmlSmartyCodes, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 29 | class TDMCreateHtmlSmartyCodes extends TDMCreateFile |
||
| 30 | { |
||
| 31 | /* |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $htmlcode; |
||
| 35 | |||
| 36 | /* |
||
| 37 | * @public function constructor |
||
| 38 | * @param null |
||
| 39 | */ |
||
| 40 | /** |
||
| 41 | * |
||
| 42 | */ |
||
| 43 | public function __construct() |
||
| 47 | |||
| 48 | /* |
||
| 49 | * @static function &getInstance |
||
| 50 | * @param null |
||
| 51 | */ |
||
| 52 | /** |
||
| 53 | * @return TDMCreateHtmlSmartyCodes |
||
| 54 | */ |
||
| 55 | public static function &getInstance() |
||
| 64 | |||
| 65 | /* |
||
| 66 | * @public function getHtmlTag |
||
| 67 | * @param string $tag |
||
| 68 | * @param array $attributes |
||
| 69 | * @param string $content |
||
| 70 | * @param bool $closed |
||
| 71 | */ |
||
| 72 | /** |
||
| 73 | * @param $tag |
||
| 74 | * @param $attributes |
||
| 75 | * @param $content |
||
| 76 | * @param $closed |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getHtmlTag($tag = '', $attributes = array(), $content = '', $closed = true) |
||
| 94 | |||
| 95 | /* |
||
| 96 | * @private function setAttributes |
||
| 97 | * @param array $attributes |
||
| 98 | */ |
||
| 99 | /** |
||
| 100 | * @param $attributes |
||
| 101 | * |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | private function getAttributes($attributes) |
||
| 115 | |||
| 116 | /* |
||
| 117 | * @public function getHtmlEmpty |
||
| 118 | * @param string $empty |
||
| 119 | */ |
||
| 120 | /** |
||
| 121 | * @param $empty |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getHtmlEmpty($empty = '') |
||
| 129 | |||
| 130 | /* |
||
| 131 | * @public function getHtmlComment |
||
| 132 | * @param string $htmlComment |
||
| 133 | */ |
||
| 134 | /** |
||
| 135 | * @param $htmlComment |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | public function getHtmlComment($htmlComment = '') |
||
| 143 | |||
| 144 | /* |
||
| 145 | * @public function getHtmlBr |
||
| 146 | * @param string $brNumb |
||
| 147 | * @param string $class |
||
| 148 | */ |
||
| 149 | /** |
||
| 150 | * @param $brNumb |
||
| 151 | * @param $class |
||
| 152 | * |
||
| 153 | * @return string |
||
| 154 | */ |
||
| 155 | public function getHtmlBr($brNumb = 1, $htmlClass = '') |
||
| 165 | |||
| 166 | /* |
||
| 167 | * @public function getHtmlHNumb |
||
| 168 | * @param string $htmlHClass |
||
| 169 | * @param string $content |
||
| 170 | */ |
||
| 171 | /** |
||
| 172 | * @param $content |
||
| 173 | * @param $htmlHClass |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function getHtmlHNumb($content = '', $n = '1', $htmlHClass = '') |
||
| 184 | |||
| 185 | /* |
||
| 186 | * @public function getHtmlDiv |
||
| 187 | * @param string $class |
||
| 188 | * @param string $content |
||
| 189 | */ |
||
| 190 | /** |
||
| 191 | * @param $content |
||
| 192 | * @param $class |
||
| 193 | * |
||
| 194 | * @return string |
||
| 195 | */ |
||
| 196 | public function getHtmlDiv($content = '', $divClass = '') |
||
| 205 | |||
| 206 | /* |
||
| 207 | * @public function getHtmlPre |
||
| 208 | * @param string $class |
||
| 209 | * @param string $content |
||
| 210 | */ |
||
| 211 | /** |
||
| 212 | * @param $content |
||
| 213 | * @param $class |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | public function getHtmlPre($content = '', $preClass = '') |
||
| 226 | |||
| 227 | /* |
||
| 228 | * @public function getHtmlSpan |
||
| 229 | * @param string $class |
||
| 230 | * @param string $content |
||
| 231 | */ |
||
| 232 | /** |
||
| 233 | * @param $content |
||
| 234 | * @param $class |
||
| 235 | * |
||
| 236 | * @return string |
||
| 237 | */ |
||
| 238 | public function getHtmlSpan($content = '', $spanClass = '') |
||
| 245 | |||
| 246 | /* |
||
| 247 | * @public function getHtmlParagraph |
||
| 248 | * @param string $class |
||
| 249 | * @param string $content |
||
| 250 | */ |
||
| 251 | /** |
||
| 252 | * @param $content |
||
| 253 | * @param $class |
||
| 254 | * |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | public function getHtmlParagraph($content = '', $pClass = '') |
||
| 266 | |||
| 267 | /* |
||
| 268 | * @public function getHtmlI |
||
| 269 | * @param string $class |
||
| 270 | * @param string $content |
||
| 271 | */ |
||
| 272 | /** |
||
| 273 | * @param $content |
||
| 274 | * @param $class |
||
| 275 | * |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | public function getHtmlI($content = '', $iClass = '') |
||
| 285 | |||
| 286 | /* |
||
| 287 | * @public function getHtmlUl |
||
| 288 | * @param string $class |
||
| 289 | * @param string $content |
||
| 290 | */ |
||
| 291 | /** |
||
| 292 | * @param $content |
||
| 293 | * @param $class |
||
| 294 | * |
||
| 295 | * @return string |
||
| 296 | */ |
||
| 297 | public function getHtmlUl($content = '', $ulClass = '') |
||
| 306 | |||
| 307 | /* |
||
| 308 | * @public function getHtmlOl |
||
| 309 | * @param string $class |
||
| 310 | * @param string $content |
||
| 311 | */ |
||
| 312 | /** |
||
| 313 | * @param $content |
||
| 314 | * @param $class |
||
| 315 | * |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function getHtmlOl($content = '', $olClass = '') |
||
| 327 | |||
| 328 | /* |
||
| 329 | * @public function getHtmlLi |
||
| 330 | * @param string $class |
||
| 331 | * @param string $content |
||
| 332 | */ |
||
| 333 | /** |
||
| 334 | * @param $content |
||
| 335 | * @param $class |
||
| 336 | * |
||
| 337 | * @return string |
||
| 338 | */ |
||
| 339 | public function getHtmlLi($content = '', $liClass = '') |
||
| 345 | |||
| 346 | /* |
||
| 347 | * @public function getHtmlStrong |
||
| 348 | * @param string $class |
||
| 349 | * @param string $content |
||
| 350 | */ |
||
| 351 | /** |
||
| 352 | * @param $content |
||
| 353 | * @param $class |
||
| 354 | * |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | public function getHtmlStrong($content = '', $strongClass = '') |
||
| 363 | |||
| 364 | /* |
||
| 365 | * @public function getHtmlAnchor |
||
| 366 | * @param string $class |
||
| 367 | * @param string $url |
||
| 368 | * @param string $target |
||
| 369 | * @param string $content |
||
| 370 | */ |
||
| 371 | /** |
||
| 372 | * @param $url |
||
| 373 | * @param $content |
||
| 374 | * @param $target |
||
| 375 | * @param $class |
||
| 376 | * |
||
| 377 | * @return string |
||
| 378 | */ |
||
| 379 | public function getHtmlAnchor($url = '#', $content = ' ', $title = '', $target = '', $aClass = '', $rel = '') |
||
| 387 | |||
| 388 | /* |
||
| 389 | * @public function getHtmlImage |
||
| 390 | * @param string $src |
||
| 391 | * @param string $alt |
||
| 392 | * @param string $class |
||
| 393 | */ |
||
| 394 | /** |
||
| 395 | * @param $src |
||
| 396 | * @param $alt |
||
| 397 | * @param $class |
||
| 398 | * |
||
| 399 | * @return string |
||
| 400 | */ |
||
| 401 | public function getHtmlImage($src = 'blank.gif', $alt = 'blank.gif', $imgClass = '') |
||
| 408 | |||
| 409 | /* |
||
| 410 | * @public function getHtmlTable |
||
| 411 | * @param string $class |
||
| 412 | * @param string $content |
||
| 413 | */ |
||
| 414 | /** |
||
| 415 | * @param $content |
||
| 416 | * @param $class |
||
| 417 | * |
||
| 418 | * @return string |
||
| 419 | */ |
||
| 420 | public function getHtmlTable($content = '', $tableClass = '') |
||
| 429 | |||
| 430 | /* |
||
| 431 | * @public function getHtmlTableThead |
||
| 432 | * @param string $class |
||
| 433 | * @param string $content |
||
| 434 | */ |
||
| 435 | /** |
||
| 436 | * @param $content |
||
| 437 | * @param $class |
||
| 438 | * |
||
| 439 | * @return string |
||
| 440 | */ |
||
| 441 | public function getHtmlTableThead($content = '', $theadClass = '') |
||
| 450 | |||
| 451 | /* |
||
| 452 | * @public function getHtmlTableTbody |
||
| 453 | * @param string $class |
||
| 454 | * @param string $content |
||
| 455 | */ |
||
| 456 | /** |
||
| 457 | * @param $content |
||
| 458 | * @param $class |
||
| 459 | * |
||
| 460 | * @return string |
||
| 461 | */ |
||
| 462 | public function getHtmlTableTbody($content = '', $tbodyClass = '') |
||
| 471 | |||
| 472 | /* |
||
| 473 | * @public function getHtmlTableTfoot |
||
| 474 | * @param string $class |
||
| 475 | * @param string $content |
||
| 476 | */ |
||
| 477 | /** |
||
| 478 | * @param $content |
||
| 479 | * @param $class |
||
| 480 | * |
||
| 481 | * @return string |
||
| 482 | */ |
||
| 483 | public function getHtmlTableTfoot($content = '', $tfootClass = '') |
||
| 492 | |||
| 493 | /* |
||
| 494 | * @public function getHtmlTableRow |
||
| 495 | * @param string $class |
||
| 496 | * @param string $content |
||
| 497 | */ |
||
| 498 | /** |
||
| 499 | * @param $content |
||
| 500 | * @param $class |
||
| 501 | * |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | public function getHtmlTableRow($content = '', $trClass = '') |
||
| 513 | |||
| 514 | /* |
||
| 515 | * @public function getHtmlTableHead |
||
| 516 | * @param string $class |
||
| 517 | * @param string $content |
||
| 518 | */ |
||
| 519 | /** |
||
| 520 | * @param $content |
||
| 521 | * @param $class |
||
| 522 | * @param $colspan |
||
| 523 | * |
||
| 524 | * @return string |
||
| 525 | */ |
||
| 526 | public function getHtmlTableHead($content = '', $thClass = '', $colspan = '') |
||
| 533 | |||
| 534 | /* |
||
| 535 | * @public function getHtmlTableData |
||
| 536 | * @param string $class |
||
| 537 | * @param string $content |
||
| 538 | */ |
||
| 539 | /** |
||
| 540 | * @param $content |
||
| 541 | * @param $class |
||
| 542 | * @param $colspan |
||
| 543 | * |
||
| 544 | * @return string |
||
| 545 | */ |
||
| 546 | public function getHtmlTableData($content = '', $tdClass = '', $colspan = '') |
||
| 553 | |||
| 554 | /* |
||
| 555 | * @public function getSmartyComment |
||
| 556 | * @param string $comment |
||
| 557 | */ |
||
| 558 | /** |
||
| 559 | * @param $comment |
||
| 560 | * |
||
| 561 | * @return string |
||
| 562 | */ |
||
| 563 | public function getSmartyComment($comment = '') |
||
| 567 | |||
| 568 | /* |
||
| 569 | * @public function getSmartyNoSimbol |
||
| 570 | * @param string $content |
||
| 571 | */ |
||
| 572 | /** |
||
| 573 | * @param $content |
||
| 574 | * |
||
| 575 | * @return string |
||
| 576 | */ |
||
| 577 | public function getSmartyNoSimbol($noSimbol = '') |
||
| 581 | |||
| 582 | /* |
||
| 583 | * @public function getSmartyConst |
||
| 584 | * @param string $language |
||
| 585 | * @param mixed $const |
||
| 586 | */ |
||
| 587 | /** |
||
| 588 | * @param $language |
||
| 589 | * @param $const |
||
| 590 | * |
||
| 591 | * @return string |
||
| 592 | */ |
||
| 593 | public function getSmartyConst($language, $const) |
||
| 597 | |||
| 598 | /* |
||
| 599 | * @public function getSmartySingleVar |
||
| 600 | * @param string $var |
||
| 601 | */ |
||
| 602 | /** |
||
| 603 | * @param string $var |
||
| 604 | * |
||
| 605 | * @return string |
||
| 606 | */ |
||
| 607 | public function getSmartySingleVar($var) |
||
| 611 | |||
| 612 | /* |
||
| 613 | * @public function getSmartyDoubleVar |
||
| 614 | * @param string $leftVar |
||
| 615 | * @param string $rightVar |
||
| 616 | */ |
||
| 617 | /** |
||
| 618 | * @param string $leftVar |
||
| 619 | * @param string $rightVar |
||
| 620 | * |
||
| 621 | * @return string |
||
| 622 | */ |
||
| 623 | public function getSmartyDoubleVar($leftVar, $rightVar) |
||
| 627 | |||
| 628 | /* |
||
| 629 | * @public function getSmartyIncludeFile |
||
| 630 | * @param string $name |
||
| 631 | */ |
||
| 632 | /** |
||
| 633 | * @param $moduleDirname |
||
| 634 | * @param $fileName |
||
| 635 | * @param $admin |
||
| 636 | * |
||
| 637 | * @return string |
||
| 638 | */ |
||
| 639 | public function getSmartyIncludeFile($moduleDirname, $fileName = 'header', $admin = false, $q = false) |
||
| 653 | |||
| 654 | /* |
||
| 655 | * @public function getSmartyIncludeFileListSection |
||
| 656 | * @param string $name |
||
| 657 | */ |
||
| 658 | /** |
||
| 659 | * @param $moduleDirname |
||
| 660 | * @param $fileName |
||
| 661 | * @param $tableFieldName |
||
| 662 | * |
||
| 663 | * @return string |
||
| 664 | */ |
||
| 665 | public function getSmartyIncludeFileListSection($moduleDirname, $fileName, $tableFieldName) |
||
| 669 | |||
| 670 | /* |
||
| 671 | * @public function getSmartyIncludeFileListForeach |
||
| 672 | * @param string $name |
||
| 673 | */ |
||
| 674 | /** |
||
| 675 | * @param $moduleDirname |
||
| 676 | * @param $fileName |
||
| 677 | * @param $tableFieldName |
||
| 678 | * |
||
| 679 | * @return string |
||
| 680 | */ |
||
| 681 | public function getSmartyIncludeFileListForeach($moduleDirname, $fileName, $tableFieldName) |
||
| 685 | |||
| 686 | /* |
||
| 687 | * @public function getSmartyConditions |
||
| 688 | * @param string $condition |
||
| 689 | * @param string $operator |
||
| 690 | * @param string $type |
||
| 691 | * @param string $contentIf |
||
| 692 | * @param mixed $contentElse |
||
| 693 | * @param bool $count |
||
| 694 | */ |
||
| 695 | /** |
||
| 696 | * @param string $condition |
||
| 697 | * @param string $operator |
||
| 698 | * @param string $type |
||
| 699 | * @param string $contentIf |
||
| 700 | * @param mixed $contentElse |
||
| 701 | * @param bool $count |
||
| 702 | * |
||
| 703 | * @return string |
||
| 704 | */ |
||
| 705 | public function getSmartyConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $count = false, $noSimbol = false) |
||
| 733 | |||
| 734 | /* |
||
| 735 | * @public function getSmartyForeach |
||
| 736 | * @param string $item |
||
| 737 | * @param string $from |
||
| 738 | * @param string $content |
||
| 739 | */ |
||
| 740 | /** |
||
| 741 | * @param string $item |
||
| 742 | * @param string $from |
||
| 743 | * @param string $content |
||
| 744 | * |
||
| 745 | * @return string |
||
| 746 | */ |
||
| 747 | View Code Duplication | public function getSmartyForeach($item = 'item', $from = 'from', $content = 'content', $name = '', $key = '') |
|
| 757 | |||
| 758 | /* |
||
| 759 | * @public function getSmartyForeachQuery |
||
| 760 | * @param string $item |
||
| 761 | * @param string $from |
||
| 762 | * @param string $content |
||
| 763 | */ |
||
| 764 | /** |
||
| 765 | * @param string $item |
||
| 766 | * @param string $from |
||
| 767 | * @param string $content |
||
| 768 | * |
||
| 769 | * @return string |
||
| 770 | */ |
||
| 771 | View Code Duplication | public function getSmartyForeachQuery($item = 'item', $from = 'from', $content = 'content', $loop = 'loop', $key = '') |
|
| 781 | |||
| 782 | /* |
||
| 783 | * @public function getSmartySection |
||
| 784 | * @param string $name |
||
| 785 | * @param string $loop |
||
| 786 | * @param string $content |
||
| 787 | */ |
||
| 788 | /** |
||
| 789 | * @param string $name |
||
| 790 | * @param string $loop |
||
| 791 | * @param string $content |
||
| 792 | * |
||
| 793 | * @return string |
||
| 794 | */ |
||
| 795 | View Code Duplication | public function getSmartySection($name = 'name', $loop = 'loop', $content = 'content', $start = 0, $step = 0) |
|
| 805 | } |
||
| 806 |
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.