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 GrayscaleImageCompressionTrait 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 GrayscaleImageCompressionTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | View Code Duplication | trait GrayscaleImageCompressionTrait |
|
|
|||
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 grayscale images |
||
48 | * |
||
49 | * @return bool |
||
50 | */ |
||
51 | public function isAntiAliasGrayImages() |
||
60 | |||
61 | /** |
||
62 | * Set anti alias grayscale images flag |
||
63 | * |
||
64 | * @param bool $antiAliasGrayImages |
||
65 | * |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function setAntiAliasGrayImages($antiAliasGrayImages) |
||
74 | |||
75 | /** |
||
76 | * Whether to auto filter grayscale images |
||
77 | * |
||
78 | * @return bool |
||
79 | */ |
||
80 | public function isAutoFilterGrayImages() |
||
89 | |||
90 | /** |
||
91 | * Set auto filter grayscale images flag |
||
92 | * |
||
93 | * @param bool $autoFilterGrayImages |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | public function setAutoFilterGrayImages($autoFilterGrayImages) |
||
103 | |||
104 | /** |
||
105 | * Whether to downsample gray images |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | public function isDownsampleGrayImages() |
||
124 | |||
125 | /** |
||
126 | * Set downsample gray images flag |
||
127 | * |
||
128 | * @param bool $downsampleGrayImages |
||
129 | * |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function setDownsampleGrayImages($downsampleGrayImages) |
||
138 | |||
139 | /** |
||
140 | * Whether to encode grayscale images |
||
141 | * |
||
142 | * @return bool |
||
143 | */ |
||
144 | public function isEncodeGrayImages() |
||
153 | |||
154 | /** |
||
155 | * Set encode grayscale images flag |
||
156 | * |
||
157 | * @param bool $encodeGrayImages |
||
158 | * |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function setEncodeGrayImages($encodeGrayImages) |
||
167 | |||
168 | /** |
||
169 | * Get gray image depth |
||
170 | * |
||
171 | * @return int |
||
172 | */ |
||
173 | public function getGrayImageDepth() |
||
182 | |||
183 | /** |
||
184 | * Set gray image depth |
||
185 | * |
||
186 | * @param int $grayImageDepth |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function setGrayImageDepth($grayImageDepth) |
||
196 | |||
197 | /** |
||
198 | * Get gray image downsample threshold |
||
199 | * |
||
200 | * @return float |
||
201 | */ |
||
202 | public function getGrayImageDownsampleThreshold() |
||
211 | |||
212 | /** |
||
213 | * Set gray image downsample threshold |
||
214 | * |
||
215 | * @param float $grayImageDownsampleThreshold |
||
216 | * |
||
217 | * @return $this |
||
218 | */ |
||
219 | public function setGrayImageDownsampleThreshold($grayImageDownsampleThreshold) |
||
225 | |||
226 | /** |
||
227 | * Get gray image downsample type |
||
228 | * |
||
229 | * @return string |
||
230 | */ |
||
231 | public function getGrayImageDownsampleType() |
||
249 | |||
250 | /** |
||
251 | * Set gray image downsample type |
||
252 | * |
||
253 | * @param string $grayImageDownsampleType |
||
254 | * |
||
255 | * @throws \InvalidArgumentException |
||
256 | * |
||
257 | * @return $this |
||
258 | */ |
||
259 | public function setGrayImageDownsampleType($grayImageDownsampleType) |
||
270 | |||
271 | /** |
||
272 | * Get gray image filter |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | public function getGrayImageFilter() |
||
285 | |||
286 | /** |
||
287 | * Set gray image filter |
||
288 | * |
||
289 | * @param string $grayImageFilter |
||
290 | * |
||
291 | * @throws \InvalidArgumentException |
||
292 | * |
||
293 | * @return $this |
||
294 | */ |
||
295 | public function setGrayImageFilter($grayImageFilter) |
||
306 | |||
307 | /** |
||
308 | * Get gray image resolution |
||
309 | * |
||
310 | * @return int |
||
311 | */ |
||
312 | public function getGrayImageResolution() |
||
329 | |||
330 | /** |
||
331 | * Set gray image resolution |
||
332 | * |
||
333 | * @param int $grayImageResolution |
||
334 | * |
||
335 | * @return $this |
||
336 | */ |
||
337 | public function setGrayImageResolution($grayImageResolution) |
||
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.