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 |
||
22 | class Image extends AbstractImage |
||
23 | { |
||
24 | /** |
||
25 | * @var \Imagick |
||
26 | */ |
||
27 | private $imagick; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected static $typeMapping = [ |
||
33 | Type::TYPE_BILEVEL => \Imagick::IMGTYPE_BILEVEL, |
||
34 | Type::TYPE_GRAYSCALE => \Imagick::IMGTYPE_GRAYSCALE, |
||
35 | Type::TYPE_PALETTE => \Imagick::IMGTYPE_PALETTE, |
||
36 | Type::TYPE_TRUECOLOR => \Imagick::IMGTYPE_TRUECOLOR, |
||
37 | Type::TYPE_COLORSEPARATION => \Imagick::IMGTYPE_COLORSEPARATION, |
||
38 | ]; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected static $colorSpaceMapping = [ |
||
44 | ColorSpace::COLOR_SPACE_RGB => \Imagick::COLORSPACE_RGB, |
||
45 | ColorSpace::COLOR_SPACE_CMYK => \Imagick::COLORSPACE_CMYK, |
||
46 | ColorSpace::COLOR_SPACE_GRAYSCALE => \Imagick::COLORSPACE_GRAY, |
||
47 | ]; |
||
48 | |||
49 | /** |
||
50 | * Create image object. |
||
51 | * |
||
52 | * @param \Imagick $imagick |
||
53 | */ |
||
54 | 32 | public function __construct(\Imagick $imagick) |
|
58 | |||
59 | /** |
||
60 | * Clone image object. |
||
61 | */ |
||
62 | 2 | public function __clone() |
|
66 | |||
67 | /** |
||
68 | * {@inheritdoc} |
||
69 | */ |
||
70 | public function getStream() |
||
74 | |||
75 | /** |
||
76 | * {@inheritdoc} |
||
77 | */ |
||
78 | 30 | View Code Duplication | public function getFormat() |
87 | |||
88 | /** |
||
89 | * {@inheritdoc} |
||
90 | */ |
||
91 | View Code Duplication | public function withFormat($format) |
|
102 | |||
103 | /** |
||
104 | * {@inheritdoc} |
||
105 | */ |
||
106 | 30 | public function getType() |
|
130 | |||
131 | /** |
||
132 | * {@inheritdoc} |
||
133 | */ |
||
134 | View Code Duplication | public function withType($type) |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 32 | public function getColorSpace() |
|
163 | |||
164 | /** |
||
165 | * {@inheritdoc} |
||
166 | */ |
||
167 | 2 | View Code Duplication | public function withColorSpace($colorSpace) |
178 | |||
179 | /** |
||
180 | * {@inheritdoc} |
||
181 | */ |
||
182 | public function getColorProfile() |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | */ |
||
196 | public function withColorProfile(ColorProfile $colorProfile) |
||
203 | } |
||
204 |
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.