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 DistillerParametersTrait 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 DistillerParametersTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | trait DistillerParametersTrait |
||
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 | * Get auto rotate pages |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | 10 | public function getAutoRotatePages() |
|
54 | { |
||
55 | 10 | $value = $this->getArgumentValue('-dAutoRotatePages'); |
|
56 | 10 | if (null === $value) { |
|
57 | 10 | switch ($this->getPdfSettings()) { |
|
58 | 10 | case PdfSettings::EBOOK: |
|
59 | 2 | return AutoRotatePages::ALL; |
|
60 | 8 | case PdfSettings::PRINTER: |
|
61 | 7 | case PdfSettings::PREPRESS: |
|
62 | 4 | return AutoRotatePages::NONE; |
|
63 | 2 | default: |
|
64 | 4 | return AutoRotatePages::PAGE_BY_PAGE; |
|
65 | 2 | } |
|
66 | } |
||
67 | |||
68 | 10 | return ltrim($value, '/'); |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * Set auto rotate pages |
||
73 | * |
||
74 | * @param string $autoRotatePages |
||
75 | * |
||
76 | * @param \InvalidArgumentException |
||
77 | * |
||
78 | * @return $this |
||
79 | */ |
||
80 | 12 | public function setAutoRotatePages($autoRotatePages) |
|
91 | |||
92 | /** |
||
93 | * Get binding |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | 2 | public function getBinding() |
|
106 | |||
107 | /** |
||
108 | * Set binding |
||
109 | * |
||
110 | * @param string $binding |
||
111 | * |
||
112 | * @param \InvalidArgumentException |
||
113 | * |
||
114 | * @return $this |
||
115 | */ |
||
116 | 4 | public function setBinding($binding) |
|
127 | |||
128 | /** |
||
129 | * Get compatibility level |
||
130 | * |
||
131 | * @return float |
||
132 | */ |
||
133 | 10 | View Code Duplication | public function getCompatibilityLevel() |
|
|||
134 | { |
||
135 | 10 | $value = $this->getArgumentValue('-dCompatibilityLevel'); |
|
136 | 10 | if (null === $value) { |
|
137 | 10 | switch ($this->getPdfSettings()) { |
|
138 | 10 | case PdfSettings::SCREEN: |
|
139 | 2 | return 1.3; |
|
140 | 4 | default: |
|
141 | 8 | return 1.4; |
|
142 | 4 | } |
|
143 | } |
||
144 | |||
145 | 10 | return floatval($value); |
|
146 | } |
||
147 | |||
148 | /** |
||
149 | * Set compatibility level |
||
150 | * |
||
151 | * @param float $compatibilityLevel |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | 10 | public function setCompatibilityLevel($compatibilityLevel) |
|
161 | |||
162 | /** |
||
163 | * Get core dist version |
||
164 | * |
||
165 | * @return int |
||
166 | */ |
||
167 | 2 | public function getCoreDistVersion() |
|
176 | |||
177 | /** |
||
178 | * Set core dist version |
||
179 | * |
||
180 | * @param int $coreDistVersion |
||
181 | * |
||
182 | * @return $this |
||
183 | */ |
||
184 | 2 | public function setCoreDistVersion($coreDistVersion) |
|
190 | |||
191 | /** |
||
192 | * Whether to do thumbnails |
||
193 | * |
||
194 | * @return bool |
||
195 | */ |
||
196 | 10 | public function isDoThumbnails() |
|
197 | { |
||
198 | 10 | $value = $this->getArgumentValue('-dDoThumbnails'); |
|
199 | 10 | if (null === $value) { |
|
200 | 10 | switch ($this->getPdfSettings()) { |
|
201 | 10 | case PdfSettings::PREPRESS: |
|
202 | 2 | return true; |
|
203 | 4 | default: |
|
204 | 8 | return false; |
|
205 | 4 | } |
|
206 | } |
||
207 | |||
208 | 10 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
209 | } |
||
210 | |||
211 | /** |
||
212 | * Set do thumbnails flag |
||
213 | * |
||
214 | * @param bool $doThumbnails |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | 10 | public function setDoThumbnails($doThumbnails) |
|
224 | |||
225 | /** |
||
226 | * Get end page |
||
227 | * |
||
228 | * @return int |
||
229 | */ |
||
230 | 2 | public function getEndPage() |
|
239 | |||
240 | /** |
||
241 | * Set end page |
||
242 | * |
||
243 | * @param int $endPage |
||
244 | * |
||
245 | * @return $this |
||
246 | */ |
||
247 | 2 | public function setEndPage($endPage) |
|
253 | |||
254 | /** |
||
255 | * Get image memory |
||
256 | * |
||
257 | * @return int |
||
258 | */ |
||
259 | 2 | public function getImageMemory() |
|
268 | |||
269 | /** |
||
270 | * Set image memory |
||
271 | * |
||
272 | * @param int $imageMemory |
||
273 | * |
||
274 | * @return $this |
||
275 | */ |
||
276 | 2 | public function setImageMemory($imageMemory) |
|
282 | |||
283 | /** |
||
284 | * Get off optimizations |
||
285 | * |
||
286 | * @return int |
||
287 | */ |
||
288 | 2 | public function getOffOptimizations() |
|
297 | |||
298 | /** |
||
299 | * Set off optimizations |
||
300 | * |
||
301 | * @param int $offOptimizations |
||
302 | * |
||
303 | * @return $this |
||
304 | */ |
||
305 | 2 | public function setOffOptimizations($offOptimizations) |
|
311 | |||
312 | /** |
||
313 | * Whether to optimize |
||
314 | * |
||
315 | * @return bool |
||
316 | */ |
||
317 | 10 | public function isOptimize() |
|
318 | { |
||
319 | 10 | $value = $this->getArgumentValue('-dOptimize'); |
|
320 | 10 | if (null === $value) { |
|
321 | 10 | switch ($this->getPdfSettings()) { |
|
322 | 10 | case PdfSettings::SCREEN: |
|
323 | 9 | case PdfSettings::EBOOK: |
|
324 | 8 | case PdfSettings::PRINTER: |
|
325 | 7 | case PdfSettings::PREPRESS: |
|
326 | 8 | return true; |
|
327 | 1 | default: |
|
328 | 2 | return false; |
|
329 | 1 | } |
|
330 | } |
||
331 | |||
332 | 10 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
333 | } |
||
334 | |||
335 | /** |
||
336 | * Set optimize flag |
||
337 | * |
||
338 | * @param bool $optimize |
||
339 | * |
||
340 | * @return $this |
||
341 | */ |
||
342 | 10 | public function setOptimize($optimize) |
|
348 | |||
349 | /** |
||
350 | * Get start page |
||
351 | * |
||
352 | * @return int |
||
353 | */ |
||
354 | 2 | public function getStartPage() |
|
363 | |||
364 | /** |
||
365 | * Set start page |
||
366 | * |
||
367 | * @param int $startPage |
||
368 | * |
||
369 | * @return $this |
||
370 | */ |
||
371 | 2 | public function setStartPage($startPage) |
|
377 | |||
378 | /** |
||
379 | * Whether to use flate compression |
||
380 | * |
||
381 | * @return bool |
||
382 | */ |
||
383 | 2 | public function isUseFlateCompression() |
|
392 | |||
393 | /** |
||
394 | * Set use flate compression flag |
||
395 | * |
||
396 | * @param bool $useFlateCompression |
||
397 | * |
||
398 | * @return $this |
||
399 | */ |
||
400 | 2 | public function setUseFlateCompression($useFlateCompression) |
|
406 | } |
||
407 |
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.