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:
1 | <?php |
||
18 | class Watermark extends IMethod |
||
19 | { |
||
20 | protected $_x = 0; |
||
21 | protected $_y = 0; |
||
22 | protected $_transparency = 100; |
||
23 | |||
24 | /** |
||
25 | * @var \library\images\Image |
||
26 | */ |
||
27 | protected $_watermark; |
||
28 | |||
29 | protected function init() |
||
31 | |||
32 | /** |
||
33 | * Sets transparency for watermark |
||
34 | * |
||
35 | * @param int $transparency |
||
36 | * @return self |
||
37 | */ |
||
38 | public function SetTransparency($transparency) |
||
43 | |||
44 | /** |
||
45 | * Use build-in logic to position the watermark |
||
46 | * |
||
47 | * @param string $x |
||
48 | * @param string $y |
||
49 | * @return self |
||
50 | */ |
||
51 | public function SetPosition($x, $y) |
||
57 | |||
58 | /** |
||
59 | * Use build-in logic to position the x of watermark |
||
60 | * |
||
61 | * @param resource $imageResource |
||
62 | * @return self |
||
63 | */ |
||
64 | View Code Duplication | protected function calculateX($imageResource) |
|
|
|||
65 | { |
||
66 | if (intval($this->_x) === $this->_x) return $this->_x; |
||
67 | |||
68 | $x = strtolower($this->_x); |
||
69 | |||
70 | $imageWidth = imagesx($imageResource); |
||
71 | $watermarkWidth = imagesx($this->GetWatermark()->GetImageResource()); |
||
72 | |||
73 | if ($x == 'left') { |
||
74 | $x = 0; |
||
75 | } elseif ($x == 'center') { |
||
76 | $x = $imageWidth / 2 - ($watermarkWidth / 2); |
||
77 | } elseif ($x == 'right') { |
||
78 | $x = $imageWidth - $watermarkWidth; |
||
79 | } |
||
80 | return intval($x); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Use build-in logic to position the y of watermark |
||
85 | * |
||
86 | * @param resource $imageResource |
||
87 | * @return self |
||
88 | */ |
||
89 | View Code Duplication | public function calculateY($imageResource) |
|
90 | { |
||
91 | if (intval($this->_y) === $this->_y) return $this->_y; |
||
92 | |||
93 | $y = strtolower($this->_y); |
||
94 | |||
95 | $imageHeight = imagesy($imageResource); |
||
96 | $watermarkHeight = imagesy($this->GetWatermark()->GetImageResource()); |
||
97 | |||
98 | if ($y == 'top') { |
||
99 | $y = 0; |
||
100 | } elseif ($y == 'center') { |
||
101 | $y = $imageHeight / 2 - ($watermarkHeight / 2); |
||
102 | } elseif ($y == 'bottom') { |
||
103 | $y = $imageHeight - $watermarkHeight; |
||
104 | } |
||
105 | return intval($y); |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Sets the image that will be used as watermark |
||
110 | * |
||
111 | * @param Image $image |
||
112 | * @return Watermark |
||
113 | */ |
||
114 | public function SetWatermark(Image $image) |
||
119 | |||
120 | /** |
||
121 | * Returns the watermark. |
||
122 | * Throws an Exception if it's not set or if it's not an \library\image\Image |
||
123 | * @return \library\images\Image |
||
124 | * @throws \Exception |
||
125 | */ |
||
126 | public function GetWatermark() |
||
131 | |||
132 | /** |
||
133 | * Set the x |
||
134 | * |
||
135 | * @param int | string $x |
||
136 | * @return self |
||
137 | */ |
||
138 | public function SetX($x) |
||
143 | |||
144 | /** |
||
145 | * Set the y |
||
146 | * |
||
147 | * @param int | string $y |
||
148 | * @return self |
||
149 | */ |
||
150 | public function SetY($y) |
||
155 | |||
156 | public function Execute($imageResource) |
||
188 | } |
||
189 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.