Completed
Push — master ( 55c0ed...187ca7 )
by Daniel
10:59
created

Image   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 94
Duplicated Lines 28.72 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 55.88%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 4
dl 27
loc 94
ccs 19
cts 34
cp 0.5588
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __clone() 0 4 1
A __construct() 0 4 1
B getColorSpace() 16 16 5
A withColorSpace() 11 11 2
A getColorProfile() 0 10 2
A withColorProfile() 0 7 1

How to fix   Duplicated Code   

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:

1
<?php
2
/**
3
 * This file is part of the Magickly project.
4
 *
5
 * @author Daniel Schröder <[email protected]>
6
 */
7
8
namespace GravityMedia\Magickly\Gmagick;
9
10
use GravityMedia\Magickly\Enum\ColorSpace;
11
use GravityMedia\Magickly\Exception\RuntimeException;
12
use GravityMedia\Magickly\Image\AbstractImage;
13
use GravityMedia\Magickly\Image\ColorProfile;
14
use GuzzleHttp\Stream\Utils as StreamUtils;
15
16
/**
17
 * The image class.
18
 *
19
 * @package GravityMedia\Magickly\Gmagick
20
 */
21
class Image extends AbstractImage
22
{
23
    /**
24
     * @var \Gmagick
25
     */
26
    private $gmagick;
27
28
    /**
29
     * @var array
30
     */
31
    protected static $colorSpaceMapping = [
32
        ColorSpace::COLOR_SPACE_RGB => \Gmagick::COLORSPACE_RGB,
33
        ColorSpace::COLOR_SPACE_CMYK => \Gmagick::COLORSPACE_CMYK,
34
        ColorSpace::COLOR_SPACE_GRAYSCALE => \Gmagick::COLORSPACE_GRAY
35
    ];
36
37
    /**
38
     * Create image object.
39
     *
40
     * @param \Gmagick $gmagick
41
     */
42 30
    public function __construct(\Gmagick $gmagick)
43
    {
44 30
        $this->gmagick = $gmagick;
45 30
    }
46
47
    /**
48
     * Clone image object.
49
     */
50 2
    public function __clone()
51
    {
52 2
        $this->gmagick = clone $this->gmagick;
53 2
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 30 View Code Duplication
    public function getColorSpace()
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...
59
    {
60 30
        $this->gmagick->getimagetype(); \Gmagick::IMGTYPE_GRAYSCALE;
61
62 30
        switch ($this->gmagick->getimagecolorspace()) {
63 30
            case \Gmagick::COLORSPACE_RGB:
64 20
            case \Gmagick::COLORSPACE_SRGB:
65 22
                return ColorSpace::COLOR_SPACE_RGB;
66 10
            case \Gmagick::COLORSPACE_CMYK:
67 10
                return ColorSpace::COLOR_SPACE_CMYK;
68
            case \Gmagick::COLORSPACE_GRAY:
69
                return ColorSpace::COLOR_SPACE_GRAYSCALE;
70
            default:
71
                throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported');
72
        }
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 2 View Code Duplication
    public function withColorSpace($colorSpace)
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...
79
    {
80 2
        if (!isset(static::$colorSpaceMapping[$colorSpace])) {
81
            throw new RuntimeException('Only RGB, grayscale and CMYK color space are currently supported');
82
        }
83
84 2
        $image = clone $this;
85 2
        $image->gmagick->setimagecolorspace(static::$colorSpaceMapping[$colorSpace]);
86
87 2
        return $image;
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function getColorProfile()
94
    {
95
        try {
96
            $data = $this->gmagick->getimageprofile('ICM');
97
        } catch (\GmagickException $exception) {
0 ignored issues
show
Bug introduced by
The class GmagickException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
98
            return null;
99
        }
100
101
        return new ColorProfile(StreamUtils::create($data));
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function withColorProfile(ColorProfile $colorProfile)
108
    {
109
        $image = clone $this;
110
        $image->gmagick->setimageprofile('ICM', $colorProfile->getData());
111
112
        return $image;
113
    }
114
}
115