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 | /** |
||
| 21 | * @var integer $y |
||
| 22 | */ |
||
| 23 | private $y = 0; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var integer $width |
||
| 27 | */ |
||
| 28 | private $width = 0; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var integer $height |
||
| 32 | */ |
||
| 33 | private $height = 0; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var mixed $geometry |
||
| 37 | */ |
||
| 38 | private $geometry; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var string $gravity |
||
| 42 | */ |
||
| 43 | private $gravity = 'center'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var boolean $repage |
||
| 47 | */ |
||
| 48 | private $repage = false; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @param integer $width The crop width. |
||
| 52 | * @throws InvalidArgumentException If the width argument is not valid. |
||
| 53 | * @return AbstractCropEffect |
||
| 54 | */ |
||
| 55 | public function setWidth($width) |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @return integer |
||
| 68 | */ |
||
| 69 | public function width() |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param integer $height The crop height. |
||
| 76 | * @throws InvalidArgumentException If the height argument is not valid. |
||
| 77 | * @return AbstractCropEffect |
||
| 78 | */ |
||
| 79 | View Code Duplication | public function setHeight($height) |
|
| 89 | |||
| 90 | /** |
||
| 91 | * @return integer |
||
| 92 | */ |
||
| 93 | public function height() |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The X coordinate of the cropped region's top left corner |
||
| 100 | * |
||
| 101 | * @param integer $x The x-position (in pixel) of the crop. |
||
| 102 | * @throws InvalidArgumentException If the x argument is not valid. |
||
| 103 | * @return AbstractCropEffect |
||
| 104 | */ |
||
| 105 | public function setX($x) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @return integer |
||
| 118 | */ |
||
| 119 | public function x() |
||
| 123 | |||
| 124 | /** |
||
| 125 | * The Y coordinate of the cropped region's top left corner |
||
| 126 | * |
||
| 127 | * @param integer $y The y-position (in pixel) of the crop. |
||
| 128 | * @throws InvalidArgumentException If the y argumnet is not valid. |
||
| 129 | * @return AbstractCropEffect |
||
| 130 | */ |
||
| 131 | public function setY($y) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * @return integer |
||
| 144 | */ |
||
| 145 | public function y() |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Set a complex geometry value. |
||
| 152 | * |
||
| 153 | * @param mixed $geometry The image geometry. |
||
| 154 | * @throws InvalidArgumentException If the geometry argument is not valid. |
||
| 155 | * @return AbstractCropEffect |
||
| 156 | */ |
||
| 157 | View Code Duplication | public function setGeometry($geometry) |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Retrieve the complex geometry value. |
||
| 170 | * |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | public function geometry() |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param string $gravity The crop gravity. |
||
| 180 | * @throws InvalidArgumentException If the argument is not a valid gravity name. |
||
| 181 | * @return AbstractCropEffect |
||
| 182 | */ |
||
| 183 | View Code Duplication | public function setGravity($gravity) |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @return string |
||
| 196 | */ |
||
| 197 | public function gravity() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param boolean $repage The repage image flag. |
||
| 204 | * @return AbstractCropEffect |
||
| 205 | */ |
||
| 206 | public function setRepage($repage) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @return boolean |
||
| 214 | */ |
||
| 215 | public function repage() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param array $data The effect data. |
||
| 222 | * @return AbstractCropEffect |
||
| 223 | */ |
||
| 224 | public function process(array $data = null) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param integer $width The crop width. |
||
| 247 | * @param integer $height The crop height. |
||
| 248 | * @param integer $x The x-position (in pixel) of the crop. |
||
| 249 | * @param integer $y The y-position (in pixel) of the crop. |
||
| 250 | * @return void |
||
| 251 | */ |
||
| 252 | abstract protected function doCrop($width, $height, $x, $y); |
||
| 253 | } |
||
| 254 |
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.