GrayImageCompressionTrait   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 isAntiAliasGrayImages() 9 9 2
A setAntiAliasGrayImages() 6 6 2
A isAutoFilterGrayImages() 9 9 2
A setAutoFilterGrayImages() 6 6 2
A setDownsampleGrayImages() 6 6 2
A isEncodeGrayImages() 9 9 2
A setEncodeGrayImages() 6 6 2
A getGrayImageDepth() 9 9 2
A setGrayImageDepth() 6 6 1
A getGrayImageDownsampleThreshold() 9 9 2
A setGrayImageDownsampleThreshold() 6 6 1
A setGrayImageDownsampleType() 11 11 2
A getGrayImageFilter() 9 9 2
A setGrayImageFilter() 11 11 2
A setGrayImageResolution() 6 6 1
A isDownsampleGrayImages() 15 15 4
A getGrayImageDownsampleType() 18 18 6
A getGrayImageResolution() 17 17 5

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 GrayImageCompressionTrait 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 GrayImageCompressionTrait, 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 grayscale 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 GrayImageCompressionTrait
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 grayscale images
50
     *
51
     * @return bool
52
     */
53 2
    public function isAntiAliasGrayImages()
54
    {
55 2
        $value = $this->getArgumentValue('-dAntiAliasGrayImages');
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 grayscale images flag
65
     *
66
     * @param bool $antiAliasGrayImages
67
     *
68
     * @return $this
69
     */
70 2
    public function setAntiAliasGrayImages($antiAliasGrayImages)
71
    {
72 2
        $this->setArgument(sprintf('-dAntiAliasGrayImages=%s', $antiAliasGrayImages ? 'true' : 'false'));
73
74 2
        return $this;
75
    }
76
77
    /**
78
     * Whether to auto filter grayscale images
79
     *
80
     * @return bool
81
     */
82 2
    public function isAutoFilterGrayImages()
83
    {
84 2
        $value = $this->getArgumentValue('-dAutoFilterGrayImages');
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 grayscale images flag
94
     *
95
     * @param bool $autoFilterGrayImages
96
     *
97
     * @return $this
98
     */
99 2
    public function setAutoFilterGrayImages($autoFilterGrayImages)
100
    {
101 2
        $this->setArgument(sprintf('-dAutoFilterGrayImages=%s', $autoFilterGrayImages ? 'true' : 'false'));
102
103 2
        return $this;
104
    }
105
106
    /**
107
     * Whether to downsample gray images
108
     *
109
     * @return bool
110
     */
111 10
    public function isDownsampleGrayImages()
112
    {
113 10
        $value = $this->getArgumentValue('-dDownsampleGrayImages');
114 10
        if (null === $value) {
115 10
            switch ($this->getPdfSettings()) {
116 10
                case PdfSettings::SCREEN:
117 9
                case PdfSettings::EBOOK:
118 4
                    return true;
119 3
                default:
120 6
                    return false;
121 3
            }
122
        }
123
124 10
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
125
    }
126
127
    /**
128
     * Set downsample gray images flag
129
     *
130
     * @param bool $downsampleGrayImages
131
     *
132
     * @return $this
133
     */
134 10
    public function setDownsampleGrayImages($downsampleGrayImages)
135
    {
136 10
        $this->setArgument(sprintf('-dDownsampleGrayImages=%s', $downsampleGrayImages ? 'true' : 'false'));
137
138 10
        return $this;
139
    }
140
141
    /**
142
     * Whether to encode grayscale images
143
     *
144
     * @return bool
145
     */
146 2
    public function isEncodeGrayImages()
147
    {
148 2
        $value = $this->getArgumentValue('-dEncodeGrayImages');
149 2
        if (null === $value) {
150 2
            return true;
151
        }
152
153 2
        return filter_var($value, FILTER_VALIDATE_BOOLEAN);
154
    }
155
156
    /**
157
     * Set encode grayscale images flag
158
     *
159
     * @param bool $encodeGrayImages
160
     *
161
     * @return $this
162
     */
163 2
    public function setEncodeGrayImages($encodeGrayImages)
164
    {
165 2
        $this->setArgument(sprintf('-dEncodeGrayImages=%s', $encodeGrayImages ? 'true' : 'false'));
166
167 2
        return $this;
168
    }
169
170
    /**
171
     * Get gray image depth
172
     *
173
     * @return int
174
     */
175 2
    public function getGrayImageDepth()
176
    {
177 2
        $value = $this->getArgumentValue('-dGrayImageDepth');
178 2
        if (null === $value) {
179 2
            return -1;
180
        }
181
182 2
        return intval($value);
183
    }
184
185
    /**
186
     * Set gray image depth
187
     *
188
     * @param int $grayImageDepth
189
     *
190
     * @return $this
191
     */
192 2
    public function setGrayImageDepth($grayImageDepth)
193
    {
194 2
        $this->setArgument(sprintf('-dGrayImageDepth=%s', $grayImageDepth));
195
196 2
        return $this;
197
    }
198
199
    /**
200
     * Get gray image downsample threshold
201
     *
202
     * @return float
203
     */
204 2
    public function getGrayImageDownsampleThreshold()
205
    {
206 2
        $value = $this->getArgumentValue('-dGrayImageDownsampleThreshold');
207 2
        if (null === $value) {
208 2
            return 1.5;
209
        }
210
211 2
        return floatval($value);
212
    }
213
214
    /**
215
     * Set gray image downsample threshold
216
     *
217
     * @param float $grayImageDownsampleThreshold
218
     *
219
     * @return $this
220
     */
221 2
    public function setGrayImageDownsampleThreshold($grayImageDownsampleThreshold)
222
    {
223 2
        $this->setArgument(sprintf('-dGrayImageDownsampleThreshold=%s', $grayImageDownsampleThreshold));
224
225 2
        return $this;
226
    }
227
228
    /**
229
     * Get gray image downsample type
230
     *
231
     * @return string
232
     */
233 10
    public function getGrayImageDownsampleType()
234
    {
235 10
        $value = $this->getArgumentValue('-dGrayImageDownsampleType');
236 10
        if (null === $value) {
237 10
            switch ($this->getPdfSettings()) {
238 10
                case PdfSettings::SCREEN:
239 2
                    return ImageDownsampleType::AVERAGE;
240 8
                case PdfSettings::EBOOK:
241 7
                case PdfSettings::PRINTER:
242 6
                case PdfSettings::PREPRESS:
243 6
                    return ImageDownsampleType::BICUBIC;
244 1
                default:
245 2
                    return ImageDownsampleType::SUBSAMPLE;
246 1
            }
247
        }
248
249 10
        return ltrim($value, '/');
250
    }
251
252
    /**
253
     * Set gray image downsample type
254
     *
255
     * @param string $grayImageDownsampleType
256
     *
257
     * @throws \InvalidArgumentException
258
     *
259
     * @return $this
260
     */
261 12
    public function setGrayImageDownsampleType($grayImageDownsampleType)
262
    {
263 12
        $grayImageDownsampleType = ltrim($grayImageDownsampleType, '/');
264 12
        if (!in_array($grayImageDownsampleType, ImageDownsampleType::values())) {
265 2
            throw new \InvalidArgumentException('Invalid grayscale image downsample type argument');
266
        }
267
268 10
        $this->setArgument(sprintf('-dGrayImageDownsampleType=/%s', $grayImageDownsampleType));
269
270 10
        return $this;
271
    }
272
273
    /**
274
     * Get gray image filter
275
     *
276
     * @return string
277
     */
278 6
    public function getGrayImageFilter()
279
    {
280 6
        $value = $this->getArgumentValue('-dGrayImageFilter');
281 6
        if (null === $value) {
282 2
            return ColorAndGrayImageFilter::DCT_ENCODE;
283
        }
284
285 4
        return ltrim($value, '/');
286
    }
287
288
    /**
289
     * Set gray image filter
290
     *
291
     * @param string $grayImageFilter
292
     *
293
     * @throws \InvalidArgumentException
294
     *
295
     * @return $this
296
     */
297 6
    public function setGrayImageFilter($grayImageFilter)
298
    {
299 6
        $grayImageFilter = ltrim($grayImageFilter, '/');
300 6
        if (!in_array($grayImageFilter, ColorAndGrayImageFilter::values())) {
301 2
            throw new \InvalidArgumentException('Invalid grayscale image filter argument');
302
        }
303
304 4
        $this->setArgument(sprintf('-dGrayImageFilter=/%s', $grayImageFilter));
305
306 4
        return $this;
307
    }
308
309
    /**
310
     * Get gray image resolution
311
     *
312
     * @return int
313
     */
314 10
    public function getGrayImageResolution()
315
    {
316 10
        $value = $this->getArgumentValue('-dGrayImageResolution');
317 10
        if (null === $value) {
318 10
            switch ($this->getPdfSettings()) {
319 10
                case PdfSettings::EBOOK:
320 2
                    return 150;
321 8
                case PdfSettings::PRINTER:
322 7
                case PdfSettings::PREPRESS:
323 4
                    return 300;
324 2
                default:
325 4
                    return 72;
326 2
            }
327
        }
328
329 10
        return intval($value);
330
    }
331
332
    /**
333
     * Set gray image resolution
334
     *
335
     * @param int $grayImageResolution
336
     *
337
     * @return $this
338
     */
339 10
    public function setGrayImageResolution($grayImageResolution)
340
    {
341 10
        $this->setArgument(sprintf('-dGrayImageResolution=%s', $grayImageResolution));
342
343 10
        return $this;
344
    }
345
}
346