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 GD 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 GD, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class GD implements Canvas |
||
25 | { |
||
26 | /** |
||
27 | * @var Dompdf |
||
28 | */ |
||
29 | private $_dompdf; |
||
30 | |||
31 | /** |
||
32 | * Resource handle for the image |
||
33 | * |
||
34 | * @var resource |
||
35 | */ |
||
36 | private $_img; |
||
37 | |||
38 | /** |
||
39 | * Resource handle for the image |
||
40 | * |
||
41 | * @var resource[] |
||
42 | */ |
||
43 | private $_imgs; |
||
44 | |||
45 | /** |
||
46 | * Apparent canvas width in pixels |
||
47 | * |
||
48 | * @var int |
||
49 | */ |
||
50 | private $_width; |
||
51 | |||
52 | /** |
||
53 | * Apparent canvas height in pixels |
||
54 | * |
||
55 | * @var int |
||
56 | */ |
||
57 | private $_height; |
||
58 | |||
59 | /** |
||
60 | * Actual image width in pixels |
||
61 | * |
||
62 | * @var int |
||
63 | */ |
||
64 | private $_actual_width; |
||
65 | |||
66 | /** |
||
67 | * Actual image height in pixels |
||
68 | * |
||
69 | * @var int |
||
70 | */ |
||
71 | private $_actual_height; |
||
72 | |||
73 | /** |
||
74 | * Current page number |
||
75 | * |
||
76 | * @var int |
||
77 | */ |
||
78 | private $_page_number; |
||
79 | |||
80 | /** |
||
81 | * Total number of pages |
||
82 | * |
||
83 | * @var int |
||
84 | */ |
||
85 | private $_page_count; |
||
86 | |||
87 | /** |
||
88 | * Image antialias factor |
||
89 | * |
||
90 | * @var float |
||
91 | */ |
||
92 | private $_aa_factor; |
||
93 | |||
94 | /** |
||
95 | * Allocated colors |
||
96 | * |
||
97 | * @var array |
||
98 | */ |
||
99 | private $_colors; |
||
100 | |||
101 | /** |
||
102 | * Background color |
||
103 | * |
||
104 | * @var int |
||
105 | */ |
||
106 | private $_bg_color; |
||
107 | |||
108 | /** |
||
109 | * Background color array |
||
110 | * |
||
111 | * @var int |
||
112 | */ |
||
113 | private $_bg_color_array; |
||
114 | |||
115 | /** |
||
116 | * Actual DPI |
||
117 | * |
||
118 | * @var int |
||
119 | */ |
||
120 | private $dpi; |
||
121 | |||
122 | /** |
||
123 | * Amount to scale font sizes |
||
124 | * |
||
125 | * Font sizes are 72 DPI, GD internally uses 96. Scale them proportionally. |
||
126 | * 72 / 96 = 0.75. |
||
127 | * |
||
128 | * @var float |
||
129 | */ |
||
130 | const FONT_SCALE = 0.75; |
||
131 | |||
132 | /** |
||
133 | * Class constructor |
||
134 | * |
||
135 | * @param mixed $size The size of image to create: array(x1,y1,x2,y2) or "letter", "legal", etc. |
||
136 | * @param string $orientation The orientation of the document (either 'landscape' or 'portrait') |
||
137 | * @param Dompdf $dompdf |
||
138 | * @param float $aa_factor Anti-aliasing factor, 1 for no AA |
||
139 | * @param array $bg_color Image background color: array(r,g,b,a), 0 <= r,g,b,a <= 1 |
||
140 | */ |
||
141 | public function __construct($size = 'letter', $orientation = "portrait", Dompdf $dompdf, $aa_factor = 1.0, $bg_color = array(1, 1, 1, 0)) |
||
186 | |||
187 | /** |
||
188 | * @return Dompdf |
||
189 | */ |
||
190 | public function get_dompdf() |
||
194 | |||
195 | /** |
||
196 | * Return the GF image resource |
||
197 | * |
||
198 | * @return resource |
||
199 | */ |
||
200 | public function get_image() |
||
204 | |||
205 | /** |
||
206 | * Return the image's width in pixels |
||
207 | * |
||
208 | * @return float |
||
209 | */ |
||
210 | public function get_width() |
||
214 | |||
215 | /** |
||
216 | * Return the image's height in pixels |
||
217 | * |
||
218 | * @return float |
||
219 | */ |
||
220 | public function get_height() |
||
224 | |||
225 | /** |
||
226 | * Returns the current page number |
||
227 | * @return int |
||
228 | */ |
||
229 | public function get_page_number() |
||
233 | |||
234 | /** |
||
235 | * Returns the total number of pages in the document |
||
236 | * @return int |
||
237 | */ |
||
238 | public function get_page_count() |
||
242 | |||
243 | /** |
||
244 | * Sets the current page number |
||
245 | * |
||
246 | * @param int $num |
||
247 | */ |
||
248 | public function set_page_number($num) |
||
252 | |||
253 | /** |
||
254 | * Sets the page count |
||
255 | * |
||
256 | * @param int $count |
||
257 | */ |
||
258 | public function set_page_count($count) |
||
262 | |||
263 | /** |
||
264 | * Sets the opacity |
||
265 | * |
||
266 | * @param $opacity |
||
267 | * @param $mode |
||
268 | */ |
||
269 | public function set_opacity($opacity, $mode = "Normal") |
||
273 | |||
274 | /** |
||
275 | * Allocate a new color. Allocate with GD as needed and store |
||
276 | * previously allocated colors in $this->_colors. |
||
277 | * |
||
278 | * @param array $color The new current color |
||
279 | * @return int The allocated color |
||
280 | */ |
||
281 | private function _allocate_color($color) |
||
321 | |||
322 | /** |
||
323 | * Scales value up to the current canvas DPI from 72 DPI |
||
324 | * |
||
325 | * @param float $length |
||
326 | * @return float |
||
327 | */ |
||
328 | private function _upscale($length) |
||
332 | |||
333 | /** |
||
334 | * Scales value down from the current canvas DPI to 72 DPI |
||
335 | * |
||
336 | * @param float $length |
||
337 | * @return float |
||
338 | */ |
||
339 | private function _downscale($length) |
||
343 | |||
344 | /** |
||
345 | * Draws a line from x1,y1 to x2,y2 |
||
346 | * |
||
347 | * See {@link Style::munge_color()} for the format of the color array. |
||
348 | * See {@link Cpdf::setLineStyle()} for a description of the format of the |
||
349 | * $style parameter (aka dash). |
||
350 | * |
||
351 | * @param float $x1 |
||
352 | * @param float $y1 |
||
353 | * @param float $x2 |
||
354 | * @param float $y2 |
||
355 | * @param array $color |
||
356 | * @param float $width |
||
357 | * @param array $style |
||
358 | */ |
||
359 | public function line($x1, $y1, $x2, $y2, $color, $width, $style = null) |
||
412 | |||
413 | /** |
||
414 | * @param float $x1 |
||
415 | * @param float $y1 |
||
416 | * @param float $r1 |
||
417 | * @param float $r2 |
||
418 | * @param float $astart |
||
419 | * @param float $aend |
||
420 | * @param array $color |
||
421 | * @param float $width |
||
422 | * @param array $style |
||
423 | */ |
||
424 | public function arc($x1, $y1, $r1, $r2, $astart, $aend, $color, $width, $style = array()) |
||
428 | |||
429 | /** |
||
430 | * Draws a rectangle at x1,y1 with width w and height h |
||
431 | * |
||
432 | * See {@link Style::munge_color()} for the format of the color array. |
||
433 | * See {@link Cpdf::setLineStyle()} for a description of the $style |
||
434 | * parameter (aka dash) |
||
435 | * |
||
436 | * @param float $x1 |
||
437 | * @param float $y1 |
||
438 | * @param float $w |
||
439 | * @param float $h |
||
440 | * @param array $color |
||
441 | * @param float $width |
||
442 | * @param array $style |
||
443 | */ |
||
444 | public function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) |
||
476 | |||
477 | /** |
||
478 | * Draws a filled rectangle at x1,y1 with width w and height h |
||
479 | * |
||
480 | * See {@link Style::munge_color()} for the format of the color array. |
||
481 | * |
||
482 | * @param float $x1 |
||
483 | * @param float $y1 |
||
484 | * @param float $w |
||
485 | * @param float $h |
||
486 | * @param array $color |
||
487 | */ |
||
488 | public function filled_rectangle($x1, $y1, $w, $h, $color) |
||
500 | |||
501 | /** |
||
502 | * Starts a clipping rectangle at x1,y1 with width w and height h |
||
503 | * |
||
504 | * @param float $x1 |
||
505 | * @param float $y1 |
||
506 | * @param float $w |
||
507 | * @param float $h |
||
508 | */ |
||
509 | public function clipping_rectangle($x1, $y1, $w, $h) |
||
513 | |||
514 | public function clipping_roundrectangle($x1, $y1, $w, $h, $rTL, $rTR, $rBR, $rBL) |
||
518 | |||
519 | /** |
||
520 | * Ends the last clipping shape |
||
521 | */ |
||
522 | public function clipping_end() |
||
526 | |||
527 | /** |
||
528 | * |
||
529 | */ |
||
530 | public function save() |
||
534 | |||
535 | /** |
||
536 | * |
||
537 | */ |
||
538 | public function restore() |
||
542 | |||
543 | /** |
||
544 | * @param $angle |
||
545 | * @param $x |
||
546 | * @param $y |
||
547 | */ |
||
548 | public function rotate($angle, $x, $y) |
||
552 | |||
553 | /** |
||
554 | * @param $angle_x |
||
555 | * @param $angle_y |
||
556 | * @param $x |
||
557 | * @param $y |
||
558 | */ |
||
559 | public function skew($angle_x, $angle_y, $x, $y) |
||
563 | |||
564 | /** |
||
565 | * @param $s_x |
||
566 | * @param $s_y |
||
567 | * @param $x |
||
568 | * @param $y |
||
569 | */ |
||
570 | public function scale($s_x, $s_y, $x, $y) |
||
574 | |||
575 | /** |
||
576 | * @param $t_x |
||
577 | * @param $t_y |
||
578 | */ |
||
579 | public function translate($t_x, $t_y) |
||
583 | |||
584 | /** |
||
585 | * @param $a |
||
586 | * @param $b |
||
587 | * @param $c |
||
588 | * @param $d |
||
589 | * @param $e |
||
590 | * @param $f |
||
591 | */ |
||
592 | public function transform($a, $b, $c, $d, $e, $f) |
||
596 | |||
597 | /** |
||
598 | * Draws a polygon |
||
599 | * |
||
600 | * The polygon is formed by joining all the points stored in the $points |
||
601 | * array. $points has the following structure: |
||
602 | * <code> |
||
603 | * array(0 => x1, |
||
604 | * 1 => y1, |
||
605 | * 2 => x2, |
||
606 | * 3 => y2, |
||
607 | * ... |
||
608 | * ); |
||
609 | * </code> |
||
610 | * |
||
611 | * See {@link Style::munge_color()} for the format of the color array. |
||
612 | * See {@link Cpdf::setLineStyle()} for a description of the $style |
||
613 | * parameter (aka dash) |
||
614 | * |
||
615 | * @param array $points |
||
616 | * @param array $color |
||
617 | * @param float $width |
||
618 | * @param array $style |
||
619 | * @param bool $fill Fills the polygon if true |
||
620 | */ |
||
621 | public function polygon($points, $color, $width = null, $style = null, $fill = false) |
||
655 | |||
656 | /** |
||
657 | * Draws a circle at $x,$y with radius $r |
||
658 | * |
||
659 | * See {@link Style::munge_color()} for the format of the color array. |
||
660 | * See {@link Cpdf::setLineStyle()} for a description of the $style |
||
661 | * parameter (aka dash) |
||
662 | * |
||
663 | * @param float $x |
||
664 | * @param float $y |
||
665 | * @param float $r |
||
666 | * @param array $color |
||
667 | * @param float $width |
||
668 | * @param array $style |
||
669 | * @param bool $fill Fills the circle if true |
||
670 | */ |
||
671 | public function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false) |
||
704 | |||
705 | /** |
||
706 | * Add an image to the pdf. |
||
707 | * The image is placed at the specified x and y coordinates with the |
||
708 | * given width and height. |
||
709 | * |
||
710 | * @param string $img_url the path to the image |
||
711 | * @param float $x x position |
||
712 | * @param float $y y position |
||
713 | * @param int $w width (in pixels) |
||
714 | * @param int $h height (in pixels) |
||
715 | * @param string $resolution |
||
716 | * @return void |
||
717 | * |
||
718 | * @throws \Exception |
||
719 | * @internal param string $img_type the type (e.g. extension) of the image |
||
720 | */ |
||
721 | public function image($img_url, $x, $y, $w, $h, $resolution = "normal") |
||
754 | |||
755 | /** |
||
756 | * Writes text at the specified x and y coordinates |
||
757 | * See {@link Style::munge_color()} for the format of the color array. |
||
758 | * |
||
759 | * @param float $x |
||
760 | * @param float $y |
||
761 | * @param string $text the text to write |
||
762 | * @param string $font the font file to use |
||
763 | * @param float $size the font size, in points |
||
764 | * @param array $color |
||
765 | * @param float $word_spacing word spacing adjustment |
||
766 | * @param float $char_spacing |
||
767 | * @param float $angle Text angle |
||
768 | * |
||
769 | * @return void |
||
770 | */ |
||
771 | public function text($x, $y, $text, $font, $size, $color = array(0, 0, 0), $word_spacing = 0.0, $char_spacing = 0.0, $angle = 0.0) |
||
794 | |||
795 | public function javascript($code) |
||
799 | |||
800 | /** |
||
801 | * Add a named destination (similar to <a name="foo">...</a> in html) |
||
802 | * |
||
803 | * @param string $anchorname The name of the named destination |
||
804 | */ |
||
805 | public function add_named_dest($anchorname) |
||
809 | |||
810 | /** |
||
811 | * Add a link to the pdf |
||
812 | * |
||
813 | * @param string $url The url to link to |
||
814 | * @param float $x The x position of the link |
||
815 | * @param float $y The y position of the link |
||
816 | * @param float $width The width of the link |
||
817 | * @param float $height The height of the link |
||
818 | */ |
||
819 | public function add_link($url, $x, $y, $width, $height) |
||
823 | |||
824 | /** |
||
825 | * Add meta information to the PDF |
||
826 | * |
||
827 | * @param string $label label of the value (Creator, Producer, etc.) |
||
828 | * @param string $value the text to set |
||
829 | */ |
||
830 | public function add_info($label, $value) |
||
834 | |||
835 | /** |
||
836 | * @param string $view |
||
837 | * @param array $options |
||
838 | */ |
||
839 | public function set_default_view($view, $options = array()) |
||
843 | |||
844 | /** |
||
845 | * Calculates text size, in points |
||
846 | * |
||
847 | * @param string $text the text to be sized |
||
848 | * @param string $font the desired font |
||
849 | * @param float $size the desired font size |
||
850 | * @param float $word_spacing word spacing, if any |
||
851 | * @param float $char_spacing char spacing, if any |
||
852 | * |
||
853 | * @return float |
||
854 | */ |
||
855 | public function get_text_width($text, $font, $size, $word_spacing = 0.0, $char_spacing = 0.0) |
||
874 | |||
875 | /** |
||
876 | * @param $font |
||
877 | * @return string |
||
878 | */ |
||
879 | public function get_ttf_file($font) |
||
903 | |||
904 | /** |
||
905 | * Calculates font height, in points |
||
906 | * |
||
907 | * @param string $font |
||
908 | * @param float $size |
||
909 | * @return float |
||
910 | */ |
||
911 | public function get_font_height($font, $size) |
||
919 | |||
920 | private function get_font_height_actual($font, $size) |
||
929 | |||
930 | /** |
||
931 | * @param string $font |
||
932 | * @param float $size |
||
933 | * @return float |
||
934 | */ |
||
935 | public function get_font_baseline($font, $size) |
||
940 | |||
941 | /** |
||
942 | * Starts a new page |
||
943 | * |
||
944 | * Subsequent drawing operations will appear on the new page. |
||
945 | */ |
||
946 | public function new_page() |
||
960 | |||
961 | public function open_object() |
||
965 | |||
966 | public function close_object() |
||
970 | |||
971 | public function add_object() |
||
975 | |||
976 | public function page_text() |
||
980 | |||
981 | public function page_line() |
||
985 | |||
986 | /** |
||
987 | * Streams the image to the client. |
||
988 | * |
||
989 | * @param string $filename The filename to present to the client. |
||
990 | * @param array $options Associative array: 'type' => jpeg|jpg|png; 'quality' => 0 - 100 (JPEG only); |
||
991 | * 'page' => Number of the page to output (defaults to the first); 'Attachment': 1 or 0 (default 1). |
||
992 | */ |
||
993 | public function stream($filename, $options = array()) |
||
1026 | |||
1027 | /** |
||
1028 | * Returns the image as a string. |
||
1029 | * |
||
1030 | * @param array $options Associative array: 'type' => jpeg|jpg|png; 'quality' => 0 - 100 (JPEG only); |
||
1031 | * 'page' => Number of the page to output (defaults to the first). |
||
1032 | * @return string |
||
1033 | */ |
||
1034 | public function output($options = array()) |
||
1042 | |||
1043 | /** |
||
1044 | * Outputs the image stream directly. |
||
1045 | * |
||
1046 | * @param array $options Associative array: 'type' => jpeg|jpg|png; 'quality' => 0 - 100 (JPEG only); |
||
1047 | * 'page' => Number of the page to output (defaults to the first). |
||
1048 | */ |
||
1049 | private function _output($options = array()) |
||
1092 | } |
||
1093 |
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.