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 FPDF_TPL 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 FPDF_TPL, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class FPDF_TPL extends FPDF { |
||
| 21 | /** |
||
| 22 | * Array of Tpl-Data |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | var $tpls = array(); |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Current Template-ID |
||
| 29 | * @var int |
||
| 30 | */ |
||
| 31 | var $tpl = 0; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * "In Template"-Flag |
||
| 35 | * @var boolean |
||
| 36 | */ |
||
| 37 | var $_intpl = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Nameprefix of Templates used in Resources-Dictonary |
||
| 41 | * @var string A String defining the Prefix used as Template-Object-Names. Have to beginn with an / |
||
| 42 | */ |
||
| 43 | var $tplprefix = "/TPL"; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Resources used By Templates and Pages |
||
| 47 | * @var array |
||
| 48 | */ |
||
| 49 | var $_res = array(); |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Last used Template data |
||
| 53 | * |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | var $lastUsedTemplateData = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Start a Template |
||
| 60 | * |
||
| 61 | * This method starts a template. You can give own coordinates to build an own sized |
||
| 62 | * Template. Pay attention, that the margins are adapted to the new templatesize. |
||
| 63 | * If you want to write outside the template, for example to build a clipped Template, |
||
| 64 | * you have to set the Margins and "Cursor"-Position manual after beginTemplate-Call. |
||
| 65 | * |
||
| 66 | * If no parameter is given, the template uses the current page-size. |
||
| 67 | * The Method returns an ID of the current Template. This ID is used later for using this template. |
||
| 68 | * Warning: A created Template is used in PDF at all events. Still if you don't use it after creation! |
||
| 69 | * |
||
| 70 | * @param int $x The x-coordinate given in user-unit |
||
| 71 | * @param int $y The y-coordinate given in user-unit |
||
| 72 | * @param int $w The width given in user-unit |
||
| 73 | * @param int $h The height given in user-unit |
||
| 74 | * @return int The ID of new created Template |
||
| 75 | */ |
||
| 76 | function beginTemplate($x = null, $y = null, $w = null, $h = null) { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * End Template |
||
| 140 | * |
||
| 141 | * This method ends a template and reset initiated variables on beginTemplate. |
||
| 142 | * |
||
| 143 | * @return mixed If a template is opened, the ID is returned. If not a false is returned. |
||
| 144 | */ |
||
| 145 | function endTemplate() { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Use a Template in current Page or other Template |
||
| 179 | * |
||
| 180 | * You can use a template in a page or in another template. |
||
| 181 | * You can give the used template a new size like you use the Image()-method. |
||
| 182 | * All parameters are optional. The width or height is calculated automaticaly |
||
| 183 | * if one is given. If no parameter is given the origin size as defined in |
||
| 184 | * beginTemplate() is used. |
||
| 185 | * The calculated or used width and height are returned as an array. |
||
| 186 | * |
||
| 187 | * @param int $tplidx A valid template-Id |
||
| 188 | * @param int $_x The x-position |
||
| 189 | * @param int $_y The y-position |
||
| 190 | * @param int $_w The new width of the template |
||
| 191 | * @param int $_h The new height of the template |
||
| 192 | * @retrun array The height and width of the template |
||
| 193 | */ |
||
| 194 | function useTemplate($tplidx, $_x = null, $_y = null, $_w = 0, $_h = 0) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get The calculated Size of a Template |
||
| 243 | * |
||
| 244 | * If one size is given, this method calculates the other one. |
||
| 245 | * |
||
| 246 | * @param int $tplidx A valid template-Id |
||
| 247 | * @param int $_w The width of the template |
||
| 248 | * @param int $_h The height of the template |
||
| 249 | * @return array The height and width of the template |
||
| 250 | */ |
||
| 251 | function getTemplateSize($tplidx, $_w = 0, $_h = 0) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * See FPDF/TCPDF-Documentation ;-) |
||
| 274 | */ |
||
| 275 | public function SetFont($family, $style = '', $size = 0, $fontfile='', $subset='default', $out=true) { |
||
| 291 | |||
| 292 | /** |
||
| 293 | * See FPDF/TCPDF-Documentation ;-) |
||
| 294 | */ |
||
| 295 | function Image( |
||
| 314 | |||
| 315 | /** |
||
| 316 | * See FPDF-Documentation ;-) |
||
| 317 | * |
||
| 318 | * AddPage is not available when you're "in" a template. |
||
| 319 | */ |
||
| 320 | function AddPage($orientation = '', $format = '', $keepmargins = false, $tocpage = false) { |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Preserve adding Links in Templates ...won't work |
||
| 334 | */ |
||
| 335 | View Code Duplication | function Link($x, $y, $w, $h, $link, $spaces = 0) { |
|
| 346 | |||
| 347 | function AddLink() { |
||
| 357 | |||
| 358 | View Code Duplication | function SetLink($link, $y = 0, $page = -1) { |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Private Method that writes the form xobjects |
||
| 371 | */ |
||
| 372 | function _putformxobjects() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Overwritten to add _putformxobjects() after _putimages() |
||
| 433 | * |
||
| 434 | */ |
||
| 435 | function _putimages() { |
||
| 436 | parent::_putimages(); |
||
| 437 | $this->_putformxobjects(); |
||
| 438 | } |
||
| 439 | |||
| 440 | function _putxobjectdict() { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Private Method |
||
| 452 | */ |
||
| 453 | function _out($s) { |
||
| 460 | } |
||
| 461 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.