Completed
Push — master ( ea43a1...cefa14 )
by Daniel
05:36
created

DistillerParametersTrait   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 451
Duplicated Lines 9.76 %

Coupling/Cohesion

Components 2
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 47
c 1
b 0
f 1
lcom 2
cbo 0
dl 44
loc 451
ccs 0
cts 114
cp 0
rs 8.439

32 Methods

Rating   Name   Duplication   Size   Complexity  
getArgumentValue() 0 1 ?
setArgument() 0 1 ?
A getAlwaysEmbed() 0 9 2
A setAlwaysEmbed() 0 8 1
A isAntiAliasColorImages() 0 4 1
A setAntiAliasColorImages() 0 6 2
A isAntiAliasGrayImages() 0 4 1
A setAntiAliasGrayImages() 0 6 2
A isAntiAliasMonoImages() 0 4 1
A setAntiAliasMonoImages() 0 6 2
A isAscii85EncodePages() 0 4 1
A setAscii85EncodePages() 0 6 2
A isAutoFilterColorImages() 0 4 1
A setAutoFilterColorImages() 0 6 2
A isAutoFilterGrayImages() 0 4 1
A setAutoFilterGrayImages() 0 6 2
A isAutoPositionEpsFiles() 0 4 1
A setAutoPositionEpsFiles() 0 6 2
A getAutoRotatePages() 0 9 2
A setAutoRotatePages() 15 15 2
A getBinding() 0 9 2
A setBinding() 14 14 2
A getCalCmykProfile() 0 9 2
A setCalCmykProfile() 0 6 1
A getCalGrayProfile() 0 9 2
A setCalGrayProfile() 0 6 1
A getCalRgbProfile() 0 9 2
A setCalRgbProfile() 0 6 1
A getCannotEmbedFontPolicy() 0 9 2
A setCannotEmbedFontPolicy() 15 15 2
A getCompatibilityLevel() 0 4 1
A setCompatibilityLevel() 0 6 1

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 DistillerParametersTrait 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 DistillerParametersTrait, 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;
9
10
/**
11
 * The distiller parameters trait
12
 *
13
 * @package GravityMedia\Ghostscript\Devices
14
 */
