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) { |
||
77 | if (is_subclass_of($this, 'TCPDF')) { |
||
78 | $this->Error('This method is only usable with FPDF. Use TCPDF methods startTemplate() instead.'); |
||
79 | return; |
||
80 | } |
||
81 | |||
82 | if ($this->page <= 0) |
||
83 | $this->error("You have to add a page to fpdf first!"); |
||
84 | |||
85 | if ($x == null) |
||
86 | $x = 0; |
||
87 | if ($y == null) |
||
88 | $y = 0; |
||
89 | if ($w == null) |
||
90 | $w = $this->w; |
||
91 | if ($h == null) |
||
92 | $h = $this->h; |
||
93 | |||
94 | // Save settings |
||
95 | $this->tpl++; |
||
96 | $tpl =& $this->tpls[$this->tpl]; |
||
97 | $tpl = array( |
||
98 | 'o_x' => $this->x, |
||
99 | 'o_y' => $this->y, |
||
100 | 'o_AutoPageBreak' => $this->AutoPageBreak, |
||
101 | 'o_bMargin' => $this->bMargin, |
||
102 | 'o_tMargin' => $this->tMargin, |
||
103 | 'o_lMargin' => $this->lMargin, |
||
104 | 'o_rMargin' => $this->rMargin, |
||
105 | 'o_h' => $this->h, |
||
106 | 'o_w' => $this->w, |
||
107 | 'o_FontFamily' => $this->FontFamily, |
||
108 | 'o_FontStyle' => $this->FontStyle, |
||
109 | 'o_FontSizePt' => $this->FontSizePt, |
||
110 | 'o_FontSize' => $this->FontSize, |
||
111 | 'buffer' => '', |
||
112 | 'x' => $x, |
||
113 | 'y' => $y, |
||
114 | 'w' => $w, |
||
115 | 'h' => $h |
||
116 | ); |
||
117 | |||
118 | $this->SetAutoPageBreak(false); |
||
119 | |||
120 | // Define own high and width to calculate possitions correct |
||
121 | $this->h = $h; |
||
122 | $this->w = $w; |
||
123 | |||
124 | $this->_intpl = true; |
||
125 | $this->SetXY($x + $this->lMargin, $y + $this->tMargin); |
||
126 | $this->SetRightMargin($this->w - $w + $this->rMargin); |
||
127 | |||
128 | if ($this->CurrentFont) { |
||
129 | $fontkey = $this->FontFamily . $this->FontStyle; |
||
130 | $this->_res['tpl'][$this->tpl]['fonts'][$fontkey] =& $this->fonts[$fontkey]; |
||
131 | |||
132 | $this->_out(sprintf('BT /F%d %.2f Tf ET', $this->CurrentFont['i'], $this->FontSizePt)); |
||
133 | } |
||
134 | |||
135 | return $this->tpl; |
||
136 | } |
||
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() { |
||
146 | if (is_subclass_of($this, 'TCPDF')) { |
||
147 | $args = func_get_args(); |
||
148 | return call_user_func_array(array($this, 'TCPDF::endTemplate'), $args); |
||
149 | } |
||
150 | |||
151 | if ($this->_intpl) { |
||
152 | $this->_intpl = false; |
||
153 | $tpl =& $this->tpls[$this->tpl]; |
||
154 | $this->SetXY($tpl['o_x'], $tpl['o_y']); |
||
155 | $this->tMargin = $tpl['o_tMargin']; |
||
156 | $this->lMargin = $tpl['o_lMargin']; |
||
157 | $this->rMargin = $tpl['o_rMargin']; |
||
158 | $this->h = $tpl['o_h']; |
||
159 | $this->w = $tpl['o_w']; |
||
160 | $this->SetAutoPageBreak($tpl['o_AutoPageBreak'], $tpl['o_bMargin']); |
||
161 | |||
162 | $this->FontFamily = $tpl['o_FontFamily']; |
||
163 | $this->FontStyle = $tpl['o_FontStyle']; |
||
164 | $this->FontSizePt = $tpl['o_FontSizePt']; |
||
165 | $this->FontSize = $tpl['o_FontSize']; |
||
166 | |||
167 | $fontkey = $this->FontFamily . $this->FontStyle; |
||
168 | if ($fontkey) |
||
169 | $this->CurrentFont =& $this->fonts[$fontkey]; |
||
170 | |||
171 | return $this->tpl; |
||
172 | } else { |
||
173 | return false; |
||
174 | } |
||
175 | } |
||
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) { |
||
195 | if ($this->page <= 0) |
||
196 | $this->error('You have to add a page first!'); |
||
197 | |||
198 | if (!isset($this->tpls[$tplidx])) |
||
199 | $this->error('Template does not exist!'); |
||
200 | |||
201 | if ($this->_intpl) { |
||
202 | $this->_res['tpl'][$this->tpl]['tpls'][$tplidx] =& $this->tpls[$tplidx]; |
||
203 | } |
||
204 | |||
205 | $tpl =& $this->tpls[$tplidx]; |
||
206 | $w = $tpl['w']; |
||
207 | $h = $tpl['h']; |
||
208 | |||
209 | if ($_x == null) |
||
210 | $_x = 0; |
||
211 | if ($_y == null) |
||
212 | $_y = 0; |
||
213 | |||
214 | $_x += $tpl['x']; |
||
215 | $_y += $tpl['y']; |
||
216 | |||
217 | $wh = $this->getTemplateSize($tplidx, $_w, $_h); |
||
218 | $_w = $wh['w']; |
||
219 | $_h = $wh['h']; |
||
220 | |||
221 | $tData = array( |
||
222 | 'x' => $this->x, |
||
223 | 'y' => $this->y, |
||
224 | 'w' => $_w, |
||
225 | 'h' => $_h, |
||
226 | 'scaleX' => ($_w / $w), |
||
227 | 'scaleY' => ($_h / $h), |
||
228 | 'tx' => $_x, |
||
229 | 'ty' => ($this->h - $_y - $_h), |
||
230 | 'lty' => ($this->h - $_y - $_h) - ($this->h - $h) * ($_h / $h) |
||
231 | ); |
||
232 | |||
233 | $this->_out(sprintf('q %.4F 0 0 %.4F %.4F %.4F cm', $tData['scaleX'], $tData['scaleY'], $tData['tx'] * $this->k, $tData['ty'] * $this->k)); // Translate |
||
234 | $this->_out(sprintf('%s%d Do Q', $this->tplprefix, $tplidx)); |
||
235 | |||
236 | $this->lastUsedTemplateData = $tData; |
||
237 | |||
238 | return array('w' => $_w, 'h' => $_h); |
||
239 | } |
||
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) { |
||
252 | if (!isset($this->tpls[$tplidx])) |
||
253 | return false; |
||
254 | |||
255 | $tpl =& $this->tpls[$tplidx]; |
||
256 | $w = $tpl['w']; |
||
257 | $h = $tpl['h']; |
||
258 | |||
259 | if ($_w == 0 and $_h == 0) { |
||
260 | $_w = $w; |
||
261 | $_h = $h; |
||
262 | } |
||
263 | |||
264 | if($_w == 0) |
||
265 | $_w = $_h * $w / $h; |
||
266 | if($_h == 0) |
||
267 | $_h = $_w * $h / $w; |
||
268 | |||
269 | return array("w" => $_w, "h" => $_h); |
||
270 | } |
||
271 | |||
272 | /** |
||
273 | * See FPDF/TCPDF-Documentation ;-) |
||
274 | */ |
||
275 | public function SetFont($family, $style = '', $size = 0, $fontfile='', $subset='default', $out=true) { |
||
276 | if (is_subclass_of($this, 'TCPDF')) { |
||
277 | $args = func_get_args(); |
||
278 | return call_user_func_array(array($this, 'TCPDF::SetFont'), $args); |
||
279 | } |
||
280 | |||
281 | parent::SetFont($family, $style, $size); |
||
282 | |||
283 | $fontkey = $this->FontFamily . $this->FontStyle; |
||
284 | |||
285 | if ($this->_intpl) { |
||
286 | $this->_res['tpl'][$this->tpl]['fonts'][$fontkey] =& $this->fonts[$fontkey]; |
||
287 | } else { |
||
288 | $this->_res['page'][$this->page]['fonts'][$fontkey] =& $this->fonts[$fontkey]; |
||
289 | } |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * See FPDF/TCPDF-Documentation ;-) |
||
294 | */ |
||
295 | function Image( |
||
296 | $file, $x = '', $y = '', $w = 0, $h = 0, $type = '', $link = '', $align = '', $resize = false, |
||
297 | $dpi = 300, $palign = '', $ismask = false, $imgmask = false, $border = 0, $fitbox = false, |
||
298 | $hidden = false, $fitonpage = false, $alt = false, $altimgs = array() |
||
299 | ) { |
||
300 | if (is_subclass_of($this, 'TCPDF')) { |
||
301 | $args = func_get_args(); |
||
302 | return call_user_func_array(array($this, 'TCPDF::Image'), $args); |
||
303 | } |
||
304 | |||
305 | $ret = parent::Image($file, $x, $y, $w, $h, $type, $link); |
||
306 | if ($this->_intpl) { |
||
307 | $this->_res['tpl'][$this->tpl]['images'][$file] =& $this->images[$file]; |
||
308 | } else { |
||
309 | $this->_res['page'][$this->page]['images'][$file] =& $this->images[$file]; |
||
310 | } |
||
311 | |||
312 | return $ret; |
||
313 | } |
||
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) { |
||
321 | if (is_subclass_of($this, 'TCPDF')) { |
||
322 | $args = func_get_args(); |
||
323 | return call_user_func_array(array($this, 'TCPDF::AddPage'), $args); |
||
324 | } |
||
325 | |||
326 | if ($this->_intpl) |
||
327 | $this->Error('Adding pages in templates isn\'t possible!'); |
||
328 | |||
329 | parent::AddPage($orientation, $format); |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * Preserve adding Links in Templates ...won't work |
||
334 | */ |
||
335 | function Link($x, $y, $w, $h, $link, $spaces = 0) { |
||
336 | if (is_subclass_of($this, 'TCPDF')) { |
||
337 | $args = func_get_args(); |
||
338 | return call_user_func_array(array($this, 'TCPDF::Link'), $args); |
||
339 | } |
||
340 | |||
341 | if ($this->_intpl) |
||
342 | $this->Error('Using links in templates aren\'t possible!'); |
||
343 | |||
344 | parent::Link($x, $y, $w, $h, $link); |
||
345 | } |
||
346 | |||
347 | function AddLink() { |
||
348 | if (is_subclass_of($this, 'TCPDF')) { |
||
349 | $args = func_get_args(); |
||
350 | return call_user_func_array(array($this, 'TCPDF::AddLink'), $args); |
||
351 | } |
||
352 | |||
353 | if ($this->_intpl) |
||
354 | $this->Error('Adding links in templates aren\'t possible!'); |
||
355 | return parent::AddLink(); |
||
356 | } |
||
357 | |||
358 | function SetLink($link, $y = 0, $page = -1) { |
||
359 | if (is_subclass_of($this, 'TCPDF')) { |
||
360 | $args = func_get_args(); |
||
361 | return call_user_func_array(array($this, 'TCPDF::SetLink'), $args); |
||
362 | } |
||
363 | |||
364 | if ($this->_intpl) |
||
365 | $this->Error('Setting links in templates aren\'t possible!'); |
||
366 | parent::SetLink($link, $y, $page); |
||
367 | } |
||
368 | |||
369 | /** |
||
370 | * Private Method that writes the form xobjects |
||
371 | */ |
||
372 | function _putformxobjects() { |
||
373 | $filter=($this->compress) ? '/Filter /FlateDecode ' : ''; |
||
374 | reset($this->tpls); |
||
375 | foreach($this->tpls AS $tplidx => $tpl) { |
||
376 | |||
377 | $p=($this->compress) ? gzcompress($tpl['buffer']) : $tpl['buffer']; |
||
378 | $this->_newobj(); |
||
379 | $this->tpls[$tplidx]['n'] = $this->n; |
||
380 | $this->_out('<<'.$filter.'/Type /XObject'); |
||
381 | $this->_out('/Subtype /Form'); |
||
382 | $this->_out('/FormType 1'); |
||
383 | $this->_out(sprintf('/BBox [%.2F %.2F %.2F %.2F]', |
||
384 | // llx |
||
385 | $tpl['x'] * $this->k, |
||
386 | // lly |
||
387 | -$tpl['y'] * $this->k, |
||
388 | // urx |
||
389 | ($tpl['w'] + $tpl['x']) * $this->k, |
||
390 | // ury |
||
391 | ($tpl['h'] - $tpl['y']) * $this->k |
||
392 | )); |
||
393 | |||
394 | if ($tpl['x'] != 0 || $tpl['y'] != 0) { |
||
395 | $this->_out(sprintf('/Matrix [1 0 0 1 %.5F %.5F]', |
||
396 | -$tpl['x'] * $this->k * 2, $tpl['y'] * $this->k * 2 |
||
397 | )); |
||
398 | } |
||
399 | |||
400 | $this->_out('/Resources '); |
||
401 | |||
402 | $this->_out('<</ProcSet [/PDF /Text /ImageB /ImageC /ImageI]'); |
||
403 | if (isset($this->_res['tpl'][$tplidx]['fonts']) && count($this->_res['tpl'][$tplidx]['fonts'])) { |
||
404 | $this->_out('/Font <<'); |
||
405 | foreach($this->_res['tpl'][$tplidx]['fonts'] as $font) |
||
406 | $this->_out('/F' . $font['i'] . ' ' . $font['n'] . ' 0 R'); |
||
407 | $this->_out('>>'); |
||
408 | } |
||
409 | if(isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images']) || |
||
410 | isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) |
||
411 | { |
||
412 | $this->_out('/XObject <<'); |
||
413 | if (isset($this->_res['tpl'][$tplidx]['images']) && count($this->_res['tpl'][$tplidx]['images'])) { |
||
414 | foreach($this->_res['tpl'][$tplidx]['images'] as $image) |
||
415 | $this->_out('/I' . $image['i'] . ' ' . $image['n'] . ' 0 R'); |
||
416 | } |
||
417 | if (isset($this->_res['tpl'][$tplidx]['tpls']) && count($this->_res['tpl'][$tplidx]['tpls'])) { |
||
418 | foreach($this->_res['tpl'][$tplidx]['tpls'] as $i => $tpl) |
||
419 | $this->_out($this->tplprefix . $i . ' ' . $tpl['n'] . ' 0 R'); |
||
420 | } |
||
421 | $this->_out('>>'); |
||
422 | } |
||
423 | $this->_out('>>'); |
||
424 | |||
425 | $this->_out('/Length ' . strlen($p) . ' >>'); |
||
426 | $this->_putstream($p); |
||
427 | $this->_out('endobj'); |
||
428 | } |
||
429 | } |
||
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() { |
||
441 | parent::_putxobjectdict(); |
||
442 | |||
443 | if (count($this->tpls)) { |
||
444 | foreach($this->tpls as $tplidx => $tpl) { |
||
445 | $this->_out(sprintf('%s%d %d 0 R', $this->tplprefix, $tplidx, $tpl['n'])); |
||
446 | } |
||
447 | } |
||
448 | } |
||
449 | |||
450 | /** |
||
451 | * Private Method |
||
452 | */ |
||
453 | function _out($s) { |
||
454 | if ($this->state == 2 && $this->_intpl) { |
||
455 | $this->tpls[$this->tpl]['buffer'] .= $s . "\n"; |
||
456 | } else { |
||
457 | parent::_out($s); |
||
458 | } |
||
459 | } |
||
460 | } |
||
461 |