ColorImageCompressionTrait   A
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 325
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 42
lcom 2
cbo 2
dl 325
loc 325
ccs 101
cts 101
cp 1
rs 9.0399
c 0
b 0
f 0

21 Methods

Rating   Name   Duplication   Size   Complexity  
getArgumentValue() 1 1 ?
setArgument() 1 1 ?
getPdfSettings() 1 1 ?
A isAntiAliasColorImages() 9 9 2
A setAntiAliasColorImages() 6 6 2
A isAutoFilterColorImages() 9 9 2
A setAutoFilterColorImages() 6 6 2
A getColorImageDepth() 9 9 2
A setColorImageDepth() 6 6 1
A getColorImageDownsampleThreshold() 9 9 2
A setColorImageDownsampleThreshold() 6 6 1
A setColorImageDownsampleType() 11 11 2
A getColorImageFilter() 9 9 2
A setColorImageFilter() 11 11 2
A setColorImageResolution() 6 6 1
A setDownsampleColorImages() 6 6 2
A isEncodeColorImages() 9 9 2
A setEncodeColorImages() 6 6 2
A getColorImageDownsampleType() 18 18 6
A getColorImageResolution() 17 17 5
A isDownsampleColorImages() 15 15 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

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 Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

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
2
/**
3
 * This file is part of the Ghostscript package
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Ghostscript\Device\DistillerParameters;
9
10
use GravityMedia\Ghostscript\Enum\ColorAndGrayImageFilter;
11
use GravityMedia\Ghostscript\Enum\ImageDownsampleType;
12
use GravityMedia\Ghostscript\Enum\PdfSettings;
13
14
/**
15
 * The color image compression distiller parameters trait.
16
 *
17
 * @package GravityMedia\Ghostscript\Device\DistillerParameters
18
 *
19
 * @link    http://ghostscript.com/doc/current/Ps2pdf.htm
20
 */
21 View Code Duplication
trait ColorImageCompressionTrait
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

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.

Loading history...
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
     * Whether to anti alias color images
50
     *
51
     * @return bool
52
     */
53 2
    public function isAntiAliasColorImages()
54
    {
55 2
        $value = $this->getArgumentValue('-dAntiAliasColorImages');
56 2
        if (null === $value) {
57 2
            return false;
58
        }
59
60 2
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
61
    }
62
63
    /**
64
     * Set anti alias color images flag
65
     *
66
     * @param bool $antiAliasColorImages
67
     *
68
     * @return $this
69
     */
70 2
    public function setAntiAliasColorImages($antiAliasColorImages)
71
    {
72 2
        $this->setArgument(sprintf('-dAntiAliasColorImages=%s', $antiAliasColorImages ? 'true' : 'false'));
73
74 2
        return $this;
75
    }
76
77
    /**
78
     * Whether to auto filter color images
79
     *
80
     * @return bool
81
     */
82 2
    public function isAutoFilterColorImages()
83
    {
84 2
        $value = $this->getArgumentValue('-dAutoFilterColorImages');
85 2
        if (null === $value) {
86 2
            return true;
87
        }
88
89 2
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
90
    }
91
92
    /**
93
     * Set auto filter color images flag
94
     *
95
     * @param bool $autoFilterColorImages
96
     *
97
     * @return $this
98
     */
99 2
    public function setAutoFilterColorImages($autoFilterColorImages)
100
    {
101 2
        $this->setArgument(sprintf('-dAutoFilterColorImages=%s', $autoFilterColorImages ? 'true' : 'false'));
102
103 2
        return $this;
104
    }
105
106
    /**
107
     * Get color image depth
108
     *
109
     * @return int
110
     */
111 2
    public function getColorImageDepth()
112
    {
113 2
        $value = $this->getArgumentValue('-dColorImageDepth');
114 2
        if (null === $value) {
115 2
            return -1;
116
        }
117
118 2
        return intval($value);
119
    }
120
121
    /**
122
     * Set color image depth
123
     *
124
     * @param int $colorImageDepth
125
     *
126
     * @return $this
127
     */
128 2
    public function setColorImageDepth($colorImageDepth)
129
    {
130 2
        $this->setArgument(sprintf('-dColorImageDepth=%s', $colorImageDepth));
131
132 2
        return $this;
133
    }
134
135
    /**
136
     * Get color image downsample threshold
137
     *
138
     * @return float
139
     */
140 2
    public function getColorImageDownsampleThreshold()
141
    {
142 2
        $value = $this->getArgumentValue('-dColorImageDownsampleThreshold');
143 2
        if (null === $value) {
144 2
            return 1.5;
145
        }
146
147 2
        return floatval($value);
148
    }
149
150
    /**
151
     * Set color image downsample threshold
152
     *
153
     * @param float $colorImageDownsampleThreshold
154
     *
155
     * @return $this
156
     */
157 2
    public function setColorImageDownsampleThreshold($colorImageDownsampleThreshold)
158
    {
159 2
        $this->setArgument(sprintf('-dColorImageDownsampleThreshold=%s', $colorImageDownsampleThreshold));
160
161 2
        return $this;
162
    }
163
164
    /**
165
     * Get color image downsample type
166
     *
167
     * @return string
168
     */
169 10
    public function getColorImageDownsampleType()
