Complex classes like XTemplate 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 XTemplate, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 97 | class XTemplate { |
||
| 98 | |||
| 99 | /* |
||
| 100 | xtemplate class 0.2.4-3 |
||
| 101 | html generation with templates - fast & easy |
||
| 102 | copyright (c) 2000 barnabás debreceni [[email protected]] |
||
| 103 | code optimization by Ivar Smolin <[email protected]> 14-march-2001 |
||
| 104 | latest stable & CVS version always available @ http://sourceforge.net/projects/xtpl |
||
| 105 | |||
| 106 | tested with php 3.0.11 and 4.0.4pl1 |
||
| 107 | |||
| 108 | This program is free software; you can redistribute it and/or |
||
| 109 | modify it under the terms of the GNU Lesser General Public License |
||
| 110 | version 2.1 as published by the Free Software Foundation. |
||
| 111 | |||
| 112 | This library is distributed in the hope that it will be useful, |
||
| 113 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 114 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 115 | GNU Lesser General Public License for more details at |
||
| 116 | http://www.gnu.org/copyleft/lgpl.html |
||
| 117 | |||
| 118 | You should have received a copy of the GNU General Public License |
||
| 119 | along with this program; if not, write to the Free Software |
||
| 120 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
||
| 121 | |||
| 122 | |||
| 123 | */ |
||
| 124 | |||
| 125 | /***[ variables ]***********************************************************/ |
||
| 126 | |||
| 127 | var $filecontents=""; /* raw contents of template file */ |
||
| 128 | var $blocks=array(); /* unparsed blocks */ |
||
| 129 | var $parsed_blocks=array(); /* parsed blocks */ |
||
| 130 | var $block_parse_order=array(); /* block parsing order for recursive parsing (sometimes reverse:) */ |
||
| 131 | var $sub_blocks=array(); /* store sub-block names for fast resetting */ |
||
| 132 | var $VARS=array(); /* variables array */ |
||
| 133 | var $alternate_include_directory = ""; |
||
| 134 | |||
| 135 | var $file_delim="/\{FILE\s*\"([^\"]+)\"\s*\}/m"; /* regexp for file includes */ |
||
| 136 | var $block_start_delim="<!-- "; /* block start delimiter */ |
||
| 137 | var $block_end_delim="-->"; /* block end delimiter */ |
||
| 138 | var $block_start_word="BEGIN:"; /* block start word */ |
||
| 139 | var $block_end_word="END:"; /* block end word */ |
||
| 140 | |||
| 141 | /* this makes the delimiters look like: <!-- BEGIN: block_name --> if you use my syntax. */ |
||
| 142 | |||
| 143 | var $NULL_STRING=array(""=>""); /* null string for unassigned vars */ |
||
| 144 | var $NULL_BLOCK=array(""=>""); /* null string for unassigned blocks */ |
||
| 145 | var $mainblock=""; |
||
| 146 | var $ERROR=""; |
||
| 147 | var $AUTORESET=1; /* auto-reset sub blocks */ |
||
| 148 | |||
| 149 | /***[ constructor ]*********************************************************/ |
||
| 150 | |||
| 151 | 2 | function __construct ($file, $alt_include = "", $mainblock="main") { |
|
| 159 | |||
| 160 | /** |
||
| 161 | * @deprecated deprecated since version 7.6, PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code, use __construct instead |
||
| 162 | */ |
||
| 163 | function XTemplate($file, $alt_include = "", $mainblock="main"){ |
||
| 164 | $deprecatedMessage = 'PHP4 Style Constructors are deprecated and will be remove in 7.8, please update your code'; |
||
| 165 | if(isset($GLOBALS['log'])) { |
||
| 166 | $GLOBALS['log']->deprecated($deprecatedMessage); |
||
| 167 | } |
||
| 168 | else { |
||
| 169 | trigger_error($deprecatedMessage, E_USER_DEPRECATED); |
||
| 170 | } |
||
| 171 | self::__construct($file, $alt_include, $mainblock); |
||
| 172 | } |
||
| 173 | |||
| 174 | |||
| 175 | |||
| 176 | /***************************************************************************/ |
||
| 177 | /***[ public stuff ]********************************************************/ |
||
| 178 | /***************************************************************************/ |
||
| 179 | |||
| 180 | |||
| 181 | /***[ assign ]**************************************************************/ |
||
| 182 | /* |
||
| 183 | assign a variable |
||
| 184 | */ |
||
| 185 | |||
| 186 | 2 | function assign ($name,$val="") { |
|
| 187 | 2 | if (is_array($name)) { |
|
| 188 | foreach ($name as $k => $v) { |
||
| 189 | $this->VARS[$k] = $v; |
||
| 190 | } |
||
| 191 | } else { |
||
| 192 | 2 | $this->VARS[$name]=$val; |
|
| 193 | } |
||
| 194 | 2 | } |
|
| 195 | |||
| 196 | function append ($varname, $name,$val="") { |
||
| 197 | if(!isset($this->VARS[$varname])){ |
||
| 198 | $this->VARS[$varname] = array(); |
||
| 199 | } |
||
| 200 | if(is_array($this->VARS[$varname])){ |
||
| 201 | $this->VARS[$varname][$name] = $val; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | |||
| 205 | /***[ parse ]***************************************************************/ |
||
| 206 | /* |
||
| 207 | parse a block |
||
| 208 | */ |
||
| 209 | |||
| 210 | 2 | function parse ($bname) { |
|
| 291 | |||
| 292 | /***[ exists ]**************************************************************/ |
||
| 293 | /* |
||
| 294 | returns true if a block exists otherwise returns false. |
||
| 295 | */ |
||
| 296 | function exists($bname){ |
||
| 297 | return (!empty($this->parsed_blocks[$bname])) || (!empty($this->blocks[$bname])); |
||
| 298 | } |
||
| 299 | |||
| 300 | |||
| 301 | /***[ var_exists ]**************************************************************/ |
||
| 302 | /* |
||
| 303 | returns true if a block exists otherwise returns false. |
||
| 304 | */ |
||
| 305 | function var_exists($bname,$vname){ |
||
| 306 | if(!empty($this->blocks[$bname])){ |
||
| 307 | return substr_count($this->blocks[$bname], '{'. $vname . '}') >0; |
||
| 308 | } |
||
| 309 | return false; |
||
| 310 | } |
||
| 311 | |||
| 312 | |||
| 313 | /***[ rparse ]**************************************************************/ |
||
| 314 | /* |
||
| 315 | returns the parsed text for a block, including all sub-blocks. |
||
| 316 | */ |
||
| 317 | |||
| 318 | function rparse($bname) { |
||
| 319 | if (!empty($this->sub_blocks[$bname])) { |
||
| 320 | reset($this->sub_blocks[$bname]); |
||
| 321 | while (list($k,$v)=each($this->sub_blocks[$bname])) |
||
|
|
|||
| 322 | if (!empty($v)) |
||
| 323 | $this->rparse($v,$indent."\t"); |
||
| 324 | } |
||
| 325 | $this->parse($bname); |
||
| 326 | } |
||
| 327 | |||
| 328 | /***[ insert_loop ]*********************************************************/ |
||
| 329 | /* |
||
| 330 | inserts a loop ( call assign & parse ) |
||
| 331 | */ |
||
| 332 | |||
| 333 | function insert_loop($bname,$var,$value="") { |
||
| 334 | $this->assign($var,$value); |
||
| 335 | $this->parse($bname); |
||
| 336 | } |
||
| 337 | |||
| 338 | /***[ text ]****************************************************************/ |
||
| 339 | /* |
||
| 340 | returns the parsed text for a block |
||
| 341 | */ |
||
| 342 | |||
| 343 | 2 | function text($bname) { |
|
| 344 | |||
| 345 | 2 | if(!empty($this->parsed_blocks)){ |
|
| 346 | 2 | return $this->parsed_blocks[isset($bname) ? $bname :$this->mainblock]; |
|
| 347 | }else{ |
||
| 348 | return ''; |
||
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | /***[ out ]*****************************************************************/ |
||
| 353 | /* |
||
| 354 | prints the parsed text |
||
| 355 | */ |
||
| 356 | |||
| 357 | function out ($bname) { |
||
| 358 | global $focus; |
||
| 359 | |||
| 360 | if(isset($focus)){ |
||
| 361 | global $action; |
||
| 362 | |||
| 363 | if($focus && is_subclass_of($focus, 'SugarBean') && !$focus->ACLAccess($action)){ |
||
| 364 | |||
| 365 | ACLController::displayNoAccess(true); |
||
| 366 | |||
| 367 | sugar_die(''); |
||
| 368 | return; |
||
| 369 | }} |
||
| 370 | |||
| 371 | echo $this->text($bname); |
||
| 372 | } |
||
| 373 | |||
| 374 | /***[ reset ]***************************************************************/ |
||
| 375 | /* |
||
| 376 | resets the parsed text |
||
| 377 | */ |
||
| 378 | |||
| 379 | 2 | function reset ($bname) { |
|
| 380 | 2 | $this->parsed_blocks[$bname]=""; |
|
| 381 | 2 | } |
|
| 382 | |||
| 383 | /***[ parsed ]**************************************************************/ |
||
| 384 | /* |
||
| 385 | returns true if block was parsed, false if not |
||
| 386 | */ |
||
| 387 | |||
| 388 | function parsed ($bname) { |
||
| 389 | return (!empty($this->parsed_blocks[$bname])); |
||
| 390 | } |
||
| 391 | |||
| 392 | /***[ SetNullString ]*******************************************************/ |
||
| 393 | /* |
||
| 394 | sets the string to replace in case the var was not assigned |
||
| 395 | */ |
||
| 396 | |||
| 397 | function SetNullString($str,$varname="") { |
||
| 398 | $this->NULL_STRING[$varname]=$str; |
||
| 399 | } |
||
| 400 | |||
| 401 | /***[ SetNullBlock ]********************************************************/ |
||
| 402 | /* |
||
| 403 | sets the string to replace in case the block was not parsed |
||
| 404 | */ |
||
| 405 | |||
| 406 | function SetNullBlock($str,$bname="") { |
||
| 407 | $this->NULL_BLOCK[$bname]=$str; |
||
| 408 | } |
||
| 409 | |||
| 410 | /***[ set_autoreset ]*******************************************************/ |
||
| 411 | /* |
||
| 412 | sets AUTORESET to 1. (default is 1) |
||
| 413 | if set to 1, parse() automatically resets the parsed blocks' sub blocks |
||
| 414 | (for multiple level blocks) |
||
| 415 | */ |
||
| 416 | |||
| 417 | function set_autoreset() { |
||
| 418 | $this->AUTORESET=1; |
||
| 419 | } |
||
| 420 | |||
| 421 | /***[ clear_autoreset ]*****************************************************/ |
||
| 422 | /* |
||
| 423 | sets AUTORESET to 0. (default is 1) |
||
| 424 | if set to 1, parse() automatically resets the parsed blocks' sub blocks |
||
| 425 | (for multiple level blocks) |
||
| 426 | */ |
||
| 427 | |||
| 428 | function clear_autoreset() { |
||
| 429 | $this->AUTORESET=0; |
||
| 430 | } |
||
| 431 | |||
| 432 | /***[ scan_globals ]********************************************************/ |
||
| 433 | /* |
||
| 434 | scans global variables |
||
| 435 | */ |
||
| 436 | |||
| 437 | function scan_globals() { |
||
| 438 | reset($GLOBALS); |
||
| 439 | while (list($k,$v)=each($GLOBALS)) |
||
| 440 | $GLOB[$k]=$v; |
||
| 441 | $this->assign("PHP",$GLOB); /* access global variables as {PHP.HTTP_HOST} in your template! */ |
||
| 442 | } |
||
| 443 | |||
| 444 | /****** |
||
| 445 | |||
| 446 | WARNING |
||
| 447 | PUBLIC FUNCTIONS BELOW THIS LINE DIDN'T GET TESTED |
||
| 448 | |||
| 449 | ******/ |
||
| 450 | |||
| 451 | |||
| 452 | /***************************************************************************/ |
||
| 453 | /***[ private stuff ]*******************************************************/ |
||
| 454 | /***************************************************************************/ |
||
| 455 | |||
| 456 | /***[ maketree ]************************************************************/ |
||
| 457 | /* |
||
| 458 | generates the array containing to-be-parsed stuff: |
||
| 459 | $blocks["main"],$blocks["main.table"],$blocks["main.table.row"], etc. |
||
| 460 | also builds the reverse parse order. |
||
| 461 | */ |
||
| 462 | |||
| 463 | |||
| 464 | 2 | function maketree($con,$block) { |
|
| 465 | 2 | $con2=explode($this->block_start_delim,$con); |
|
| 466 | 2 | $level=0; |
|
| 467 | 2 | $block_names=array(); |
|
| 468 | 2 | $blocks=array(); |
|
| 469 | 2 | reset($con2); |
|
| 470 | 2 | while(list($k,$v)=each($con2)) { |
|
| 471 | 2 | $patt="($this->block_start_word|$this->block_end_word)\s*(\w+)\s*$this->block_end_delim(.*)"; |
|
| 472 | 2 | if (preg_match_all("/$patt/ims",$v,$res, PREG_SET_ORDER)) { |
|
| 473 | // $res[0][1] = BEGIN or END |
||
| 474 | // $res[0][2] = block name |
||
| 475 | // $res[0][3] = kinda content |
||
| 476 | 2 | if ($res[0][1]==$this->block_start_word) { |
|
| 477 | 2 | $parent_name=implode(".",$block_names); |
|
| 478 | 2 | $block_names[++$level]=$res[0][2]; /* add one level - array("main","table","row")*/ |
|
| 479 | 2 | $cur_block_name=implode(".",$block_names); /* make block name (main.table.row) */ |
|
| 480 | 2 | $this->block_parse_order[]=$cur_block_name; /* build block parsing order (reverse) */ |
|
| 481 | |||
| 482 | 2 | if(array_key_exists($cur_block_name, $blocks)) |
|
| 483 | { |
||
| 484 | $blocks[$cur_block_name].=$res[0][3]; /* add contents */ |
||
| 485 | } |
||
| 486 | else |
||
| 487 | { |
||
| 488 | 2 | $blocks[$cur_block_name]=$res[0][3]; /* add contents */ |
|
| 489 | } |
||
| 490 | |||
| 491 | /* add {_BLOCK_.blockname} string to parent block */ |
||
| 492 | 2 | if(array_key_exists($parent_name, $blocks)) |
|
| 493 | { |
||
| 494 | 2 | $blocks[$parent_name].="{_BLOCK_.$cur_block_name}"; |
|
| 495 | } |
||
| 496 | else |
||
| 497 | { |
||
| 498 | 2 | $blocks[$parent_name]="{_BLOCK_.$cur_block_name}"; |
|
| 499 | } |
||
| 500 | |||
| 501 | 2 | $this->sub_blocks[$parent_name][]=$cur_block_name; /* store sub block names for autoresetting and recursive parsing */ |
|
| 502 | 2 | $this->sub_blocks[$cur_block_name][]=""; /* store sub block names for autoresetting */ |
|
| 503 | 2 | } else if ($res[0][1]==$this->block_end_word) { |
|
| 504 | 2 | unset($block_names[$level--]); |
|
| 505 | 2 | $parent_name=implode(".",$block_names); |
|
| 506 | 2 | $blocks[$parent_name].=$res[0][3]; /* add rest of block to parent block */ |
|
| 507 | } |
||
| 508 | } else { /* no block delimiters found */ |
||
| 509 | 2 | $index = implode(".",$block_names); |
|
| 510 | 2 | if(array_key_exists($index, $blocks)) |
|
| 511 | { |
||
| 512 | $blocks[].=$this->block_start_delim.$v; |
||
| 513 | } |
||
| 514 | else |
||
| 515 | { |
||
| 516 | 2 | $blocks[]=$this->block_start_delim.$v; |
|
| 517 | } |
||
| 518 | } |
||
| 519 | } |
||
| 520 | 2 | return $blocks; |
|
| 521 | } |
||
| 522 | |||
| 523 | |||
| 524 | |||
| 525 | /***[ error stuff ]*********************************************************/ |
||
| 526 | /* |
||
| 527 | sets and gets error |
||
| 528 | */ |
||
| 529 | |||
| 530 | function get_error() { |
||
| 531 | return ($this->ERROR=="")?0:$this->ERROR; |
||
| 532 | } |
||
| 533 | |||
| 534 | |||
| 535 | function set_error($str) { |
||
| 536 | $this->ERROR=$str; |
||
| 537 | } |
||
| 538 | |||
| 539 | /***[ getfile ]*************************************************************/ |
||
| 540 | /* |
||
| 541 | returns the contents of a file |
||
| 542 | */ |
||
| 543 | |||
| 544 | 2 | function getfile($file) { |
|
| 545 | 2 | if (!isset($file)) { |
|
| 546 | $this->set_error("!isset file name!"); |
||
| 547 | return ""; |
||
| 548 | } |
||
| 549 | |||
| 550 | // Pick which folder we should include from |
||
| 551 | // Prefer the local directory, then try the theme directory. |
||
| 552 | 2 | if (!is_file($file)) |
|
| 553 | $file = $this->alternate_include_directory.$file; |
||
| 554 | |||
| 555 | 2 | if(is_file($file)) |
|
| 556 | { |
||
| 557 | 2 | $file_text=file_get_contents($file); |
|
| 558 | |||
| 559 | } else { |
||
| 560 | $this->set_error("[$file] does not exist"); |
||
| 561 | $file_text="<b>__XTemplate fatal error: file [$file] does not exist__</b>"; |
||
| 562 | } |
||
| 563 | |||
| 564 | 2 | return $file_text; |
|
| 565 | } |
||
| 566 | |||
| 567 | /***[ r_getfile ]***********************************************************/ |
||
| 568 | /* |
||
| 569 | recursively gets the content of a file with {FILE "filename.tpl"} directives |
||
| 570 | */ |
||
| 571 | |||
| 572 | |||
| 573 | 2 | function r_getfile($file) { |
|
| 574 | 2 | $text=$this->getfile($file); |
|
| 575 | 2 | while (preg_match($this->file_delim,$text,$res)) { |
|
| 576 | $text2=$this->getfile($res[1]); |
||
| 577 | $text=str_replace($res[0], $text2, $text); |
||
| 578 | } |
||
| 579 | 2 | return $text; |
|
| 580 | } |
||
| 581 | |||
| 582 | } /* end of XTemplate class. */ |
||
| 583 | |||
| 585 |
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.