Completed
Push — master ( 0ae2b7...d7633b )
by Daniel
03:14
created

ColorImageCompressionTrait   B

Complexity

Total Complexity 42

Size/Duplication

Total Lines 325
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

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
B getColorImageDownsampleType() 18 18 6
A setColorImageDownsampleType() 11 11 2
A getColorImageFilter() 9 9 2
A setColorImageFilter() 11 11 2
B getColorImageResolution() 17 17 5
A setColorImageResolution() 6 6 1
A isDownsampleColorImages() 15 15 4
A setDownsampleColorImages() 6 6 2
A isEncodeColorImages() 9 9 2
A setEncodeColorImages() 6 6 2

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