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 |
||
15 | trait DistillerParametersTrait |
||
16 | { |
||
17 | /** |
||
18 | * Get argument value |
||
19 | * |
||
20 | * @param string $name |
||
21 | * |
||
22 | * @return string |
||
23 | */ |
||
24 | abstract function getArgumentValue($name); |
||
25 | |||
26 | /** |
||
27 | * Set argument |
||
28 | * |
||
29 | * @param string $argument |
||
30 | * |
||
31 | * @return $this |
||
32 | */ |
||
33 | abstract function setArgument($argument); |
||
34 | |||
35 | /** |
||
36 | * Get always embed |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | public function getAlwaysEmbed() |
||
49 | |||
50 | /** |
||
51 | * Set always embed |
||
52 | * |
||
53 | * @param array $alwaysEmbed |
||
54 | * |
||
55 | * @return $this |
||
56 | */ |
||
57 | public function setAlwaysEmbed(array $alwaysEmbed) |
||
65 | |||
66 | /** |
||
67 | * Whether to anti alias color images |
||
68 | * |
||
69 | * @return bool |
||
70 | */ |
||
71 | public function isAntiAliasColorImages() |
||
75 | |||
76 | /** |
||
77 | * Set anti alias color images flag |
||
78 | * |
||
79 | * @param bool $antiAliasColorImages |
||
80 | * |
||
81 | * @return $this |
||
82 | */ |
||
83 | public function setAntiAliasColorImages($antiAliasColorImages) |
||
89 | |||
90 | /** |
||
91 | * Whether to anti alias gray images |
||
92 | * |
||
93 | * @return bool |
||
94 | */ |
||
95 | public function isAntiAliasGrayImages() |
||
99 | |||
100 | /** |
||
101 | * Set anti alias gray images flag |
||
102 | * |
||
103 | * @param bool $antiAliasGrayImages |
||
104 | * |
||
105 | * @return $this |
||
106 | */ |
||
107 | public function setAntiAliasGrayImages($antiAliasGrayImages) |
||
113 | |||
114 | /** |
||
115 | * Whether to anti alias mono images |
||
116 | * |
||
117 | * @return bool |
||
118 | */ |
||
119 | public function isAntiAliasMonoImages() |
||
123 | |||
124 | /** |
||
125 | * Set anti alias mono images flag |
||
126 | * |
||
127 | * @param bool $antiAliasMonoImages |
||
128 | * |
||
129 | * @return $this |
||
130 | */ |
||
131 | public function setAntiAliasMonoImages($antiAliasMonoImages) |
||
137 | |||
138 | /** |
||
139 | * Whether ASCII85 encode pages |
||
140 | * |
||
141 | * @return bool |
||
142 | */ |
||
143 | public function isAscii85EncodePages() |
||
147 | |||
148 | /** |
||
149 | * Set ASCII85 encode pages flag |
||
150 | * |
||
151 | * @param bool $ascii85EncodePages |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setAscii85EncodePages($ascii85EncodePages) |
||
161 | |||
162 | /** |
||
163 | * Whether to auto filter color images |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | public function isAutoFilterColorImages() |
||
171 | |||
172 | /** |
||
173 | * Set auto filter color images flag |
||
174 | * |
||
175 | * @param bool $autoFilterColorImages |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function setAutoFilterColorImages($autoFilterColorImages) |
||
185 | |||
186 | /** |
||
187 | * Whether to auto filter gray images |
||
188 | * |
||
189 | * @return bool |
||
190 | */ |
||
191 | public function isAutoFilterGrayImages() |
||
195 | |||
196 | /** |
||
197 | * Set auto filter gray images flag |
||
198 | * |
||
199 | * @param bool $autoFilterGrayImages |
||
200 | * |
||
201 | * @return $this |
||
202 | */ |
||
203 | public function setAutoFilterGrayImages($autoFilterGrayImages) |
||
209 | |||
210 | /** |
||
211 | * Whether to auto position EPS files |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | public function isAutoPositionEpsFiles() |
||
219 | |||
220 | /** |
||
221 | * Set auto position EPS files flag |
||
222 | * |
||
223 | * @param bool $autoPositionEpsFiles |
||
224 | * |
||
225 | * @return $this |
||
226 | */ |
||
227 | public function setAutoPositionEpsFiles($autoPositionEpsFiles) |
||
233 | |||
234 | /** |
||
235 | * Get auto rotate pages |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getAutoRotatePages() |
||
248 | |||
249 | |||
250 | /** |
||
251 | * Set auto rotate pages |
||
252 | * |
||
253 | * @param string $autoRotatePages |
||
254 | * |
||
255 | * @param \InvalidArgumentException |
||
256 | * |
||
257 | * @return $this |
||
258 | */ |
||
259 | View Code Duplication | public function setAutoRotatePages($autoRotatePages) |
|
274 | |||
275 | /** |
||
276 | * Get binding |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | public function getBinding() |
||
289 | |||
290 | |||
291 | /** |
||
292 | * Set binding |
||
293 | * |
||
294 | * @param string $binding |
||
295 | * |
||
296 | * @param \InvalidArgumentException |
||
297 | * |
||
298 | * @return $this |
||
299 | */ |
||
300 | View Code Duplication | public function setBinding($binding) |
|
314 | |||
315 | /** |
||
316 | * Get cal CMYK profile |
||
317 | * |
||
318 | * @return null|string |
||
319 | */ |
||
320 | public function getCalCmykProfile() |
||
329 | |||
330 | /** |
||
331 | * Set cal CMYK profile |
||
332 | * |
||
333 | * @param string $valCmykProfile |
||
334 | * |
||
335 | * @return $this |
||
336 | */ |
||
337 | public function setCalCmykProfile($valCmykProfile) |
||
343 | |||
344 | /** |
||
345 | * Get cal gray profile |
||
346 | * |
||
347 | * @return null|string |
||
348 | */ |
||
349 | public function getCalGrayProfile() |
||
358 | |||
359 | /** |
||
360 | * Set cal gray profile |
||
361 | * |
||
362 | * @param string $valGrayProfile |
||
363 | * |
||
364 | * @return $this |
||
365 | */ |
||
366 | public function setCalGrayProfile($valGrayProfile) |
||
372 | |||
373 | /** |
||
374 | * Get cal RGB profile |
||
375 | * |
||
376 | * @return null|string |
||
377 | */ |
||
378 | public function getCalRgbProfile() |
||
387 | |||
388 | /** |
||
389 | * Set cal RGB profile |
||
390 | * |
||
391 | * @param string $valRgbProfile |
||
392 | * |
||
393 | * @return $this |
||
394 | */ |
||
395 | public function setCalRgbProfile($valRgbProfile) |
||
401 | |||
402 | /** |
||
403 | * Get cannot embed font policy |
||
404 | * |
||
405 | * @return string |
||
406 | */ |
||
407 | public function getCannotEmbedFontPolicy() |
||
416 | |||
417 | /** |
||
418 | * Set cannot embed font policy |
||
419 | * |
||
420 | * @param string $cannotEmbedFontPolicy |
||
421 | * |
||
422 | * @param \InvalidArgumentException |
||
423 | * |
||
424 | * @return $this |
||
425 | */ |
||
426 | View Code Duplication | public function setCannotEmbedFontPolicy($cannotEmbedFontPolicy) |
|
441 | |||
442 | /** |
||
443 | * Get compatibility level |
||
444 | * |
||
445 | * @return null|string |
||
446 | */ |
||
447 | public function getCompatibilityLevel() |
||
451 | |||
452 | /** |
||
453 | * Set compatibility level |
||
454 | * |
||
455 | * @param string $compatibilityLevel |
||
456 | * |
||
457 | * @return $this |
||
458 | */ |
||
459 | public function setCompatibilityLevel($compatibilityLevel) |
||
465 | } |
||
466 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.