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 |
||
15 | class Crop extends IMethod |
||
16 | { |
||
17 | protected $_width; |
||
18 | protected $_height; |
||
19 | protected $_x; |
||
20 | protected $_y; |
||
21 | |||
22 | protected $_destWidth; |
||
23 | protected $_destHeight; |
||
24 | protected $_destX = 0.0; |
||
25 | protected $_destY = 0.0; |
||
26 | |||
27 | /** |
||
28 | * @var null|array |
||
29 | */ |
||
30 | protected $_backgroundColor = null; |
||
31 | |||
32 | public function init() |
||
34 | |||
35 | /** |
||
36 | * Set the width |
||
37 | * |
||
38 | * @param int $width |
||
39 | * @return self |
||
40 | */ |
||
41 | public function SetWidth($width) |
||
47 | |||
48 | /** |
||
49 | * Set the height |
||
50 | * |
||
51 | * @param int $height |
||
52 | * @return self |
||
53 | */ |
||
54 | public function SetHeight($height) |
||
60 | |||
61 | /** |
||
62 | * Set the x |
||
63 | * |
||
64 | * @param int $x |
||
65 | * @return self |
||
66 | */ |
||
67 | public function SetX($x) |
||
72 | |||
73 | /** |
||
74 | * Set the y |
||
75 | * |
||
76 | * @param int $y |
||
77 | * @return self |
||
78 | */ |
||
79 | public function SetY($y) |
||
84 | |||
85 | /** |
||
86 | * If neccesary, fill the background color |
||
87 | * with this color |
||
88 | * |
||
89 | * @param int $r Red |
||
90 | * @param int $g Green |
||
91 | * @param int $b Blue |
||
92 | * |
||
93 | * @return $this |
||
94 | */ |
||
95 | public function FillBackground($r, $g, $b) |
||
100 | |||
101 | /** |
||
102 | * @param resource $imageResource |
||
103 | * |
||
104 | * @return resource |
||
105 | */ |
||
106 | public function Execute($imageResource) |
||
125 | } |
||
126 | } |
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.