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:
Complex classes like Image_Core often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Image_Core, and based on these observations, apply Extract Interface, too.
| 1 | <?php defined('SYSPATH') or die('No direct access allowed.'); |
||
| 13 | class Image_Core |
||
| 14 | { |
||
| 15 | |||
| 16 | // Master Dimension |
||
| 17 | const NONE = 1; |
||
| 18 | const AUTO = 2; |
||
| 19 | const HEIGHT = 3; |
||
| 20 | const WIDTH = 4; |
||
| 21 | // Flip Directions |
||
| 22 | const HORIZONTAL = 5; |
||
| 23 | const VERTICAL = 6; |
||
| 24 | |||
| 25 | // Allowed image types |
||
| 26 | public static $allowed_types = array( |
||
| 27 | IMAGETYPE_GIF => 'gif', |
||
| 28 | IMAGETYPE_JPEG => 'jpg', |
||
| 29 | IMAGETYPE_PNG => 'png', |
||
| 30 | IMAGETYPE_TIFF_II => 'tiff', |
||
| 31 | IMAGETYPE_TIFF_MM => 'tiff', |
||
| 32 | ); |
||
| 33 | |||
| 34 | // Driver instance |
||
| 35 | protected $driver; |
||
| 36 | |||
| 37 | // Driver actions |
||
| 38 | protected $actions = array(); |
||
| 39 | |||
| 40 | // Reference to the current image filename |
||
| 41 | protected $image = ''; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Creates a new Image instance and returns it. |
||
| 45 | * |
||
| 46 | * @param string filename of image |
||
| 47 | * @param array non-default configurations |
||
| 48 | * @return object |
||
|
|
|||
| 49 | */ |
||
| 50 | public static function factory($image, $config = null) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Creates a new image editor instance. |
||
| 57 | * |
||
| 58 | * @throws Kohana_Exception |
||
| 59 | * @param string filename of image |
||
| 60 | * @param array non-default configurations |
||
| 61 | * @return void |
||
| 62 | */ |
||
| 63 | public function __construct($image, $config = null) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Handles retrieval of pre-save image properties |
||
| 130 | * |
||
| 131 | * @param string property name |
||
| 132 | * @return string |
||
| 133 | */ |
||
| 134 | public function __get($property) |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Resize an image to a specific width and height. By default, Kohana will |
||
| 145 | * maintain the aspect ratio using the width as the master dimension. If you |
||
| 146 | * wish to use height as master dim, set $image->master_dim = Image::HEIGHT |
||
| 147 | * This method is chainable. |
||
| 148 | * |
||
| 149 | * @throws Kohana_Exception |
||
| 150 | * @param integer width |
||
| 151 | * @param integer height |
||
| 152 | * @param integer one of: Image::NONE, Image::AUTO, Image::WIDTH, Image::HEIGHT |
||
| 153 | * @return Image_Core |
||
| 154 | */ |
||
| 155 | public function resize($width, $height, $master = null) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Crop an image to a specific width and height. You may also set the top |
||
| 187 | * and left offset. |
||
| 188 | * This method is chainable. |
||
| 189 | * |
||
| 190 | * @throws Kohana_Exception |
||
| 191 | * @param integer width |
||
| 192 | * @param integer height |
||
| 193 | * @param integer top offset, pixel value or one of: top, center, bottom |
||
| 194 | * @param integer left offset, pixel value or one of: left, center, right |
||
| 195 | * @return Image_Core |
||
| 196 | */ |
||
| 197 | public function crop($width, $height, $top = 'center', $left = 'center') |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Allows rotation of an image by 180 degrees clockwise or counter clockwise. |
||
| 231 | * |
||
| 232 | * @param integer degrees |
||
| 233 | * @return Image_Core |
||
| 234 | */ |
||
| 235 | public function rotate($degrees) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Flip an image horizontally or vertically. |
||
| 260 | * |
||
| 261 | * @throws Kohana_Exception |
||
| 262 | * @param integer direction |
||
| 263 | * @return Image_Core |
||
| 264 | */ |
||
| 265 | public function flip($direction) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Change the quality of an image. |
||
| 278 | * |
||
| 279 | * @param integer quality as a percentage |
||
| 280 | * @return Image_Core |
||
| 281 | */ |
||
| 282 | public function quality($amount) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Sharpen an image. |
||
| 291 | * |
||
| 292 | * @param integer amount to sharpen, usually ~20 is ideal |
||
| 293 | * @return Image_Core |
||
| 294 | */ |
||
| 295 | public function sharpen($amount) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Save the image to a new image or overwrite this image. |
||
| 304 | * |
||
| 305 | * @throws Kohana_Exception |
||
| 306 | * @param string new image filename |
||
| 307 | * @param integer permissions for new image |
||
| 308 | * @param boolean keep or discard image process actions |
||
| 309 | * @return object |
||
| 310 | */ |
||
| 311 | public function save($new_image = false, $chmod = 0644, $keep_actions = false) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Output the image to the browser. |
||
| 344 | * |
||
| 345 | * @param boolean keep or discard image process actions |
||
| 346 | * @return object |
||
| 347 | */ |
||
| 348 | public function render($keep_actions = false) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Sanitize a given value type. |
||
| 372 | * |
||
| 373 | * @param string type of property |
||
| 374 | * @param mixed property value |
||
| 375 | * @param string $type |
||
| 376 | * @return boolean |
||
| 377 | */ |
||
| 378 | protected function valid_size($type, & $value) |
||
| 430 | } // End Image |
||
| 431 |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.