Complex classes like ColorConversionTrait 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 ColorConversionTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | trait ColorConversionTrait |
||
24 | { |
||
25 | /** |
||
26 | * Get argument value |
||
27 | * |
||
28 | * @param string $name |
||
29 | * |
||
30 | * @return null|string |
||
31 | */ |
||
32 | abstract protected function getArgumentValue($name); |
||
33 | |||
34 | /** |
||
35 | * Set argument |
||
36 | * |
||
37 | * @param string $argument |
||
38 | * |
||
39 | * @return $this |
||
40 | */ |
||
41 | abstract protected function setArgument($argument); |
||
42 | |||
43 | /** |
||
44 | * Get PDF settings |
||
45 | * |
||
46 | * @return string |
||
47 | */ |
||
48 | abstract public function getPdfSettings(); |
||
49 | |||
50 | /** |
||
51 | * Get cal CMYK profile |
||
52 | * |
||
53 | * @return null|string |
||
54 | */ |
||
55 | 2 | public function getCalCmykProfile() |
|
64 | |||
65 | /** |
||
66 | * Set cal CMYK profile |
||
67 | * |
||
68 | * @param string $valCmykProfile |
||
69 | * |
||
70 | * @return $this |
||
71 | */ |
||
72 | 2 | public function setCalCmykProfile($valCmykProfile) |
|
78 | |||
79 | /** |
||
80 | * Get cal gray profile |
||
81 | * |
||
82 | * @return null|string |
||
83 | */ |
||
84 | 2 | public function getCalGrayProfile() |
|
93 | |||
94 | /** |
||
95 | * Set cal gray profile |
||
96 | * |
||
97 | * @param string $valGrayProfile |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | 2 | public function setCalGrayProfile($valGrayProfile) |
|
107 | |||
108 | /** |
||
109 | * Get cal RGB profile |
||
110 | * |
||
111 | * @return null|string |
||
112 | */ |
||
113 | 2 | public function getCalRgbProfile() |
|
122 | |||
123 | /** |
||
124 | * Set cal RGB profile |
||
125 | * |
||
126 | * @param string $valRgbProfile |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | 2 | public function setCalRgbProfile($valRgbProfile) |
|
136 | |||
137 | /** |
||
138 | * Get color conversion strategy |
||
139 | * |
||
140 | * @return string |
||
141 | */ |
||
142 | 10 | public function getColorConversionStrategy() |
|
143 | { |
||
144 | 10 | $value = $this->getArgumentValue('-dColorConversionStrategy'); |
|
145 | 10 | if (null === $value) { |
|
146 | 10 | switch ($this->getPdfSettings()) { |
|
147 | 10 | case PdfSettings::SCREEN: |
|
148 | 9 | case PdfSettings::EBOOK: |
|
149 | 4 | return ColorConversionStrategy::SRGB; |
|
150 | 6 | case PdfSettings::PRINTER: |
|
151 | 2 | return ColorConversionStrategy::USE_DEVICE_INDEPENDENT_COLOR; |
|
152 | 2 | default: |
|
153 | 4 | return ColorConversionStrategy::LEAVE_COLOR_UNCHANGED; |
|
154 | 2 | } |
|
155 | } |
||
156 | |||
157 | 10 | return ltrim($value, '/'); |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Set color conversion strategy |
||
162 | * |
||
163 | * @param string $colorConversionStrategy |
||
164 | * |
||
165 | * @throws \InvalidArgumentException |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | 12 | public function setColorConversionStrategy($colorConversionStrategy) |
|
180 | |||
181 | /** |
||
182 | * Whether to convert CMYK images to RGB |
||
183 | * |
||
184 | * @return bool |
||
185 | */ |
||
186 | 2 | public function isConvertCmykImagesToRgb() |
|
195 | |||
196 | /** |
||
197 | * Set convert convert CMYK images to RGB flag |
||
198 | * |
||
199 | * @param bool $convertCmykImagesToRgb |
||
200 | * |
||
201 | * @return $this |
||
202 | */ |
||
203 | 2 | public function setConvertCmykImagesToRgb($convertCmykImagesToRgb) |
|
209 | |||
210 | /** |
||
211 | * Whether to convert images to indexed |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | 2 | public function isConvertImagesToIndexed() |
|
224 | |||
225 | /** |
||
226 | * Set convert images to indexed flag |
||
227 | * |
||
228 | * @param bool $convertImagesToIndexed |
||
229 | * |
||
230 | * @return $this |
||
231 | */ |
||
232 | 2 | public function setConvertImagesToIndexed($convertImagesToIndexed) |
|
238 | |||
239 | /** |
||
240 | * Get default rendering intent |
||
241 | * |
||
242 | * @return string |
||
243 | */ |
||
244 | 12 | public function getDefaultRenderingIntent() |
|
253 | |||
254 | /** |
||
255 | * Set default rendering intent |
||
256 | * |
||
257 | * @param string $defaultRenderingIntent |
||
258 | * |
||
259 | * @throws \InvalidArgumentException |
||
260 | * |
||
261 | * @return $this |
||
262 | */ |
||
263 | 12 | public function setDefaultRenderingIntent($defaultRenderingIntent) |
|
274 | |||
275 | /** |
||
276 | * Whether to preserve halftone info |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | 2 | public function isPreserveHalftoneInfo() |
|
289 | |||
290 | /** |
||
291 | * Set preserve halftone info flag |
||
292 | * |
||
293 | * @param bool $preserveHalftoneInfo |
||
294 | * |
||
295 | * @return $this |
||
296 | */ |
||
297 | 2 | public function setPreserveHalftoneInfo($preserveHalftoneInfo) |
|
303 | |||
304 | /** |
||
305 | * Whether to preserve overprint settings |
||
306 | * |
||
307 | * @return bool |
||
308 | */ |
||
309 | 10 | public function isPreserveOverprintSettings() |
|
310 | { |
||
311 | 10 | $value = $this->getArgumentValue('-dPreserveOverprintSettings'); |
|
312 | 10 | if (null === $value) { |
|
313 | 10 | switch ($this->getPdfSettings()) { |
|
314 | 10 | case PdfSettings::PRINTER: |
|
315 | 9 | case PdfSettings::PREPRESS: |
|
316 | 4 | return true; |
|
317 | 3 | default: |
|
318 | 6 | return false; |
|
319 | 3 | } |
|
320 | } |
||
321 | |||
322 | 10 | return filter_var($value, FILTER_VALIDATE_BOOLEAN); |
|
323 | } |
||
324 | |||
325 | /** |
||
326 | * Set preserve overprint settings flag |
||
327 | * |
||
328 | * @param bool $preserveOverprintSettings |
||
329 | * |
||
330 | * @return $this |
||
331 | */ |
||
332 | 10 | public function setPreserveOverprintSettings($preserveOverprintSettings) |
|
338 | |||
339 | /** |
||
340 | * Get sRGB profile |
||
341 | * |
||
342 | * @return null|string |
||
343 | */ |
||
344 | 2 | public function getSRgbProfile() |
|
353 | |||
354 | /** |
||
355 | * Set sRGB profile |
||
356 | * |
||
357 | * @param string $sRgbProfile |
||
358 | * |
||
359 | * @return $this |
||
360 | */ |
||
361 | 2 | public function setSRgbProfile($sRgbProfile) |
|
367 | |||
368 | /** |
||
369 | * Get transfer function info |
||
370 | * |
||
371 | * @return string |
||
372 | */ |
||
373 | 8 | public function getTransferFunctionInfo() |
|
382 | |||
383 | /** |
||
384 | * Set transfer function info |
||
385 | * |
||
386 | * @param string $transferFunctionInfo |
||
387 | * |
||
388 | * @throws \InvalidArgumentException |
||
389 | * |
||
390 | * @return $this |
||
391 | */ |
||
392 | 8 | public function setTransferFunctionInfo($transferFunctionInfo) |
|
403 | |||
404 | /** |
||
405 | * Get UCR and BG info |
||
406 | * |
||
407 | * @return string |
||
408 | */ |
||
409 | 10 | public function getUcrAndBgInfo() |
|
410 | { |
||
411 | 10 | $value = $this->getArgumentValue('-dUCRandBGInfo'); |
|
412 | 10 | if (null === $value) { |
|
413 | 10 | switch ($this->getPdfSettings()) { |
|
414 | 10 | case PdfSettings::PRINTER: |
|
415 | 9 | case PdfSettings::PREPRESS: |
|
416 | 4 | return UcrAndBgInfo::PRESERVE; |
|
417 | 3 | default: |
|
418 | 6 | return UcrAndBgInfo::REMOVE; |
|
419 | 3 | } |
|
420 | } |
||
421 | |||
422 | 10 | return ltrim($value, '/'); |
|
423 | } |
||
424 | |||
425 | /** |
||
426 | * Set UCR and BG info |
||
427 | * |
||
428 | * @param string $ucrAndBgInfo |
||
429 | * |
||
430 | * @throws \InvalidArgumentException |
||
431 | * |
||
432 | * @return $this |
||
433 | */ |
||
434 | 12 | public function setUcrAndBgInfo($ucrAndBgInfo) |
|
445 | } |
||
446 |