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_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 | * Start a Template |
||
53 | * |
||
54 | * This method starts a template. You can give own coordinates to build an own sized |
||
55 | * Template. Pay attention, that the margins are adapted to the new templatesize. |
||
56 | * If you want to write outside the template, for example to build a clipped Template, |
||
57 | * you have to set the Margins and "Cursor"-Position manual after beginTemplate-Call. |
||
58 | * |
||
59 | * If no parameter is given, the template uses the current page-size. |
||
60 | * The Method returns an ID of the current Template. This ID is used later for using this template. |
||
61 | * Warning: A created Template is used in PDF at all events. Still if you don't use it after creation! |
||
62 | * |
||
63 | * @param int $x The x-coordinate given in user-unit |
||
64 | * @param int $y The y-coordinate given in user-unit |
||
65 | * @param int $w The width given in user-unit |
||
66 | * @param int $h The height given in user-unit |
||
67 | * @return int The ID of new created Template |
||
68 | */ |
||
69 | function beginTemplate($x=null, $y=null, $w=null, $h=null) { |
||
114 | |||
115 | /** |
||
116 | * End Template |
||
117 | * |
||
118 | * This method ends a template and reset initiated variables on beginTemplate. |
||
119 | * |
||
120 | * @return mixed If a template is opened, the ID is returned. If not a false is returned. |
||
121 | */ |
||
122 | function endTemplate() { |
||
139 | |||
140 | /** |
||
141 | * Use a Template in current Page or other Template |
||
142 | * |
||
143 | * You can use a template in a page or in another template. |
||
144 | * You can give the used template a new size like you use the Image()-method. |
||
145 | * All parameters are optional. The width or height is calculated automaticaly |
||
146 | * if one is given. If no parameter is given the origin size as defined in |
||
147 | * beginTemplate() is used. |
||
148 | * The calculated or used width and height are returned as an array. |
||
149 | * |
||
150 | * @param int $tplidx A valid template-Id |
||
151 | * @param int $_x The x-position |
||
152 | * @param int $_y The y-position |
||
153 | * @param int $_w The new width of the template |
||
154 | * @param int $_h The new height of the template |
||
155 | * @retrun array The height and width of the template |
||
156 | */ |
||
157 | function useTemplate($tplidx, $_x=null, $_y=null, $_w=0, $_h=0) { |
||
187 | |||
188 | /** |
||
189 | * Get The calculated Size of a Template |
||
190 | * |
||
191 | * If one size is given, this method calculates the other one. |
||
192 | * |
||
193 | * @param int $tplidx A valid template-Id |
||
194 | * @param int $_w The width of the template |
||
195 | * @param int $_h The height of the template |
||
196 | * @return array The height and width of the template |
||
197 | */ |
||
198 | function getTemplateSize($tplidx, $_w=0, $_h=0) { |
||
218 | |||
219 | /** |
||
220 | * See FPDF-Documentation ;-) |
||
221 | */ |
||
222 | function SetFont($family, $style='', $size=0) { |
||
239 | |||
240 | /** |
||
241 | * See FPDF/TCPDF-Documentation ;-) |
||
242 | */ |
||
243 | function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link=''){ |
||
257 | |||
258 | /** |
||
259 | * See FPDF-Documentation ;-) |
||
260 | * |
||
261 | * AddPage is not available when you're "in" a template. |
||
262 | */ |
||
263 | function AddPage($orientation='', $size='', $rotation=0) { |
||
268 | |||
269 | /** |
||
270 | * Preserve adding Links in Templates ...won't work |
||
271 | */ |
||
272 | function Link($x, $y, $w, $h, $link) { |
||
277 | |||
278 | function AddLink() { |
||
283 | |||
284 | function SetLink($link, $y=0, $page=-1) { |
||
289 | |||
290 | /** |
||
291 | * Private Method that writes the form xobjects |
||
292 | */ |
||
293 | function _putformxobjects() { |
||
335 | |||
336 | /** |
||
337 | * Private Method |
||
338 | */ |
||
339 | View Code Duplication | function _putresources() { |
|
378 | |||
379 | function _putxobjectdict() { |
||
388 | |||
389 | /** |
||
390 | * Private Method |
||
391 | */ |
||
392 | function _out($s) { |
||
399 | } |
||
400 |
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.