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 |
||
17 | View Code Duplication | trait GrayscaleImageCompressionTrait |
|
|
|||
18 | { |
||
19 | /** |
||
20 | * Available grayscale image downsample type values |
||
21 | * |
||
22 | * @var string[] |
||
23 | */ |
||
24 | protected static $grayImageDownsampleTypeValues = [ |
||
25 | DistillerParametersInterface::IMAGE_DOWNSAMPLE_TYPE_AVERAGE, |
||
26 | DistillerParametersInterface::IMAGE_DOWNSAMPLE_TYPE_BICUBIC, |
||
27 | DistillerParametersInterface::IMAGE_DOWNSAMPLE_TYPE_SUBSAMPLE, |
||
28 | DistillerParametersInterface::IMAGE_DOWNSAMPLE_TYPE_NONE, |
||
29 | ]; |
||
30 | |||
31 | /** |
||
32 | * Available grayscale image filter values |
||
33 | * |
||
34 | * @var string[] |
||
35 | */ |
||
36 | protected static $grayImageFilterValues = [ |
||
37 | DistillerParametersInterface::IMAGE_FILTER_DCT_ENCODE, |
||
38 | DistillerParametersInterface::IMAGE_FILTER_FLATE_ENCODE |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * Get argument value |
||
43 | * |
||
44 | * @param string $name |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | abstract protected function getArgumentValue($name); |
||
49 | |||
50 | /** |
||
51 | * Set argument |
||
52 | * |
||
53 | * @param string $argument |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | abstract protected function setArgument($argument); |
||
58 | |||
59 | /** |
||
60 | * Whether to anti alias grayscale images |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | public function isAntiAliasGrayImages() |
||
73 | |||
74 | /** |
||
75 | * Set anti alias grayscale images flag |
||
76 | * |
||
77 | * @param bool $antiAliasGrayImages |
||
78 | * |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function setAntiAliasGrayImages($antiAliasGrayImages) |
||
87 | |||
88 | /** |
||
89 | * Whether to auto filter grayscale images |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function isAutoFilterGrayImages() |
||
102 | |||
103 | /** |
||
104 | * Set auto filter grayscale images flag |
||
105 | * |
||
106 | * @param bool $autoFilterGrayImages |
||
107 | * |
||
108 | * @return $this |
||
109 | */ |
||
110 | public function setAutoFilterGrayImages($autoFilterGrayImages) |
||
116 | |||
117 | /** |
||
118 | * Whether to downsample gray images |
||
119 | * |
||
120 | * @return bool |
||
121 | */ |
||
122 | public function isDownsampleGrayImages() |
||
131 | |||
132 | /** |
||
133 | * Set downsample gray images flag |
||
134 | * |
||
135 | * @param bool $downsampleGrayImages |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function setDownsampleGrayImages($downsampleGrayImages) |
||
145 | |||
146 | /** |
||
147 | * Whether to encode grayscale images |
||
148 | * |
||
149 | * @return bool |
||
150 | */ |
||
151 | public function isEncodeGrayImages() |
||
160 | |||
161 | /** |
||
162 | * Set encode grayscale images flag |
||
163 | * |
||
164 | * @param bool $encodeGrayImages |
||
165 | * |
||
166 | * @return $this |
||
167 | */ |
||
168 | public function setEncodeGrayImages($encodeGrayImages) |
||
174 | |||
175 | /** |
||
176 | * Get gray image depth |
||
177 | * |
||
178 | * @return int |
||
179 | */ |
||
180 | public function getGrayImageDepth() |
||
189 | |||
190 | /** |
||
191 | * Set gray image depth |
||
192 | * |
||
193 | * @param int $grayImageDepth |
||
194 | * |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function setGrayImageDepth($grayImageDepth) |
||
203 | |||
204 | /** |
||
205 | * Get gray image downsample threshold |
||
206 | * |
||
207 | * @return float |
||
208 | */ |
||
209 | public function getGrayImageDownsampleThreshold() |
||
218 | |||
219 | /** |
||
220 | * Set gray image downsample threshold |
||
221 | * |
||
222 | * @param float $grayImageDownsampleThreshold |
||
223 | * |
||
224 | * @return $this |
||
225 | */ |
||
226 | public function setGrayImageDownsampleThreshold($grayImageDownsampleThreshold) |
||
232 | |||
233 | /** |
||
234 | * Get gray image downsample type |
||
235 | * |
||
236 | * @return string |
||
237 | */ |
||
238 | public function getGrayImageDownsampleType() |
||
247 | |||
248 | /** |
||
249 | * Set gray image downsample type |
||
250 | * |
||
251 | * @param string $grayImageDownsampleType |
||
252 | * |
||
253 | * @throws \InvalidArgumentException |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function setGrayImageDownsampleType($grayImageDownsampleType) |
||
267 | |||
268 | /** |
||
269 | * Get gray image filter |
||
270 | * |
||
271 | * @return string |
||
272 | */ |
||
273 | public function getGrayImageFilter() |
||
282 | |||
283 | /** |
||
284 | * Set gray image filter |
||
285 | * |
||
286 | * @param string $grayImageFilter |
||
287 | * |
||
288 | * @throws \InvalidArgumentException |
||
289 | * |
||
290 | * @return $this |
||
291 | */ |
||
292 | public function setGrayImageFilter($grayImageFilter) |
||
302 | |||
303 | /** |
||
304 | * Get gray image resolution |
||
305 | * |
||
306 | * @return int |
||
307 | */ |
||
308 | public function getGrayImageResolution() |
||
317 | |||
318 | /** |
||
319 | * Set gray image resolution |
||
320 | * |
||
321 | * @param int $grayImageResolution |
||
322 | * |
||
323 | * @return $this |
||
324 | */ |
||
325 | public function setGrayImageResolution($grayImageResolution) |
||
331 | } |
||
332 |
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.