170
    {
171 10
        $value = $this->getArgumentValue('-dColorImageDownsampleType');
172 10
        if (null === $value) {
173 10
            switch ($this->getPdfSettings()) {
174 10
                case PdfSettings::SCREEN:
175 9
                case PdfSettings::EBOOK:
176 8
                case PdfSettings::PRINTER:
177 6
                    return ImageDownsampleType::AVERAGE;
178 4
                case PdfSettings::PREPRESS:
179 2
                    return ImageDownsampleType::BICUBIC;
180 1
                default:
181 2
                    return ImageDownsampleType::SUBSAMPLE;
182 1
            }
183
        }
184
185 10
        return ltrim($value, '/');
186
    }
187
188
    /**
189
     * Set color image downsample type
190
     *
191
     * @param string $colorImageDownsampleType
192
     *
193
     * @throws \InvalidArgumentException
194
     *
195
     * @return $this
196
     */
197 12
    public function setColorImageDownsampleType($colorImageDownsampleType)
198
    {
199 12
        $colorImageDownsampleType = ltrim($colorImageDownsampleType, '/');
200 12
        if (!in_array($colorImageDownsampleType, ImageDownsampleType::values())) {
201 2
            throw new \InvalidArgumentException('Invalid color image downsample type argument');
202
        }
203
204 10
        $this->setArgument(sprintf('-dColorImageDownsampleType=/%s', $colorImageDownsampleType));
205
206 10
        return $this;
207
    }
208
209
    /**
210
     * Get color image filter
211
     *
212
     * @return string
213
     */
214 6
    public function getColorImageFilter()
215
    {
216 6
        $value = $this->getArgumentValue('-dColorImageFilter');
217 6
        if (null === $value) {
218 2
            return ColorAndGrayImageFilter::DCT_ENCODE;
219
        }
220
221 4
        return ltrim($value, '/');
222
    }
223
224
    /**
225
     * Set color image filter
226
     *
227
     * @param string $colorImageFilter
228
     *
229
     * @throws \InvalidArgumentException
230
     *
231
     * @return $this
232
     */
233 6
    public function setColorImageFilter($colorImageFilter)
234
    {
235 6
        $colorImageFilter = ltrim($colorImageFilter, '/');
236 6
        if (!in_array($colorImageFilter, ColorAndGrayImageFilter::values())) {
237 2
            throw new \InvalidArgumentException('Invalid color image filter argument');
238
        }
239
240 4
        $this->setArgument(sprintf('-dColorImageFilter=/%s', $colorImageFilter));
241
242 4
        return $this;
243
    }
244
245
    /**
246
     * Get color image resolution
247
     *
248
     * @return int
249
     */
250 10
    public function getColorImageResolution()
251
    {
252 10
        $value = $this->getArgumentValue('-dColorImageResolution');
253 10
        if (null === $value) {
254 10
            switch ($this->getPdfSettings()) {
255 10
                case PdfSettings::EBOOK:
256 2
                    return 150;
257 8
                case PdfSettings::PRINTER:
258 7
                case PdfSettings::PREPRESS:
259 4
                    return 300;
260 2
                default:
261 4
                    return 72;
262 2
            }
263
        }
264
265 10
        return intval($value);
266
    }
267
268
    /**
269
     * Set color image resolution
270
     *
271
     * @param int $colorImageResolution
272
     *
273
     * @return $this
274
     */
275 10
    public function setColorImageResolution($colorImageResolution)
276
    {
277 10
        $this->setArgument(sprintf('-dColorImageResolution=%s', $colorImageResolution));
278
279 10
        return $this;
280
    }
281
282
    /**
283
     * Whether to downsample color images
284
     *
285
     * @return bool
286
     */
287 10
    public function isDownsampleColorImages()
288
    {
289 10
        $value = $this->getArgumentValue('-dDownsampleColorImages');
290 10
        if (null === $value) {
291 10
            switch ($this->getPdfSettings()) {
292 10
                case PdfSettings::SCREEN:
293 9
                case PdfSettings::EBOOK:
294 4
                    return true;
295 3
                default:
296 6
                    return false;
297 3
            }
298
        }
299
300 10
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
301
    }
302
303
    /**
304
     * Set downsample color images flag
305
     *
306
     * @param bool $downsampleColorImages
307
     *
308
     * @return $this
309
     */
310 10
    public function setDownsampleColorImages($downsampleColorImages)
311
    {
312 10
        $this->setArgument(sprintf('-dDownsampleColorImages=%s', $downsampleColorImages ? 'true' : 'false'));
313
314 10
        return $this;
315
    }
316
317
    /**
318
     * Whether to encode color images
319
     *
320
     * @return bool
321
     */
322 2
    public function isEncodeColorImages()
323
    {
324 2
        $value = $this->getArgumentValue('-dEncodeColorImages');
325 2
        if (null === $value) {
326 2
            return true;
327
        }
328
329 2
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
330
    }
331
332
    /**
333
     * Set encode color images flag
334
     *
335
     * @param bool $encodeColorImages
336
     *
337
     * @return $this
338
     */
339 2
    public function setEncodeColorImages($encodeColorImages)
340
    {
341 2
        $this->setArgument(sprintf('-dEncodeColorImages=%s', $encodeColorImages ? 'true' : 'false'));
342
343 2
        return $this;
344
    }
345
}
346