15
trait DistillerParametersTrait
16
{
17
    /**
18
     * Get argument value
19
     *
20
     * @param string $name
21
     *
22
     * @return string
23
     */
24
    abstract function getArgumentValue($name);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
25
26
    /**
27
     * Set argument
28
     *
29
     * @param string $argument
30
     *
31
     * @return $this
32
     */
33
    abstract function setArgument($argument);
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
34
35
    /**
36
     * Get always embed
37
     *
38
     * @return array
39
     */
40
    public function getAlwaysEmbed()
41
    {
42
        $value = $this->getArgumentValue('-dAlwaysEmbed');
43
        if (null === $value) {
44
            return [];
45
        }
46
47
        return explode(' /', substr($value, 2, -1));
48
    }
49
50
    /**
51
     * Set always embed
52
     *
53
     * @param array $alwaysEmbed
54
     *
55
     * @return $this
56
     */
57
    public function setAlwaysEmbed(array $alwaysEmbed)
58
    {
59
        $this->setArgument('-dAlwaysEmbed=[' . implode(' ', array_map(function ($fontName) {
60
                return '/' . ltrim($fontName, '/');
61
            }, $alwaysEmbed)) . ']');
62
63
        return $this;
64
    }
65
66
    /**
67
     * Whether to anti alias color images
68
     *
69
     * @return bool
70
     */
71
    public function isAntiAliasColorImages()
72
    {
73
        return filter_var($this->getArgumentValue('-dAntiAliasColorImages'), FILTER_VALIDATE_BOOLEAN);
74
    }
75
76
    /**
77
     * Set anti alias color images flag
78
     *
79
     * @param bool $antiAliasColorImages
80
     *
81
     * @return $this
82
     */
83
    public function setAntiAliasColorImages($antiAliasColorImages)
84
    {
85
        $this->setArgument('-dAntiAliasColorImages=' . ($antiAliasColorImages ? 'true' : 'false'));
86
87
        return $this;
88
    }
89
90
    /**
91
     * Whether to anti alias gray images
92
     *
93
     * @return bool
94
     */
95
    public function isAntiAliasGrayImages()
96
    {
97
        return filter_var($this->getArgumentValue('-dAntiAliasGrayImages'), FILTER_VALIDATE_BOOLEAN);
98
    }
99
100
    /**
101
     * Set anti alias gray images flag
102
     *
103
     * @param bool $antiAliasGrayImages
104
     *
105
     * @return $this
106
     */
107
    public function setAntiAliasGrayImages($antiAliasGrayImages)
108
    {
109
        $this->setArgument('-dAntiAliasGrayImages=' . ($antiAliasGrayImages ? 'true' : 'false'));
110
111
        return $this;
112
    }
113
114
    /**
115
     * Whether to anti alias mono images
116
     *
117
     * @return bool
118
     */
119
    public function isAntiAliasMonoImages()
120
    {
121
        return filter_var($this->getArgumentValue('-dAntiAliasMonoImages'), FILTER_VALIDATE_BOOLEAN);
122
    }
123
124
    /**
125
     * Set anti alias mono images flag
126
     *
127
     * @param bool $antiAliasMonoImages
128
     *
129
     * @return $this
130
     */
131
    public function setAntiAliasMonoImages($antiAliasMonoImages)
132
    {
133
        $this->setArgument('-dAntiAliasMonoImages=' . ($antiAliasMonoImages ? 'true' : 'false'));
134
135
        return $this;
136
    }
137
138
    /**
139
     * Whether ASCII85 encode pages
140
     *
141
     * @return bool
142
     */
143
    public function isAscii85EncodePages()
144
    {
145
        return filter_var($this->getArgumentValue('-dASCII85EncodePages'), FILTER_VALIDATE_BOOLEAN);
146
    }
147
148
    /**
149
     * Set ASCII85 encode pages flag
150
     *
151
     * @param bool $ascii85EncodePages
152
     *
153
     * @return $this
154
     */
155
    public function setAscii85EncodePages($ascii85EncodePages)
156
    {
157
        $this->setArgument('-dASCII85EncodePages=' . ($ascii85EncodePages ? 'true' : 'false'));
158
159
        return $this;
160
    }
161
162
    /**
163
     * Whether to auto filter color images
164
     *
165
     * @return bool
166
     */
167
    public function isAutoFilterColorImages()
168
    {
169
        return filter_var($this->getArgumentValue('-dAutoFilterColorImages'), FILTER_VALIDATE_BOOLEAN);
170
    }
171
172
    /**
173
     * Set auto filter color images flag
174
     *
175
     * @param bool $autoFilterColorImages
176
     *
177
     * @return $this
178
     */
179
    public function setAutoFilterColorImages($autoFilterColorImages)
180
    {
181
        $this->setArgument('-dAutoFilterColorImages=' . ($autoFilterColorImages ? 'true' : 'false'));
182
183
        return $this;
184
    }
185
186
    /**
187
     * Whether to auto filter gray images
188
     *
189
     * @return bool
190
     */
191
    public function isAutoFilterGrayImages()
192
    {
193
        return filter_var($this->getArgumentValue('-dAutoFilterGrayImages'), FILTER_VALIDATE_BOOLEAN);
194
    }
195
196
    /**
197
     * Set auto filter gray images flag
198
     *
199
     * @param bool $autoFilterGrayImages
200
     *
201
     * @return $this
202
     */
203
    public function setAutoFilterGrayImages($autoFilterGrayImages)
204
    {
205
        $this->setArgument('-dAutoFilterGrayImages=' . ($autoFilterGrayImages ? 'true' : 'false'));
206
207
        return $this;
208
    }
209
210
    /**
211
     * Whether to auto position EPS files
212
     *
213
     * @return bool
214
     */
215
    public function isAutoPositionEpsFiles()
216
    {
217
        return filter_var($this->getArgumentValue('-dAutoPositionEPSFiles'), FILTER_VALIDATE_BOOLEAN);
218
    }
219
220
    /**
221
     * Set auto position EPS files flag
222
     *
223
     * @param bool $autoPositionEpsFiles
224
     *
225
     * @return $this
226
     */
227
    public function setAutoPositionEpsFiles($autoPositionEpsFiles)
228
    {
229
        $this->setArgument('-dAutoPositionEPSFiles=' . ($autoPositionEpsFiles ? 'true' : 'false'));
230
231
        return $this;
232
    }
233
234
    /**
235
     * Get auto rotate pages
236
     *
237
     * @return string
238
     */
239
    public function getAutoRotatePages()
240
    {
241
        $value = $this->getArgumentValue('-dAutoRotatePages');
242
        if (null === $value) {
243
            return null;
244
        }
245
246
        return substr($value, 1);
247
    }
248
249
250
    /**
251
     * Set auto rotate pages
252
     *
253
     * @param string $autoRotatePages
254
     *
255
     * @param \InvalidArgumentException
256
     *
257
     * @return $this
258
     */
259 View Code Duplication
    public function setAutoRotatePages($autoRotatePages)
0 ignored issues
show
Duplication introduced by
This method 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...
260
    {
261
        if (!in_array($autoRotatePages, array(
262
            DistillerParametersInterface::AUTO_ROTATE_PAGES_NONE,
263
            DistillerParametersInterface::AUTO_ROTATE_PAGES_ALL,
264
            DistillerParametersInterface::AUTO_ROTATE_PAGES_PAGE_BY_PAGE
265
        ))
266
        ) {
267
            throw new \InvalidArgumentException('Invalid auto rotate pages argument');
268
        }
269
270
        $this->setArgument('-dAutoRotatePages=/' . $autoRotatePages);
271
272
        return $this;
273
    }
274
275
    /**
276
     * Get binding
277
     *
278
     * @return string
279
     */
280
    public function getBinding()
281
    {
282
        $value = $this->getArgumentValue('-dBinding');
283
        if (null === $value) {
284
            return null;
285
        }
286
287
        return substr($value, 1);
288
    }
289
290
291
    /**
292
     * Set binding
293
     *
294
     * @param string $binding
295
     *
296
     * @param \InvalidArgumentException
297
     *
298
     * @return $this
299
     */
300 View Code Duplication
    public function setBinding($binding)
0 ignored issues
show
Duplication introduced by
This method 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...
301
    {
302
        if (!in_array($binding, array(
303
            DistillerParametersInterface::BINDING_LEFT,
304
            DistillerParametersInterface::BINDING_RIGHT
305
        ))
306
        ) {
307
            throw new \InvalidArgumentException('Invalid binding argument');
308
        }
309
310
        $this->setArgument('-dBinding=/' . $binding);
311
312
        return $this;
313
    }
314
315
    /**
316
     * Get cal CMYK profile
317
     *
318
     * @return null|string
319
     */
320
    public function getCalCmykProfile()
321
    {
322
        $value = $this->getArgumentValue('-dCalCMYKProfile');
323
        if (null === $value) {
324
            return null;
325
        }
326
327
        return substr($value, 1, -1);
328
    }
329
330
    /**
331
     * Set cal CMYK profile
332
     *
333
     * @param string $valCmykProfile
334
     *
335
     * @return $this
336
     */
337
    public function setCalCmykProfile($valCmykProfile)
338
    {
339
        $this->setArgument('-dCalCMYKProfile=(' . $valCmykProfile . ')');
340
341
        return $this;
342
    }
343
344
    /**
345
     * Get cal gray profile
346
     *
347
     * @return null|string
348
     */
349
    public function getCalGrayProfile()
350
    {
351
        $value = $this->getArgumentValue('-dCalGrayProfile');
352
        if (null === $value) {
353
            return null;
354
        }
355
356
        return substr($value, 1, -1);
357
    }
358
359
    /**
360
     * Set cal gray profile
361
     *
362
     * @param string $valGrayProfile
363
     *
364
     * @return $this
365
     */
366
    public function setCalGrayProfile($valGrayProfile)
367
    {
368
        $this->setArgument('-dCalGrayProfile=(' . $valGrayProfile . ')');
369
370
        return $this;
371
    }
372
373
    /**
374
     * Get cal RGB profile
375
     *
376
     * @return null|string
377
     */
378
    public function getCalRgbProfile()
379
    {
380
        $value = $this->getArgumentValue('-dCalRGBProfile');
381
        if (null === $value) {
382
            return null;
383
        }
384
385
        return substr($value, 1, -1);
386
    }
387
388
    /**
389
     * Set cal RGB profile
390
     *
391
     * @param string $valRgbProfile
392
     *
393
     * @return $this
394
     */
395
    public function setCalRgbProfile($valRgbProfile)
396
    {
397
        $this->setArgument('-dCalRGBProfile=(' . $valRgbProfile . ')');
398
399
        return $this;
400
    }
401
402
    /**
403
     * Get cannot embed font policy
404
     *
405
     * @return string
406
     */
407
    public function getCannotEmbedFontPolicy()
408
    {
409
        $value = $this->getArgumentValue('-dCannotEmbedFontPolicy');
410
        if (null === $value) {
411
            return null;
412
        }
413
414
        return substr($value, 1);
415
    }
416
417
    /**
418
     * Set cannot embed font policy
419
     *
420
     * @param string $cannotEmbedFontPolicy
421
     *
422
     * @param \InvalidArgumentException
423
     *
424
     * @return $this
425
     */
426 View Code Duplication
    public function setCannotEmbedFontPolicy($cannotEmbedFontPolicy)
0 ignored issues
show
Duplication introduced by
This method 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...
427
    {
428
        if (!in_array($cannotEmbedFontPolicy, array(
429
            DistillerParametersInterface::CANNOT_EMBED_FONT_POLICY_OK,
430
            DistillerParametersInterface::CANNOT_EMBED_FONT_POLICY_WARNING,
431
            DistillerParametersInterface::CANNOT_EMBED_FONT_POLICY_ERROR
432
        ))
433
        ) {
434
            throw new \InvalidArgumentException('Invalid cannot embed font policy argument');
435
        }
436
437
        $this->setArgument('-dCannotEmbedFontPolicy=/' . $cannotEmbedFontPolicy);
438
439
        return $this;
440
    }
441
442
    /**
443
     * Get compatibility level
444
     *
445
     * @return null|string
446
     */
447
    public function getCompatibilityLevel()
448
    {
449
        return $this->getArgumentValue('-dCompatibilityLevel');
450
    }
451
452
    /**
453
     * Set compatibility level
454
     *
455
     * @param string $compatibilityLevel
456
     *
457
     * @return $this
458
     */
459
    public function setCompatibilityLevel($compatibilityLevel)
460
    {
461
        $this->setArgument('-dCompatibilityLevel=' . $compatibilityLevel);
462
463
        return $this;
464
    }
465
}
466