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 HTML2PDF_myPdf 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 HTML2PDF_myPdf, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class HTML2PDF_myPdf extends TCPDF |
||
16 | { |
||
17 | protected $_footerParam = array(); |
||
18 | protected $_transf = array(); |
||
19 | protected $_myLastPageGroup = null; |
||
20 | protected $_myLastPageGroupNb = 0; |
||
21 | |||
22 | // used to make a radius with bezier : (4/3 * (sqrt(2) - 1)) |
||
23 | const MY_ARC = 0.5522847498; |
||
24 | |||
25 | // nb of segment to build a arc with bezier curv |
||
26 | const ARC_NB_SEGMENT = 8; |
||
27 | |||
28 | /** |
||
29 | * class constructor |
||
30 | * |
||
31 | * @param string $orientation page orientation, same as TCPDF |
||
32 | * @param string $unit User measure unit, same as TCPDF |
||
33 | * @param mixed $format The format used for pages, same as TCPDF |
||
34 | * @param boolean $unicode TRUE means that the input text is unicode (default = true) |
||
35 | * @param String $encoding charset encoding; default is UTF-8 |
||
36 | * @param boolean $diskcache if TRUE reduce the RAM memory usage by caching temporary data on filesystem (slower). |
||
37 | * @access public |
||
38 | */ |
||
39 | public function __construct( |
||
62 | |||
63 | /** |
||
64 | * Set the parameters for the automatic footer |
||
65 | * |
||
66 | * @param boolean $page display the page number |
||
67 | * @param boolean $date display the date |
||
68 | * @param boolean $hour display the hour |
||
69 | * @param boolean $form display a warning abour forms |
||
70 | * @access public |
||
71 | */ |
||
72 | public function SetMyFooter($page = false, $date = false, $hour = false, $form = false) |
||
81 | |||
82 | /** |
||
83 | * This function is call automatically by TCPDF at the end of a page |
||
84 | * It takes no parameters |
||
85 | * |
||
86 | * @access public |
||
87 | */ |
||
88 | public function Footer() |
||
128 | |||
129 | /** |
||
130 | * after cloning a object, we does not want to clone all the front informations |
||
131 | * because it take a lot a time and a lot of memory => we use reference |
||
132 | * |
||
133 | * @param &HTML2PDF_myPdf object |
||
134 | * @param HTML2PDF_myPdf $pdf |
||
135 | * @access public |
||
136 | */ |
||
137 | public function cloneFontFrom(&$pdf) |
||
148 | |||
149 | /** |
||
150 | * multiple public accessor for some private attributs |
||
151 | * used only by cloneFontFrom |
||
152 | * |
||
153 | * @return &array |
||
154 | * @access public |
||
155 | */ |
||
156 | public function &getFonts() |
||
188 | |||
189 | /** |
||
190 | * Verify that a Font is already loaded |
||
191 | * |
||
192 | * @param string Font Key |
||
193 | * @return boolean |
||
194 | * @access public |
||
195 | */ |
||
196 | public function isLoadedFont($fontKey) |
||
208 | |||
209 | /** |
||
210 | * Get the Word Spacing |
||
211 | * |
||
212 | * @access public |
||
213 | * @return float word spacing |
||
214 | */ |
||
215 | public function getWordSpacing() |
||
219 | |||
220 | /** |
||
221 | * set the Word Spacing |
||
222 | * |
||
223 | * @param float word spacing |
||
224 | * @access public |
||
225 | */ |
||
226 | public function setWordSpacing($ws = 0.) |
||
231 | |||
232 | /** |
||
233 | * start to use a rectangular Cliping Path with radius corners |
||
234 | * |
||
235 | * @param float $x (top left corner) |
||
236 | * @param float $y (top left corner) |
||
237 | * @param float $w (x+w = botom rigth corner) |
||
238 | * @param float $h (y+h = botom rigth corner) |
||
239 | * @param array $cornerTL radius of the Top Left corner |
||
240 | * @param array $cornerTR radius of the Top Right corner |
||
241 | * @param array $cornerBL radius of the Bottom Left corner |
||
242 | * @param array $cornerBR radius of the Bottom Right corner |
||
243 | * @access public |
||
244 | */ |
||
245 | public function clippingPathStart( |
||
363 | |||
364 | /** |
||
365 | * stop to use the Cliping Path |
||
366 | * |
||
367 | * @access public |
||
368 | */ |
||
369 | public function clippingPathStop() |
||
373 | |||
374 | /** |
||
375 | * draw a filled corner of a border with a external and a internal radius |
||
376 | * /--------+ ext2 |
||
377 | * / | |
||
378 | * / /-------+ int2 |
||
379 | * / / |
||
380 | * | / |
||
381 | * | | |
||
382 | * | | |
||
383 | * ext1 +-+ int1 + cen |
||
384 | * |
||
385 | * @param float $ext1X |
||
386 | * @param float $ext1Y |
||
387 | * @param float $ext2X |
||
388 | * @param float $ext2Y |
||
389 | * @param float $int1X |
||
390 | * @param float $int1Y |
||
391 | * @param float $int2X |
||
392 | * @param float $int2Y |
||
393 | * @param float $cenX |
||
394 | * @param float $cenY |
||
395 | * @access public |
||
396 | */ |
||
397 | public function drawCurve($ext1X, $ext1Y, $ext2X, $ext2Y, $int1X, $int1Y, $int2X, $int2Y, $cenX, $cenY) |
||
446 | |||
447 | /** |
||
448 | * draw a filled corner of a border with only a external radius |
||
449 | * /--+ ext2 |
||
450 | * / | |
||
451 | * / | |
||
452 | * / | |
||
453 | * | | |
||
454 | * | | |
||
455 | * | | |
||
456 | * ext1 +-----+ int + cen |
||
457 | * |
||
458 | * @param float $ext1X |
||
459 | * @param float $ext1Y |
||
460 | * @param float $ext2X |
||
461 | * @param float $ext2Y |
||
462 | * @param float $intX |
||
463 | * @param float $intY |
||
464 | * @param float $cenX |
||
465 | * @param float $cenY |
||
466 | * @access public |
||
467 | */ |
||
468 | public function drawCorner($ext1X, $ext1Y, $ext2X, $ext2Y, $intX, $intY, $cenX, $cenY) |
||
503 | |||
504 | /** |
||
505 | * Start a transformation |
||
506 | * |
||
507 | * @access public |
||
508 | */ |
||
509 | public function startTransform() |
||
513 | |||
514 | /** |
||
515 | * Stop a transformation |
||
516 | * |
||
517 | * @access public |
||
518 | */ |
||
519 | public function stopTransform() |
||
523 | |||
524 | /** |
||
525 | * add a Translate transformation |
||
526 | * |
||
527 | * @access public |
||
528 | */ |
||
529 | public function setTranslate($xT, $yT) |
||
542 | |||
543 | /** |
||
544 | * add a Rotate transformation |
||
545 | * |
||
546 | * @param float $angle |
||
547 | * @access public |
||
548 | */ |
||
549 | public function setRotation($angle, $xC = null, $yC = null) |
||
570 | |||
571 | /** |
||
572 | * we redifine the original SetX method, because we don't want the automatic treatment. |
||
573 | * It is HTML2PDF that make the treatment |
||
574 | * |
||
575 | * @param float $x |
||
576 | * @param boolean $rtloff NOT USED |
||
577 | * @access public |
||
578 | */ |
||
579 | public function SetX($x, $rtloff = false) |
||
583 | |||
584 | /** |
||
585 | * we redifine the original SetY method, because we don't want the automatic treatment. |
||
586 | * It is HTML2PDF that make the treatment |
||
587 | * |
||
588 | * @param float $y |
||
589 | * @param boolean $resetx Reset the X position |
||
590 | * @param boolean $rtloff NOT USED |
||
591 | * @access public |
||
592 | */ |
||
593 | public function SetY($y, $resetx = true, $rtloff = false) |
||
600 | |||
601 | /** |
||
602 | * we redifine the original SetXY method, because we don't want the automatic treatment. |
||
603 | * It is HTML2PDF that make the treatment |
||
604 | * |
||
605 | * @param integer $x |
||
606 | * @param integer $y |
||
607 | * @param boolean $rtloff NOT USED |
||
608 | * @access public |
||
609 | */ |
||
610 | public function SetXY($x, $y, $rtloff = false) |
||
615 | |||
616 | /** |
||
617 | * multiple public accessor because HTML2PDF need to use TCPDF without being a extend of it |
||
618 | * |
||
619 | * @param mixed |
||
620 | * @return mixed |
||
621 | * @access public |
||
622 | */ |
||
623 | public function getK() |
||
627 | |||
628 | /** |
||
629 | * @return double |
||
630 | */ |
||
631 | public function getW() |
||
635 | |||
636 | /** |
||
637 | * @return double |
||
638 | */ |
||
639 | public function getH() |
||
647 | |||
648 | /** |
||
649 | * @return double |
||
650 | */ |
||
651 | public function getrMargin() |
||
655 | |||
656 | /** |
||
657 | * @return double |
||
658 | */ |
||
659 | public function gettMargin() |
||
671 | |||
672 | /** |
||
673 | * SVG - Convert a SVG Style in PDF Style |
||
674 | * |
||
675 | * @param array $styles SVG Style |
||
676 | * @return string PDF style |
||
677 | * @access public |
||
678 | */ |
||
679 | public function svgSetStyle($styles) |
||
704 | |||
705 | /** |
||
706 | * SVG - make a Rectangle |
||
707 | * |
||
708 | * @param float $x |
||
709 | * @param float $y |
||
710 | * @param float $w |
||
711 | * @param float $h |
||
712 | * @param string $style PDF Style |
||
713 | * @access public |
||
714 | */ |
||
715 | public function svgRect($x, $y, $w, $h, $style) |
||
741 | |||
742 | /** |
||
743 | * SVG - make a Line |
||
744 | * |
||
745 | * @param float $x1 |
||
746 | * @param float $y1 |
||
747 | * @param float $x2 |
||
748 | * @param float $y2 |
||
749 | * @access public |
||
750 | */ |
||
751 | public function svgLine($x1, $y1, $x2, $y2) |
||
761 | |||
762 | /** |
||
763 | * SVG - make a Ellipse |
||
764 | * |
||
765 | * @param float $x0 x Center |
||
766 | * @param float $y0 y Center |
||
767 | * @param float $rx x radius |
||
768 | * @param float $ry y radius |
||
769 | * @param string $style PDF Style |
||
770 | * @access public |
||
771 | */ |
||
772 | public function svgEllipse($x0, $y0, $rx, $ry, $style) |
||
783 | |||
784 | /** |
||
785 | * SVG - make a Advanced Polygone |
||
786 | * |
||
787 | * @param array $actions list of actions |
||
788 | * @param string $style PDF Style |
||
789 | * @access public |
||
790 | */ |
||
791 | public function svgPolygone($actions, $style) |
||
924 | |||
925 | /** |
||
926 | * SVG - go to a point |
||
927 | * |
||
928 | * @param float $x |
||
929 | * @param float $y |
||
930 | * @param boolean $trans apply transformation |
||
931 | * @access protected |
||
932 | */ |
||
933 | protected function _Point($x, $y, $trans = false) |
||
941 | |||
942 | /** |
||
943 | * SVG - make a line from the last point to (x,y) |
||
944 | * |
||
945 | * @param float $x |
||
946 | * @param float $y |
||
947 | * @param boolean $trans apply transformation |
||
948 | * @access protected |
||
949 | */ |
||
950 | protected function _Line($x, $y, $trans = false) |
||
958 | |||
959 | /** |
||
960 | * SVG - make a bezier curve from the last point to (xf,yf), with the 2 direction points (x1,y1) and (x2,y2) |
||
961 | * |
||
962 | * @param float $x1 |
||
963 | * @param float $y1 |
||
964 | * @param float $x2 |
||
965 | * @param float $y2 |
||
966 | * @param float $xf |
||
967 | * @param float $yf |
||
968 | * @param boolean $trans apply transformation |
||
969 | * @access protected |
||
970 | */ |
||
971 | protected function _Curve($x1, $y1, $x2, $y2, $xf, $yf, $trans = false) |
||
980 | |||
981 | /** |
||
982 | * SVG - make a arc with Center, Radius, from angleBegin to angleEnd |
||
983 | * |
||
984 | * @param float $xc |
||
985 | * @param float $yc |
||
986 | * @param float $rx |
||
987 | * @param float $ry |
||
988 | * @param float $angleBegin in radians |
||
989 | * @param float $angleEnd in radians |
||
990 | * @param boolean $direction |
||
991 | * @param boolean $drawFirst, true => add the first point |
||
992 | * @param boolean $trans apply transformation |
||
993 | * @access protected |
||
994 | */ |
||
995 | protected function _Arc( |
||
1052 | |||
1053 | /** |
||
1054 | * SVG - make a arc from Pt1 to Pt2, with Radius |
||
1055 | * |
||
1056 | * @param float $x1 |
||
1057 | * @param float $y1 |
||
1058 | * @param float $x2 |
||
1059 | * @param float $y2 |
||
1060 | * @param float $rx |
||
1061 | * @param float $ry |
||
1062 | * @param float $angle deviation angle of the axis X |
||
1063 | * @param integer $l large-arc-flag |
||
1064 | * @param integer $s sweep-flag |
||
1065 | * @param boolean $trans apply transformation |
||
1066 | * @access protected |
||
1067 | */ |
||
1068 | protected function _Arc2($x1, $y1, $x2, $y2, $rx, $ry, $angle = 0., $l = 0, $s = 0, $trans = false) |
||
1157 | |||
1158 | /** |
||
1159 | * SVG - transform the point (reference) |
||
1160 | * |
||
1161 | * @param float &$x |
||
1162 | * @param float &$y |
||
1163 | * @param boolean $trans true => convert into PDF unit |
||
1164 | * @param double $x |
||
1165 | * @param double $y |
||
1166 | * @return boolean |
||
1167 | * @access public |
||
1168 | */ |
||
1169 | public function ptTransform(&$x, &$y, $trans = true) |
||
1187 | |||
1188 | /** |
||
1189 | * SVG - add a transformation Matric |
||
1190 | * |
||
1191 | * @param array $n matrix |
||
1192 | * @access public |
||
1193 | */ |
||
1194 | public function doTransform($n = null) |
||
1214 | |||
1215 | /** |
||
1216 | * SVG - remove a transformation Matric |
||
1217 | * |
||
1218 | * @access public |
||
1219 | */ |
||
1220 | public function undoTransform() |
||
1224 | |||
1225 | /** |
||
1226 | * Convert a HTML2PDF barcode in a TCPDF barcode |
||
1227 | * |
||
1228 | * @param string $code code to print |
||
1229 | * @param string $type type of barcode (see tcpdf/barcodes.php for supported formats) |
||
1230 | * @param int $x x position in user units |
||
1231 | * @param int $y y position in user units |
||
1232 | * @param int $w width in user units |
||
1233 | * @param int $h height in user units |
||
1234 | * @param int $labelFontsize of the Test Label. If false : no Label |
||
1235 | * @param array $color color of the foreground |
||
1236 | * @access public |
||
1237 | */ |
||
1238 | public function myBarcode($code, $type, $x, $y, $w, $h, $labelFontsize, $color) |
||
1257 | |||
1258 | /** |
||
1259 | * create a automatic Index on a page |
||
1260 | * |
||
1261 | * @param html2pdf $obj parent object |
||
1262 | * @param string $titre Title of the Index Page |
||
1263 | * @param integer $sizeTitle Font size for hthe Title |
||
1264 | * @param integer $sizeBookmark Font size for the bookmarks |
||
1265 | * @param boolean $bookmarkTitle Bookmark the Title |
||
1266 | * @param boolean $displayPage Display the page number for each bookmark |
||
1267 | * @param integer $page draw the automatic Index on a specific Page. if null => add a page at the end |
||
1268 | * @param string $fontName FontName to use |
||
1269 | * @access public |
||
1270 | */ |
||
1271 | public function createIndex( |
||
1340 | |||
1341 | /** |
||
1342 | * Returns the string alias used for the total number of pages. |
||
1343 | * |
||
1344 | * @access public |
||
1345 | * @return string; |
||
1346 | * @see TCPDF::getAliasNbPages(), TCPDF::getPageGroupAlias() |
||
1347 | */ |
||
1348 | public function getMyAliasNbPages() |
||
1361 | |||
1362 | /** |
||
1363 | * Returns the current page number. |
||
1364 | * |
||
1365 | * @access public |
||
1366 | * @param integer $page |
||
1367 | * @return integer; |
||
1368 | */ |
||
1369 | public function getMyNumPage($page = null) |
||
1381 | |||
1382 | /** |
||
1383 | * Start a new group of pages |
||
1384 | * |
||
1385 | * @access public |
||
1386 | * @return integer; |
||
1387 | * @see tcpdf::startPageGroup |
||
1388 | */ |
||
1389 | public function myStartPageGroup() |
||
1394 | |||
1395 | /** |
||
1396 | * get $_myLastPageGroup; |
||
1397 | * |
||
1398 | * @access public |
||
1399 | * @return integer $_myLastPageGroup; |
||
1400 | */ |
||
1401 | public function getMyLastPageGroup() |
||
1405 | |||
1406 | /** |
||
1407 | * set $_myLastPageGroup; |
||
1408 | * |
||
1409 | * @access public |
||
1410 | * @param integer $myLastPageGroup; |
||
1411 | * @param integer $myLastPageGroup |
||
1412 | */ |
||
1413 | public function setMyLastPageGroup($myLastPageGroup) |
||
1417 | |||
1418 | /** |
||
1419 | * get $_myLastPageGroupNb; |
||
1420 | * |
||
1421 | * @access public |
||
1422 | * @return integer $_myLastPageGroupNb; |
||
1423 | */ |
||
1424 | public function getMyLastPageGroupNb() |
||
1428 | |||
1429 | /** |
||
1430 | * set $_myLastPageGroupNb; |
||
1431 | * |
||
1432 | * @access public |
||
1433 | * @param integer $myLastPageGroupNb; |
||
1434 | * @param integer $myLastPageGroupNb |
||
1435 | */ |
||
1436 | public function setMyLastPageGroupNb($myLastPageGroupNb) |
||
1440 | } |
||
1441 |
This check looks for a call to a parent method whose name is different than the method from which it is called.
Consider the following code:
The
getFirstName()
method in theSon
calls the wrong method in the parent class.