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 |
||
13 | abstract class AbstractCropEffect extends AbstractEffect |
||
14 | { |
||
15 | /** |
||
16 | * @var integer $x |
||
17 | */ |
||
18 | private $x = 0; |
||
19 | /** |
||
20 | * @var integer $y |
||
21 | */ |
||
22 | private $y = 0; |
||
23 | /** |
||
24 | * @var integer $width |
||
25 | */ |
||
26 | private $width = 0; |
||
27 | /** |
||
28 | * @var integer $height |
||
29 | */ |
||
30 | private $height = 0; |
||
31 | |||
32 | /** |
||
33 | * @param int $width |
||
34 | * @throws InvalidArgumentException |
||
35 | * @return Rotate Chainable |
||
36 | */ |
||
37 | public function setWidth($width) |
||
47 | |||
48 | /** |
||
49 | * @return float |
||
50 | */ |
||
51 | public function width() |
||
55 | |||
56 | /** |
||
57 | * @param integer $height |
||
58 | * @throws InvalidArgumentException |
||
59 | * @return $this Chainable |
||
60 | */ |
||
61 | View Code Duplication | public function setHeight($height) |
|
71 | |||
72 | /** |
||
73 | * @return float |
||
74 | */ |
||
75 | public function height() |
||
79 | |||
80 | /** |
||
81 | * The X coordinate of the cropped region's top left corner |
||
82 | * |
||
83 | * @param integer $x |
||
84 | * @throws InvalidArgumentException |
||
85 | * @return $this Chainable |
||
86 | */ |
||
87 | public function setX($x) |
||
97 | |||
98 | /** |
||
99 | * @return float |
||
100 | */ |
||
101 | public function x() |
||
102 | { |
||
103 | return $this->x; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * The Y coordinate of the cropped region's top left corner |
||
108 | * |
||
109 | * @param integer $y |
||
110 | * @throws InvalidArgumentException |
||
111 | * @return $this Chainable |
||
112 | */ |
||
113 | public function setY($y) |
||
114 | { |
||
115 | if (!is_int($y) || ($y < 0)) { |
||
116 | throw new InvalidArgumentException( |
||
117 | 'Height must be a positive integer' |
||
118 | ); |
||
119 | } |
||
120 | $this->y = $y; |
||
121 | return $this; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @return float |
||
126 | */ |
||
127 | public function x() |
||
131 | |||
132 | /** |
||
133 | * @param array $data |
||
134 | * @throws Exception |
||
135 | * @return AbstractResizeEffect Chainable |
||
136 | */ |
||
137 | public function process(array $data = null) |
||
152 | |||
153 | /** |
||
154 | * @param integer $width |
||
155 | * @param integer $height |
||
156 | * @param boolean $best_fit |
||
157 | * @return void |
||
158 | */ |
||
159 | abstract protected function doCrop($x, $y, $width, $height); |
||
160 | } |
||
161 |
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.