Image   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 157
Duplicated Lines 7.64 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 15
c 1
b 0
f 0
lcom 1
cbo 2
dl 12
loc 157
ccs 57
cts 57
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 14 2
A createBitmap() 0 20 3
A cloneWith() 0 7 1
A getBitmap() 0 4 1
A getWidth() 0 4 1
A getHeight() 0 4 1
B createImage() 0 23 5
A difference() 12 12 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
namespace Undemanding\Difference;
4
5
use InvalidArgumentException;
6
use Undemanding\Difference\Transformation;
7
8
class Image
9
{
10
    /**
11
     * @var array
12
     */
13
    private $bitmap;
14
15
    /**
16
     * @var int
17
     */
18
    private $width;
19
20
    /**
21
     * @var int
22
     */
23
    private $height;
24
25
    /**
26
     * @param string $path
27
     */
28 4
    public function __construct($path)
29
    {
30 4
        if (!file_exists($path)) {
31 1
            throw new InvalidArgumentException("image not found");
32
        }
33
34 3
        $image = $this->createImage($path);
35
36 2
        $this->bitmap = $this->createBitmap(
37 2
            $image,
38 2
            $this->width = imagesx($image),
39 2
            $this->height = imagesy($image)
40 2
        );
41 2
    }
42
43
    /**
44
     * Create new image resource from image path.
45
     *
46
     * @param string $path
47
     *
48
     * @return resource
49
     *
50
     * @throws InvalidArgumentException
51
     */
52 3
    private function createImage($path)
53
    {
54 3
        $info = getimagesize($path);
55 3
        $type = $info[2];
56
57 3
        $image = null;
58
59 3
        if ($type == IMAGETYPE_JPEG) {
60 1
            $image = imagecreatefromjpeg($path);
61 1
        }
62 3
        if ($type == IMAGETYPE_GIF) {
63 1
            $image = imagecreatefromgif($path);
64 1
        }
65 3
        if ($type == IMAGETYPE_PNG) {
66 2
            $image = imagecreatefrompng($path);
67 2
        }
68
69 3
        if (!$image) {
70 1
            throw new InvalidArgumentException("image invalid");
71
        }
72
73 2
        return $image;
74
    }
75
76
    /**
77
     * Creates new bitmap from image resource.
78
     *
79
     * @param resource $image
80
     * @param int $width
81
     * @param int $height
82
     *
83
     * @return array
84
     */
85 2
    private function createBitmap($image, $width, $height)
86
    {
87 2
        $bitmap = [];
88
89 2
        for ($y = 0; $y < $height; $y++) {
90 2
            $bitmap[$y] = [];
91
92 2
            for ($x = 0; $x < $width; $x++) {
93 2
                $color = imagecolorat($image, $x, $y);
94
95 2
                $bitmap[$y][$x] = [
96 2
                    "r" => ($color >> 16) & 0xFF,
97 2
                    "g" => ($color >> 8) & 0xFF,
98
                    "b" => $color & 0xFF
99 2
                ];
100 2
            }
101 2
        }
102
103 2
        return $bitmap;
104
    }
105
106
    /**
107
     * Difference between two bitmap states.
108
     *
109
     * @param Image $image
110
     * @param callable $method
111
     *
112
     * @return Difference
113
     */
114 1 View Code Duplication
    public function difference(Image $image, callable $method)
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...
115
    {
116 1
        $transformation = new Transformation\Difference();
117
118 1
        $bitmap = $transformation(
119 1
            $this->bitmap, $image->bitmap, $this->width, $this->height, $method
120 1
        );
121
122 1
        return new Difference(
123 1
            $this->cloneWith("bitmap", $bitmap)
124 1
        );
125
    }
126
127
    /**
128
     * @param string $property
129
     * @param mixed $value
130
     *
131
     * @return Image
132
     */
133 1
    private function cloneWith($property, $value)
134
    {
135 1
        $clone = clone $this;
136 1
        $clone->$property = $value;
137
138 1
        return $clone;
139
    }
140
141
    /**
142
     * @return array
143
     */
144 2
    public function getBitmap()
145
    {
146 2
        return $this->bitmap;
147
    }
148
149
    /**
150
     * @return int
151
     */
152 2
    public function getWidth()
153
    {
154 2
        return $this->width;
155
    }
156
157
    /**
158
     * @return int
159
     */
160 2
    public function getHeight()
161
    {
162 2
        return $this->height;
163
    }
164
}
165