Complex classes like Tcpdf 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 Tcpdf, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class Tcpdf extends PDF |
||
19 | { |
||
20 | const WATERMARK_TYPE_TEXT = 0; |
||
21 | const WATERMARK_TYPE_IMAGE = 1; |
||
22 | |||
23 | /** |
||
24 | * Are we fully configured or default params were given in constructor (not fully configured) ? |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | private $isDefault = true; |
||
29 | |||
30 | /** |
||
31 | * HTML content. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | public $html = ''; |
||
36 | |||
37 | /** |
||
38 | * Page format. |
||
39 | * |
||
40 | * @var string |
||
41 | */ |
||
42 | public $format = ''; |
||
43 | |||
44 | /** |
||
45 | * Page orientation. |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | public $pageOrientation = ['PLL_PORTRAIT' => 'P', 'PLL_LANDSCAPE' => 'L']; |
||
50 | |||
51 | /** |
||
52 | * Default margins. |
||
53 | * |
||
54 | * @var array |
||
55 | */ |
||
56 | public $defaultMargins = [ |
||
57 | 'left' => 15, |
||
58 | 'right' => 15, |
||
59 | 'top' => 16, |
||
60 | 'bottom' => 16 |
||
61 | ]; |
||
62 | |||
63 | /** |
||
64 | * Default font. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $defaultFontFamily = 'dejavusans'; |
||
69 | |||
70 | /** |
||
71 | * Default font size. |
||
72 | * |
||
73 | * @var int |
||
74 | */ |
||
75 | protected $defaultFontSize = 10; |
||
76 | |||
77 | /** |
||
78 | * @var \Vtiger_Module_Model |
||
79 | */ |
||
80 | protected $moduleModel; |
||
81 | |||
82 | /** |
||
83 | * Returns pdf library object. |
||
84 | */ |
||
85 | 1 | public function pdf() |
|
89 | |||
90 | /** |
||
91 | * Constructor. |
||
92 | */ |
||
93 | 1 | public function __construct($mode = '', $format = 'A4', $defaultFontSize = 10, $defaultFont = 'dejavusans', $orientation = 'P', $leftMargin = 15, $rightMargin = 15, $topMargin = 16, $bottomMargin = 16, $headerMargin = 9, $footerMargin = 9) |
|
106 | |||
107 | /** |
||
108 | * Initialize pdf file params. |
||
109 | * |
||
110 | * @param string $mode |
||
111 | * @param string $format |
||
112 | * @param int $defaultFontSize |
||
113 | * @param string $defaultFont |
||
114 | * @param string $orientation |
||
115 | * @param int $leftMargin |
||
116 | * @param int $rightMargin |
||
117 | * @param int $topMargin |
||
118 | * @param int $bottomMargin |
||
119 | * @param int $headerMargin |
||
120 | * @param int $footerMargin |
||
121 | */ |
||
122 | 1 | public function initializePdf($mode = '', $format = 'A4', $defaultFontSize = 10, $defaultFont = 'dejavusans', $orientation = 'P', $leftMargin = 15, $rightMargin = 15, $topMargin = 16, $bottomMargin = 16, $headerMargin = 9, $footerMargin = 9) |
|
142 | |||
143 | /** |
||
144 | * Returns bank name. |
||
145 | */ |
||
146 | public function getLibraryName() |
||
150 | |||
151 | /** |
||
152 | * Sets library name. |
||
153 | */ |
||
154 | 1 | public function setLibraryName($name) |
|
158 | |||
159 | /** |
||
160 | * Returns template id. |
||
161 | */ |
||
162 | public function getTemplateId() |
||
166 | |||
167 | /** |
||
168 | * Sets the template id. |
||
169 | */ |
||
170 | 1 | public function setTemplateId($id) |
|
174 | |||
175 | /** |
||
176 | * Returns record id. |
||
177 | */ |
||
178 | public function getRecordId() |
||
182 | |||
183 | /** |
||
184 | * Sets the record id. |
||
185 | */ |
||
186 | 1 | public function setRecordId($id) |
|
190 | |||
191 | /** |
||
192 | * Returns module name. |
||
193 | */ |
||
194 | public function getModuleName() |
||
198 | |||
199 | /** |
||
200 | * Sets module name. |
||
201 | */ |
||
202 | 1 | public function setModuleName($name) |
|
208 | |||
209 | /** |
||
210 | * Set top margin. |
||
211 | */ |
||
212 | 1 | public function setTopMargin($margin) |
|
216 | |||
217 | /** |
||
218 | * Set bottom margin. |
||
219 | */ |
||
220 | 1 | public function setBottomMargin($margin) |
|
224 | |||
225 | /** |
||
226 | * Set left margin. |
||
227 | */ |
||
228 | 1 | public function setLeftMargin($margin) |
|
232 | |||
233 | /** |
||
234 | * Set right margin. |
||
235 | */ |
||
236 | 1 | public function setRightMargin($margin) |
|
240 | |||
241 | /** |
||
242 | * Set page size and orientation. |
||
243 | * |
||
244 | * @param string|null $format - page format |
||
245 | * @param string $orientation - page orientation |
||
246 | */ |
||
247 | public function setPageSize($format, $orientation = null) |
||
251 | |||
252 | /** |
||
253 | * Set language. |
||
254 | * |
||
255 | * @param $language |
||
256 | */ |
||
257 | 1 | public function setLanguage($language) |
|
262 | |||
263 | /** |
||
264 | * Parse variables. |
||
265 | * |
||
266 | * @param string $str |
||
267 | * |
||
268 | * @return string |
||
269 | */ |
||
270 | 1 | public function parseVariables(string $str) |
|
280 | |||
281 | /** |
||
282 | * Parse and set options. |
||
283 | * |
||
284 | * @param array $params - array of parameters |
||
285 | * @param bool $defaultMargins - use default margins or custom user specified? |
||
286 | */ |
||
287 | 1 | public function parseParams(array $params, $defaultMargins = true) |
|
288 | { |
||
289 | 1 | if ($defaultMargins) { |
|
290 | 1 | $params = array_diff_key($params, ['margin-top', 'margin-bottom', 'margin-left', 'margin-right', 'header_height', 'footer_height']); |
|
291 | } |
||
292 | 1 | foreach ($params as $param => $value) { |
|
293 | 1 | switch ($param) { |
|
294 | 1 | case 'margin-top': |
|
295 | 1 | if (is_numeric($value)) { |
|
296 | 1 | $this->setTopMargin($value); |
|
297 | } else { |
||
298 | $this->setTopMargin($this->defaultMargins['top']); |
||
299 | } |
||
300 | 1 | break; |
|
301 | 1 | case 'margin-bottom': |
|
302 | 1 | if (is_numeric($value)) { |
|
303 | 1 | $this->setBottomMargin($value); |
|
304 | } else { |
||
305 | $this->setBottomMargin($this->defaultMargins['bottom']); |
||
306 | } |
||
307 | 1 | break; |
|
308 | 1 | case 'margin-left': |
|
309 | 1 | if (is_numeric($value)) { |
|
310 | 1 | $this->setLeftMargin($value); |
|
311 | } else { |
||
312 | $this->setLeftMargin($this->defaultMargins['left']); |
||
313 | } |
||
314 | 1 | break; |
|
315 | 1 | case 'margin-right': |
|
316 | 1 | if (is_numeric($value)) { |
|
317 | 1 | $this->setRightMargin($value); |
|
318 | } else { |
||
319 | $this->setRightMargin($this->defaultMargins['right']); |
||
320 | } |
||
321 | 1 | break; |
|
322 | 1 | case 'header_height': |
|
323 | 1 | if (is_numeric($value)) { |
|
324 | 1 | $this->pdf->setHeaderMargin($value); |
|
325 | } |
||
326 | 1 | break; |
|
327 | 1 | case 'footer_height': |
|
328 | 1 | if (is_numeric($value)) { |
|
329 | 1 | $this->pdf->setFooterMargin($value); |
|
330 | } |
||
331 | 1 | break; |
|
332 | 1 | case 'title': |
|
333 | 1 | $this->setTitle($value); |
|
334 | 1 | break; |
|
335 | 1 | case 'author': |
|
336 | 1 | $this->setAuthor($value); |
|
337 | 1 | break; |
|
338 | 1 | case 'creator': |
|
339 | 1 | $this->setCreator($value); |
|
340 | 1 | break; |
|
341 | 1 | case 'subject': |
|
342 | 1 | $this->setSubject($value); |
|
343 | 1 | break; |
|
344 | 1 | case 'keywords': |
|
345 | 1 | $this->setKeywords($value); |
|
346 | 1 | break; |
|
347 | default: |
||
348 | 1 | break; |
|
349 | } |
||
350 | } |
||
351 | 1 | } |
|
352 | |||
353 | // meta attributes |
||
354 | |||
355 | /** |
||
356 | * Set Title of the document. |
||
357 | */ |
||
358 | 1 | public function setTitle($title) |
|
362 | |||
363 | /** |
||
364 | * Set Title of the document. |
||
365 | */ |
||
366 | 1 | public function setAuthor($author) |
|
370 | |||
371 | /** |
||
372 | * Set Title of the document. |
||
373 | */ |
||
374 | 1 | public function setCreator($creator) |
|
378 | |||
379 | /** |
||
380 | * Set Title of the document. |
||
381 | */ |
||
382 | 1 | public function setSubject($subject) |
|
386 | |||
387 | /** |
||
388 | * Set Title of the document. |
||
389 | */ |
||
390 | 1 | public function setKeywords($keywords) |
|
394 | |||
395 | /** |
||
396 | * Set header content. |
||
397 | */ |
||
398 | public function setHeader($name, $header) |
||
402 | |||
403 | /** |
||
404 | * Set footer content. |
||
405 | */ |
||
406 | public function setFooter($name, $footer) |
||
410 | |||
411 | /** |
||
412 | * Write html. |
||
413 | */ |
||
414 | 1 | public function writeHTML() |
|
418 | |||
419 | /** |
||
420 | * Set watermark. |
||
421 | * |
||
422 | * @param $templateModel |
||
423 | */ |
||
424 | 1 | public function setWaterMark($templateModel) |
|
436 | |||
437 | /** |
||
438 | * Load html. |
||
439 | * |
||
440 | * @param string $html |
||
441 | */ |
||
442 | public function loadHTML($html) |
||
446 | |||
447 | /** |
||
448 | * Prepare pdf, generate all content. |
||
449 | * |
||
450 | * @param int $recordId |
||
451 | * @param string $moduleName |
||
452 | * @param int $templateId |
||
453 | * @param int $templateMainRecordId - optional if null $recordId is used |
||
454 | * |
||
455 | * @return Tcpdf current or new instance if needed |
||
456 | */ |
||
457 | 1 | public function generateContent($recordId, $moduleName, $templateId, $templateMainRecordId = null) |
|
491 | |||
492 | /** |
||
493 | * Output content to PDF. |
||
494 | * |
||
495 | * @param string $fileName |
||
496 | * @param string $dest |
||
497 | */ |
||
498 | 1 | public function output($fileName = '', $dest = '') |
|
507 | |||
508 | /** |
||
509 | * Export record to PDF file. |
||
510 | * |
||
511 | * @param int $recordId - id of a record |
||
512 | * @param string $moduleName - name of records module |
||
513 | * @param int $templateId - id of pdf template |
||
514 | * @param string $filePath - path name for saving pdf file |
||
515 | * @param string $saveFlag - save option flag |
||
516 | */ |
||
517 | 1 | public function export($recordId, $moduleName, $templateId, $filePath = '', $saveFlag = '') |
|
521 | } |
||
522 |