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 |
||
21 | trait MonoImageCompressionTrait |
||
22 | { |
||
23 | /** |
||
24 | * Get argument value |
||
25 | * |
||
26 | * @param string $name |
||
27 | * |
||
28 | * @return null|string |
||
29 | */ |
||
30 | abstract protected function getArgumentValue($name); |
||
31 | |||
32 | /** |
||
33 | * Set argument |
||
34 | * |
||
35 | * @param string $argument |
||
36 | * |
||
37 | * @return $this |
||
38 | */ |
||
39 | abstract protected function setArgument($argument); |
||
40 | |||
41 | /** |
||
42 | * Get PDF settings |
||
43 | * |
||
44 | * @return string |
||
45 | */ |
||
46 | abstract public function getPdfSettings(); |
||
47 | |||
48 | /** |
||
49 | * Whether to anti alias monochrome images |
||
50 | * |
||
51 | * @return bool |
||
52 | */ |
||
53 | 2 | public function isAntiAliasMonoImages() |
|
62 | |||
63 | /** |
||
64 | * Set anti alias monochrome images flag |
||
65 | * |
||
66 | * @param bool $antiAliasMonoImages |
||
67 | * |
||
68 | * @return $this |
||
69 | */ |
||
70 | 2 | public function setAntiAliasMonoImages($antiAliasMonoImages) |
|
76 | |||
77 | /** |
||
78 | * Whether to downsample monochrome images |
||
79 | * |
||
80 | * @return bool |
||
81 | */ |
||
82 | 10 | public function isDownsampleMonoImages() |
|
83 | { |
||
84 | 10 | $value = $this->getArgumentValue('-dDownsampleMonoImages'); |
|
85 | 10 | if (null === $value) { |
|
86 | 10 | switch ($this->getPdfSettings()) { |
|
87 | 10 | case PdfSettings::SCREEN: |
|
88 | 9 | case PdfSettings::EBOOK: |
|
89 | 4 | return true; |
|
90 | 3 | default: |
|
91 | 6 | return false; |
|
92 | 3 | } |
|
93 | } |
||
94 | |||
95 | 10 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
96 | } |
||
97 | |||
98 | /** |
||
99 | * Set downsample monochrome images flag |
||
100 | * |
||
101 | * @param bool $downsampleMonoImages |
||
102 | * |
||
103 | * @return $this |
||
104 | */ |
||
105 | 10 | public function setDownsampleMonoImages($downsampleMonoImages) |
|
111 | |||
112 | /** |
||
113 | * Whether to encode monochrome images |
||
114 | * |
||
115 | * @return bool |
||
116 | */ |
||
117 | 2 | public function isEncodeMonoImages() |
|
126 | |||
127 | /** |
||
128 | * Set encode monochrome images flag |
||
129 | * |
||
130 | * @param bool $encodeMonoImages |
||
131 | * |
||
132 | * @return $this |
||
133 | */ |
||
134 | 2 | public function setEncodeMonoImages($encodeMonoImages) |
|
140 | |||
141 | /** |
||
142 | * Get monochrome image depth |
||
143 | * |
||
144 | * @return int |
||
145 | */ |
||
146 | 2 | public function getMonoImageDepth() |
|
155 | |||
156 | /** |
||
157 | * Set monochrome image depth |
||
158 | * |
||
159 | * @param int $monoImageDepth |
||
160 | * |
||
161 | * @return $this |
||
162 | */ |
||
163 | 2 | public function setMonoImageDepth($monoImageDepth) |
|
169 | |||
170 | /** |
||
171 | * Get monochrome image downsample threshold |
||
172 | * |
||
173 | * @return float |
||
174 | */ |
||
175 | 2 | public function getMonoImageDownsampleThreshold() |
|
184 | |||
185 | /** |
||
186 | * Set monochrome image downsample threshold |
||
187 | * |
||
188 | * @param float $monoImageDownsampleThreshold |
||
189 | * |
||
190 | * @return $this |
||
191 | */ |
||
192 | 2 | public function setMonoImageDownsampleThreshold($monoImageDownsampleThreshold) |
|
198 | |||
199 | /** |
||
200 | * Get monochrome image downsample type |
||
201 | * |
||
202 | * @return string |
||
203 | */ |
||
204 | 10 | View Code Duplication | public function getMonoImageDownsampleType() |
|
|||
205 | { |
||
206 | 10 | $value = $this->getArgumentValue('-dMonoImageDownsampleType'); |
|
207 | 10 | if (null === $value) { |
|
208 | 10 | switch ($this->getPdfSettings()) { |
|
209 | 10 | case PdfSettings::PREPRESS: |
|
210 | 2 | return ImageDownsampleType::BICUBIC; |
|
211 | 4 | default: |
|
212 | 8 | return ImageDownsampleType::SUBSAMPLE; |
|
213 | 4 | } |
|
214 | } |
||
215 | |||
216 | 10 | return ltrim($value, '/'); |
|
217 | } |
||
218 | |||
219 | /** |
||
220 | * Set monochrome image downsample type |
||
221 | * |
||
222 | * @param string $monoImageDownsampleType |
||
223 | * |
||
224 | * @throws \InvalidArgumentException |
||
225 | * |
||
226 | * @return $this |
||
227 | */ |
||
228 | 12 | public function setMonoImageDownsampleType($monoImageDownsampleType) |
|
239 | |||
240 | /** |
||
241 | * Get monochrome image filter |
||
242 | * |
||
243 | * @return string |
||
244 | */ |
||
245 | 8 | public function getMonoImageFilter() |
|
254 | |||
255 | /** |
||
256 | * Set monochrome image filter |
||
257 | * |
||
258 | * @param string $monoImageFilter |
||
259 | * |
||
260 | * @throws \InvalidArgumentException |
||
261 | * |
||
262 | * @return $this |
||
263 | */ |
||
264 | 8 | public function setMonoImageFilter($monoImageFilter) |
|
275 | |||
276 | /** |
||
277 | * Get monochrome image resolution |
||
278 | * |
||
279 | * @return int |
||
280 | */ |
||
281 | 10 | View Code Duplication | public function getMonoImageResolution() |
282 | { |
||
283 | 10 | $value = $this->getArgumentValue('-dMonoImageResolution'); |
|
284 | 10 | if (null === $value) { |
|
285 | 10 | switch ($this->getPdfSettings()) { |
|
286 | 10 | case PdfSettings::PRINTER: |
|
287 | 9 | case PdfSettings::PREPRESS: |
|
288 | 4 | return 1200; |
|
289 | 3 | default: |
|
290 | 6 | return 300; |
|
291 | 3 | } |
|
292 | } |
||
293 | |||
294 | 10 | return intval($value); |
|
295 | } |
||
296 | |||
297 | /** |
||
298 | * Set monochrome image resolution |
||
299 | * |
||
300 | * @param int $monoImageResolution |
||
301 | * |
||
302 | * @return $this |
||
303 | */ |
||
304 | 10 | public function setMonoImageResolution($monoImageResolution) |
|
310 | } |
||
311 |
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.