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 |
||
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) |
|
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) |
|
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) |
|
|||
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) |
|
140 | |||
141 | /** |
||
142 | * @return array |
||
143 | */ |
||
144 | 2 | public function getBitmap() |
|
148 | |||
149 | /** |
||
150 | * @return int |
||
151 | */ |
||
152 | 2 | public function getWidth() |
|
156 | |||
157 | /** |
||
158 | * @return int |
||
159 | */ |
||
160 | 2 | public function getHeight() |
|
164 | } |
||
165 |
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.