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 ColorImageCompressionTrait 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 ColorImageCompressionTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | View Code Duplication | trait ColorImageCompressionTrait |
|
|
|||
20 | { |
||
21 | /** |
||
22 | * Get argument value |
||
23 | * |
||
24 | * @param string $name |
||
25 | * |
||
26 | * @return string |
||
27 | */ |
||
28 | abstract protected function getArgumentValue($name); |
||
29 | |||
30 | /** |
||
31 | * Set argument |
||
32 | * |
||
33 | * @param string $argument |
||
34 | * |
||
35 | * @return $this |
||
36 | */ |
||
37 | abstract protected function setArgument($argument); |
||
38 | |||
39 | /** |
||
40 | * Get PDF settings |
||
41 | * |
||
42 | * @return string |
||
43 | */ |
||
44 | abstract public function getPdfSettings(); |
||
45 | |||
46 | /** |
||
47 | * Whether to anti alias color images |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | public function isAntiAliasColorImages() |
||
60 | |||
61 | /** |
||
62 | * Set anti alias color images flag |
||
63 | * |
||
64 | * @param bool $antiAliasColorImages |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function setAntiAliasColorImages($antiAliasColorImages) |
||
74 | |||
75 | /** |
||
76 | * Whether to auto filter color images |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function isAutoFilterColorImages() |
||
89 | |||
90 | /** |
||
91 | * Set auto filter color images flag |
||
92 | * |
||
93 | * @param bool $autoFilterColorImages |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | public function setAutoFilterColorImages($autoFilterColorImages) |
||
103 | |||
104 | /** |
||
105 | * Get color image depth |
||
106 | * |
||
107 | * @return int |
||
108 | */ |
||
109 | public function getColorImageDepth() |
||
118 | |||
119 | /** |
||
120 | * Set color image depth |
||
121 | * |
||
122 | * @param int $colorImageDepth |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | public function setColorImageDepth($colorImageDepth) |
||
132 | |||
133 | /** |
||
134 | * Get color image downsample threshold |
||
135 | * |
||
136 | * @return float |
||
137 | */ |
||
138 | public function getColorImageDownsampleThreshold() |
||
147 | |||
148 | /** |
||
149 | * Set color image downsample threshold |
||
150 | * |
||
151 | * @param float $colorImageDownsampleThreshold |
||
152 | * |
||
153 | * @return $this |
||
154 | */ |
||
155 | public function setColorImageDownsampleThreshold($colorImageDownsampleThreshold) |
||
161 | |||
162 | /** |
||
163 | * Get color image downsample type |
||
164 | * |
||
165 | * @return string |
||
166 | */ |
||
167 | public function getColorImageDownsampleType() |
||
185 | |||
186 | /** |
||
187 | * Set color image downsample type |
||
188 | * |
||
189 | * @param string $colorImageDownsampleType |
||
190 | * |
||
191 | * @throws \InvalidArgumentException |
||
192 | * |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function setColorImageDownsampleType($colorImageDownsampleType) |
||
206 | |||
207 | /** |
||
208 | * Get color image filter |
||
209 | * |
||
210 | * @return string |
||
211 | */ |
||
212 | public function getColorImageFilter() |
||
221 | |||
222 | /** |
||
223 | * Set color image filter |
||
224 | * |
||
225 | * @param string $colorImageFilter |
||
226 | * |
||
227 | * @throws \InvalidArgumentException |
||
228 | * |
||
229 | * @return $this |
||
230 | */ |
||
231 | public function setColorImageFilter($colorImageFilter) |
||
242 | |||
243 | /** |
||
244 | * Get color image resolution |
||
245 | * |
||
246 | * @return int |
||
247 | */ |
||
248 | public function getColorImageResolution() |
||
265 | |||
266 | /** |
||
267 | * Set color image resolution |
||
268 | * |
||
269 | * @param int $colorImageResolution |
||
270 | * |
||
271 | * @return $this |
||
272 | */ |
||
273 | public function setColorImageResolution($colorImageResolution) |
||
279 | |||
280 | /** |
||
281 | * Whether to downsample color images |
||
282 | * |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function isDownsampleColorImages() |
||
300 | |||
301 | /** |
||
302 | * Set downsample color images flag |
||
303 | * |
||
304 | * @param bool $downsampleColorImages |
||
305 | * |
||
306 | * @return $this |
||
307 | */ |
||
308 | public function setDownsampleColorImages($downsampleColorImages) |
||
314 | |||
315 | /** |
||
316 | * Whether to encode color images |
||
317 | * |
||
318 | * @return bool |
||
319 | */ |
||
320 | public function isEncodeColorImages() |
||
329 | |||
330 | /** |
||
331 | * Set encode color images flag |
||
332 | * |
||
333 | * @param bool $encodeColorImages |
||
334 | * |
||
335 | * @return $this |
||
336 | */ |
||
337 | public function setEncodeColorImages($encodeColorImages) |
||
343 | } |
||
344 |
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.